C#在窗体中点击任意位置鼠标左键获得该点坐标

private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            Point p = e.Location;
            MessageBox.Show(p.ToString());
        }
Microsoft Visual Studio 2010做的C#实时监控鼠标位置左键点击时的位置实例,主要代码: public class MouseHook { private Point point; private Point Point { get { return point; } set { if (point != value) { point = value; if (MouseMoveEvent != null) { var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0); MouseMoveEvent(this, e); } } } } private int hHook; private const int WM_LBUTTONDOWN = 0x201; public const int WH_MOUSE_LL = 14; public Win32Api.HookProc hProc; public MouseHook() { this.Point = new Point(); } public int SetHook() { hProc = new Win32Api.HookProc(MouseHookProc); hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0); return hHook; } public void UnHook() { Win32Api.UnhookWindowsHookEx(hHook); } private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam) { Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Api.MouseHookStruct)); if (nCode < 0) { return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } else { if (MouseClickEvent != null) { MouseButtons button = MouseButtons.None; int clickCount = 0; switch ((Int32)wParam) { case WM_LBUTTONDOWN: button = MouseButtons.Left; clickCount = 1; break; } var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0); MouseClickEvent(this, e); } this.Point = new Point(MyMouseHookStruct.pt.x, MyMouseHookStruct.pt.y); return Win32Api.CallNextHookEx(hHook, nCode, wParam, lParam); } } public delegate void MouseMoveHandler(object sender, MouseEventArgs e); public event MouseMoveHandler MouseMoveEvent; public delegate void MouseClickHandler(object sender, MouseEventArgs e); public event MouseClickHandler MouseClickEvent; }
以下是在C#实现查找窗体句柄,并在坐标100,100点击鼠标左键的示例代码: ```c# using System; using System.Runtime.InteropServices; public class MouseClicker { // 导入FindWindow和SendMessage函数 [DllImport("user32.dll", EntryPoint = "FindWindow")] private static extern IntPtr FindWindow(string className, string windowName); [DllImport("user32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam); // 定义窗口标题和鼠标点击坐标 private const string WINDOW_TITLE = "窗口标题"; private const int MOUSE_X = 100; private const int MOUSE_Y = 100; public static void Main() { // 查找窗口句柄 IntPtr hwnd = FindWindow(null, WINDOW_TITLE); // 如果找到窗口,则执行鼠标左键点击操作 if (hwnd != IntPtr.Zero) { // 将窗口激活,确保鼠标点击事件能够正常触发 SendMessage(hwnd, 0x0110, 0, 0); // 计算鼠标点击的屏幕坐标 int lParam = (MOUSE_Y << 16) | MOUSE_X; // 发送鼠标左键按下和抬起消息,完成鼠标点击操作 SendMessage(hwnd, 0x0201, 1, lParam); SendMessage(hwnd, 0x0202, 0, lParam); } else { Console.WriteLine("未找到窗口"); } } } ``` 在C#,需要使用`DllImport`特性导入`user32.dll`的`FindWindow`和`SendMessage`函数。首先定义了窗口标题和鼠标点击坐标,然后调用`FindWindow`函数查找窗口句柄。如果找到窗口,则将其激活,并计算鼠标点击的屏幕坐标。最后使用`SendMessage`函数发送鼠标左键按下和抬起消息,完成鼠标点击操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值