C#__ 模拟鼠标单击事件

首先要用到的引用有

[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);

 

1、SetCursorPos:用于设置鼠标的坐标。与它对应的当然就是GetCursorPos啦!

2、mouse_event:这个事件是用于模仿鼠标的各种操作(单击、双击....),视输入参数的不同而定了。

 =============================================================================================

下面是完整的代码:

[DllImport("User32")]
public extern static void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
[DllImport("User32")]
public extern static void SetCursorPos(int x, int y);
[DllImport("User32")]
public extern static bool GetCursorPos(out POINT p);
[StructLayout(LayoutKind.Sequential)]
public struct POINT
{
public int X;
public int Y;
}
public enum MouseEventFlags
{
Move = 0x0001, //移动鼠标
LeftDown = 0x0002,//模拟鼠标左键按下
LeftUp = 0x0004,//模拟鼠标左键抬起
RightDown = 0x0008,//鼠标右键按下
RightUp = 0x0010,//鼠标右键抬起
MiddleDown = 0x0020,//鼠标中键按下 
MiddleUp = 0x0040,//中键抬起
Wheel = 0x0800,
Absolute = 0x8000//标示是否采用绝对坐标
}
private void button1_Click(object sender, EventArgs e)
{
// POINT p = new POINT();
// GetCursorPos(out p);
// MessageBox.Show(p.X.ToString()+":"+p.Y.ToString());
AutoClick();
}

public static void AutoClick()
{
while (true)
{

//设置鼠标的坐标

SetCursorPos(72, 40);

//这里模拟的是一个鼠标双击事件
mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);

mouse_event((int)(MouseEventFlags.LeftUp | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
mouse_event((int)(MouseEventFlags.LeftDown | MouseEventFlags.Absolute), 72, 40, 0, IntPtr.Zero);
}
}

 

 

 

posted @ 2013-07-09 16:35  cindy_Lu  阅读(8137)  评论(0编辑  收藏  举报