C# winform 模拟键盘鼠标操作

时间:2022-07-13 07:45:07

1、获取鼠标位置

private void timer1_Tick(object sender, EventArgs e)
{
// timer1.Stop();
// int x = Control.MousePosition.X;
// int y = Control.MousePosition.Y;
// label1.Text = string.Format("x:{0} y:{1}",x,y);
// timer1.Start(); label1.Text = mouse();
} private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
} public string mouse()
{
int x = Control.MousePosition.X;
int y = Control.MousePosition.Y;
string mouse_inf = "x:" + x.ToString() + " y:" + y.ToString();
return mouse_inf;
} }
}
timer 是一个触发器 开始搞不明白  用了mousemove事件  发现移出窗口就不触发了。后来找到了这个。

2、获取鼠标点击的类
        public static string mouse_position()
{
int x = Control.MousePosition.X;
int y = Control.MousePosition.Y;
string str_click = "null";
MouseButtons mb = Control.MouseButtons;
if (mb == System.Windows.Forms.MouseButtons.Left) str_click = "Left";
if (mb == System.Windows.Forms.MouseButtons.Right) str_click = "Right";
if (mb == System.Windows.Forms.MouseButtons.Middle) str_click = "Middle"; string mouse_inf = string.Format("{0},{1},{2}", x, y, str_click);
return mouse_inf;
}

调用

 string get = mouse_position();
label1.Text = get;
string[] mouse = get.Split(',');
if (mouse[] != "null")
{
listBox1.Items.Add(get);
}

模拟按键输入

 SendKeys.Send(send_string);