鼠标单击右击双击简单功能的实现
在糊里糊涂的情况下,我实现了鼠标的左击右击和双击,感觉什么都不知道似地,貌似HOOK里面有点复杂,搞不清楚诶,那位高人能指点哈子。最好了。
FORM1.CS代码
事件代码 网络上自己找的。
FORM1.CS代码
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace hook鼠标控制
{
public partial class Form1 : Form
{
hook鼠标控制.MouseHook mouse;
public Form1()
{
InitializeComponent();
mouse = new MouseHook();
mouse.OnMouseActivity += new MouseEventHandler(this.mouse_OnMouseActivity);
}
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool SetDoubleClickTime(uint uInterval);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
int i = 0;
int j = 0;
private void button1_Click(object sender, EventArgs e)
{
//mouse.Start();
i = Convert.ToInt16(tb_times.Text.Trim());
z = 0;
j = 0;
this.time_timer.Interval = Convert.ToInt16(this.tb_time.Text.Trim());
this.time_timer.Start();
}
private void mouse_OnMouseActivity(object sender, MouseEventArgs e)
{
this.lb_x.Text = e.X.ToString();
this.lb_y.Text = e.Y.ToString();
this.lb_z.Text = e.Button.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
lb_xx.Text = lb_x.Text;
lb_yy.Text = lb_y.Text;
}
private void time_timer_Tick(object sender, EventArgs e)
{
j++;
if (j > i)
this.time_timer.Stop();
if (this.cmb_clickWay.Text == "左击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
else if (this.cmb_clickWay.Text == "右击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
else if (this.cmb_clickWay.Text == "双击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
this.doubleclick_timer.Start();
}
}
int z = 0;
private void doubleclick_timer_Tick(object sender, EventArgs e)
{
z++;
if (z == 3)
doubleclick_timer.Stop();
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
}
using System.Collections.Generic;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
namespace hook鼠标控制
{
public partial class Form1 : Form
{
hook鼠标控制.MouseHook mouse;
public Form1()
{
InitializeComponent();
mouse = new MouseHook();
mouse.OnMouseActivity += new MouseEventHandler(this.mouse_OnMouseActivity);
}
[Flags]
enum MouseEventFlag : uint
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
XDown = 0x0080,
XUp = 0x0100,
Wheel = 0x0800,
VirtualDesk = 0x4000,
Absolute = 0x8000
}
[DllImport("user32.dll ", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool SetDoubleClickTime(uint uInterval);
[DllImport("user32.dll")]
static extern void mouse_event(MouseEventFlag flags, int dx, int dy, uint data, UIntPtr extraInfo);
[DllImport("user32.dll")]
static extern bool SetCursorPos(int X, int Y);
int i = 0;
int j = 0;
private void button1_Click(object sender, EventArgs e)
{
//mouse.Start();
i = Convert.ToInt16(tb_times.Text.Trim());
z = 0;
j = 0;
this.time_timer.Interval = Convert.ToInt16(this.tb_time.Text.Trim());
this.time_timer.Start();
}
private void mouse_OnMouseActivity(object sender, MouseEventArgs e)
{
this.lb_x.Text = e.X.ToString();
this.lb_y.Text = e.Y.ToString();
this.lb_z.Text = e.Button.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
lb_xx.Text = lb_x.Text;
lb_yy.Text = lb_y.Text;
}
private void time_timer_Tick(object sender, EventArgs e)
{
j++;
if (j > i)
this.time_timer.Stop();
if (this.cmb_clickWay.Text == "左击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
else if (this.cmb_clickWay.Text == "右击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
mouse_event(MouseEventFlag.RightDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.RightUp, 0, 0, 0, UIntPtr.Zero);
}
else if (this.cmb_clickWay.Text == "双击")
{
SetCursorPos(Convert.ToInt16(this.lb_xx.Text.Trim()), Convert.ToInt16(this.lb_yy.Text.Trim()));
this.doubleclick_timer.Start();
}
}
int z = 0;
private void doubleclick_timer_Tick(object sender, EventArgs e)
{
z++;
if (z == 3)
doubleclick_timer.Stop();
mouse_event(MouseEventFlag.LeftDown, 0, 0, 0, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp, 0, 0, 0, UIntPtr.Zero);
}
}
}
事件代码 网络上自己找的。
Code