[英]speech recognition for mouse left click in windows form using in c#
我正在尝试编写鼠标左键单击Windows窗体。 当我说“左键单击”时,我想在屏幕上的任意位置左键单击鼠标。 我尝试了一些代码,但它给出的错误是代码:
public Form1()
{
InitializeComponent();
this.KeyPreview = true;
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
var Form1 = new Form();
Form1.Location = new Point(50, 50);
Form1.AutoSize = true;
Form1.Click += new EventHandler(Form1_Click);
this.Controls.Add(Form1);
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
public const int MOUSEEVENTF_LEFTDOWN = 0x02;
public const int MOUSEEVENTF_LEFTUP = 0x04;
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
SimulateLeftClick();
}
private void SimulateLeftClick()
{
int xpos = Cursor.Position.X;
int ypos = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
}
case "left click":
int xpos = Cursor.Position.X;
int ypos = Cursor.Position.Y;
mouse_event(MOUSEEVENTF_LEFTDOWN, xpos, ypos, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, xpos, ypos, 0, 0);
break;
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show("Left Click simulated");
}
当我运行此程序时,错误提供“顶级控件无法添加到控件”。 在上面的程序中的this.Controls.Add(Form1) 。 所以请任何人给我一个解决方案
顶级控件定义为不是另一个Windows窗体控件的父级控件。 通常,这是包含控件的最外面的Form。
在您的情况下,似乎Form1
是顶级控件。 删除行:
this.Controls.Add(Form1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.