在 C# 中使用 windows 形式鼠标左键的语音识别
speech recognition for mouse left click in windows form using in c#
我正在尝试编码为 windows 形式的鼠标左键单击。当我说 "left click" 时,我想在屏幕上的任意位置 单击鼠标左键 。我已经尝试了一些代码,但它给出了错误
这是代码:
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");
}
当我 运行 这个程序出错时 "Top-level control cannot be added to a control." 在 this.Controls.Add(Form1) 在上面的程序中。所以请任何人给我一个解决方案
顶级控件定义为不由另一个 Windows Forms 控件作为父级的父控件。通常,这是包含控件的最外层窗体。
在您的情况下,Form1
似乎是顶级控件。删除行:
this.Controls.Add(Form1);
我正在尝试编码为 windows 形式的鼠标左键单击。当我说 "left click" 时,我想在屏幕上的任意位置 单击鼠标左键 。我已经尝试了一些代码,但它给出了错误 这是代码:
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");
}
当我 运行 这个程序出错时 "Top-level control cannot be added to a control." 在 this.Controls.Add(Form1) 在上面的程序中。所以请任何人给我一个解决方案
顶级控件定义为不由另一个 Windows Forms 控件作为父级的父控件。通常,这是包含控件的最外层窗体。
在您的情况下,Form1
似乎是顶级控件。删除行:
this.Controls.Add(Form1);