问题描述
在 C# 中,我想单击一个按钮.我不想使用像 button.click+=... 这样的按钮事件.我想单击 Form Application 中的按钮,然后单击我计算机的任何键.我试过 SendKeys,但效果不佳.
In C# I want to have a button clicked. I don't want to use a button event like button.click+=... I want to click on the button in Form Application, and any key of my computer will be clicked. I tried SendKeys but it doesn't work well.
我需要更多关于如何去做的想法.我试过了:
I need more ideas how to do it. I tried:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
Button[,] b;
List<char> chrlist = new List<char>();
public Form5()
{
InitializeComponent();
start();
}
public void start()
{
panel1.Controls.Clear();
int m = 13;
int n = 2;
b = new Button[n, m];
int x = 0;
int i, j;
int y = 0;
// int count = 0;
int chr1=(int)'A';
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
// count++;
b[i, j] = new Button();
b[i, j].SetBounds(x, y, panel1.Size.Width / m, panel1.Size.Height / n);
b[i, j].Name = i + "," + j;
b[i, j].Text = ((char)chr1).ToString();
b[i, j].Click += new EventHandler(ButtonClick);
panel1.Controls.Add(b[i, j]);
x = x + panel1.Size.Width / m;
chr1++;
}
y = y + panel1.Size.Height / n;
x = 0;
}
}
public void ButtonClick(Object sender, EventArgs e)
{
Button a = (Button)sender;
MessageBox.Show(a.Text);
}
public void started()
{
while (true)
{
int sec = 1000;
Thread.Sleep(sec * 3);
SendKeys.SendWait("{TAB}");
}
}
private void button1_Click(object sender, EventArgs e)
{
Thread workerThread = new Thread(started);
workerThread.Start();
}
}
}
这在我的其他应用程序中不起作用.
And that didn't work in my other application.
推荐答案
在我看来,您正在尝试构建一个屏幕键盘.
It looks to me like you're attempting to build an onscreen keyboard.
前提:使用 SendKeys 向活动应用程序发送一封信.
Premise: Use SendKeys to send a letter to the active application.
问题:当您单击表单中的按钮发送信件时,哪个应用程序具有焦点?
Question: When you click the button in your Form to send a letter, what application has focus?
答案:你的.因此,密钥将发送到您的应用程序,而不是最后一个获得焦点的应用程序.
Answer: Yours. Therefore the key will be sent to your application, not the last application that was focused.
解决方案:防止您的应用程序集中注意力.
Solution: Prevent your application from getting focused.
这可以通过 CreateParams() 在扩展窗口样式中设置 WS_EX_NOACTIVATE 标志来实现
This can be accomplished by setting the WS_EX_NOACTIVATE flag in your extended window styles via CreateParams()
private const int WS_EX_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.ExStyle |= WS_EX_NOACTIVATE;
return p;
}
}
public void ButtonClick(Object sender, EventArgs e)
{
SendKeys.Send(((Control)sender).Text);
}
现在,当您单击 OSK 中的按钮时,当前聚焦的应用程序将保持聚焦(因为您的应用程序无法获得焦点)并且 SendKeys() 将正确定位它.
Now when you click a button in your OSK, the currently focused application will STAY focused (because your application cannot receive focus) and SendKeys() will correctly target it.
*注意事项:仅当屏幕键盘是不同的应用程序时才有效.换句话说,OSK 不能针对您自己的应用程序中的其他表单……这是一个奇怪的焦点问题.要定位到您自己的应用程序,您必须手动跟踪应用程序中的最后一个表单,并在发送密钥之前再次为其设置焦点.
*Caveats: This only works if the onscreen keyboard is a different application. In other words, the OSK cannot target other forms in your own application...it's a weird focus thing. To target your own app you'd have to manually track the last form in your application and give it focus again before sending the keys.
这是您向记事本发送密钥的 OSK:
Here's your OSK sending keys to Notepad:
这是我的测试应用中的全部代码:
This is ALL of the code in my test app:
public partial class Form1 : Form
{
private Button[,] b;
private const int WS_EX_NOACTIVATE = 0x08000000;
protected override CreateParams CreateParams
{
get
{
CreateParams p = base.CreateParams;
p.ExStyle |= WS_EX_NOACTIVATE;
return p;
}
}
public Form1()
{
InitializeComponent();
start();
}
public void start()
{
panel1.Controls.Clear();
int m = 13;
int n = 2;
b = new Button[n, m];
int x = 0;
int i, j;
int y = 0;
// int count = 0;
int chr1 = (int)'A';
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
// count++;
b[i, j] = new Button();
b[i, j].SetBounds(x, y, panel1.Size.Width / m, panel1.Size.Height / n);
b[i, j].Name = i + "," + j;
b[i, j].Text = ((char)chr1).ToString();
b[i, j].Click += new EventHandler(ButtonClick);
panel1.Controls.Add(b[i, j]);
x = x + panel1.Size.Width / m;
chr1++;
}
y = y + panel1.Size.Height / n;
x = 0;
}
}
public void ButtonClick(Object sender, EventArgs e)
{
SendKeys.Send(((Control)sender).Text);
}
}
这篇关于单击按钮,然后单击键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!