如何跟踪鼠标X/Y位置并将其打印到标签上

本文关键字:位置 打印 标签 何跟踪 跟踪 鼠标 | 更新日期: 2023-09-27 18:20:26

我知道如何从MSDN上的示例中修改光标位置。

我的问题是如何在鼠标移动时检查鼠标的位置,然后打印X和Y位置以表示标签?

编辑:假设我想从整个屏幕上跟踪我的鼠标位置。

编辑2:我的应用程序将在后台/最小化。

我已经在使用鼠标挂钩:

namespace Program
{
    public partial class MouseHook
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, IntPtr dwExtraInfo);
        private const int MOUSEEVENTF_LEFTDOWN = 0x02;
        private const int MOUSEEVENTF_LEFTUP = 0x04;
        private const int MOUSEEVENTF_RIGHTDOWN = 0x08;
        private const int MOUSEEVENTF_RIGHTUP = 0x10;
        public void DoMouseClick()
        {
            int X = Cursor.Position.X;
            int Y = Cursor.Position.Y;
            IntPtr newP = new IntPtr(Convert.ToInt64("0", 16));
            mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, X, Y, 0, newP);
        }
    }
}

如何跟踪鼠标X/Y位置并将其打印到标签上

这是关于系统级鼠标事件处理调用(低级鼠标进程)的msdn文档。

下面是一个使用低级鼠标procs来更改滚动事件的示例。

在第二个链接的答案中,从这里使用WM_MOUSEMOVE而不是WM_MOUEWHEEL。

需要注意的一点是:当鼠标在具有提升权限的程序上时,为了让该程序继续捕获鼠标事件,必须使用提升权限启动该程序。

代码(未测试):

using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CatchMouseMove
{
    class InterceptMouse
    {
        const int INPUT_MOUSE = 0;
        const int MOUSEEVENTF_WHEEL = 0x0800;
        const int WH_MOUSE_LL = 14; 

        private static LowLevelMouseProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
        public static void Main()
        {
            _hookID = SetHook(_proc);
            if (_hookID == null)
            {
                MessageBox.Show("SetWindowsHookEx Failed");
                return;
            }
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }
        private static IntPtr SetHook(LowLevelMouseProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_MOUSE_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }
        private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            int xPos = 0;
            int yPos = 0;
            if (nCode >= 0 && MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam)
            {    
                xPos = GET_X_LPARAM(lParam); 
                yPos = GET_Y_LPARAM(lParam);
                //do stuff with xPos and yPos
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }

        private enum MouseMessages
        {
            WM_MOUSEMOVE = 0x0200
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct POINT
        {
            public int x;
            public int y;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct MSLLHOOKSTRUCT
        {
            public POINT pt;
            public int mouseData;
            public int flags;
            public int time;
            public IntPtr dwExtraInfo;
        }
        public struct INPUT
        {
            public int type;
            public MOUSEINPUT mi;
        }
        [StructLayout(LayoutKind.Sequential)]
        public struct MOUSEINPUT
        {
            public int dx;
            public int dy;
            public int mouseData;
            public uint dwFlags;
            public int time;
            public int dwExtraInfo;
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
            IntPtr wParam, IntPtr lParam);
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    }
}

您需要设置一个Windows挂钩才能实现这一点。MSDN上的文章"如何在Visual C#.NET中设置Windows挂钩"展示了如何设置鼠标挂钩。

我试过了,它捕捉了整个窗体上的鼠标,即使鼠标光标在控件上也是如此。

您可以使用MouseMove事件的EventArgs,因为它保存鼠标坐标。从那里,您可以轻松地将标签的文本属性设置为将从e(鼠标移动事件参数)获得的X或Y坐标。

private void Form_MouseMove(object sender, MouseEventArgs e)
{
    // Update the mouse coordinates displayed in the textbox.
    myTextBox.Text = e.Location.ToString();
}