C# 显示指针的位置

C# 显示指针的位置,c#,cursor,mouse,C#,Cursor,Mouse,我正在尝试实现一个screencast应用程序,我需要在单击时显示鼠标的位置 如果在(鼠标属性->指针选项->按下CTRL键时显示指针位置)上启用该选项,我如何显示指针(鼠标)的位置,就像在windows上默认使用CTRL键一样 除了在画布上用故事板画一个圆圈来变小然后消失之外,还有什么好的解决方案吗?你可以使用光标来获得鼠标位置。定位 然后可以围绕光标位置绘制圆 因此,如果您需要像ctrl场景那样的按键,您可以为按键绑定一个事件侦听器,或者如果您需要某个常量,您可以在鼠标移动时设置一个事件侦听

我正在尝试实现一个screencast应用程序,我需要在单击时显示鼠标的位置

如果在(鼠标属性->指针选项->按下CTRL键时显示指针位置)上启用该选项,我如何显示指针(鼠标)的位置,就像在windows上默认使用CTRL键一样


除了在画布上用故事板画一个圆圈来变小然后消失之外,还有什么好的解决方案吗?

你可以使用
光标来获得鼠标位置。定位

然后可以围绕光标位置绘制圆

因此,如果您需要像ctrl场景那样的按键,您可以为按键绑定一个事件侦听器,或者如果您需要某个常量,您可以在鼠标移动时设置一个事件侦听器

希望这会有所帮助

Liron

您可以使用
光标来获取鼠标位置。位置

然后可以围绕光标位置绘制圆

因此,如果您需要像ctrl场景那样的按键,您可以为按键绑定一个事件侦听器,或者如果您需要某个常量,您可以在鼠标移动时设置一个事件侦听器

希望这会有所帮助

Liron

过了一段时间,我用那个代码做了

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class MouseHook
{
    public class MSLLHOOKSTRUCTEventArgs : EventArgs
    {
        private readonly MSLLHOOKSTRUCT _hookStruct;

        public MSLLHOOKSTRUCTEventArgs(MSLLHOOKSTRUCT hookStruct)
        {
            _hookStruct = hookStruct;
        }

        public MSLLHOOKSTRUCT HookStruct
        {
            get { return _hookStruct; }
        }
    }

    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseDown = delegate { };
    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseUp = delegate { };
    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseMove = delegate { };
    public static bool Started { get; set; }

    static MouseHook()
    {
        Started = false;
    }
    public static void Start()
    {
        Debug.WriteLine("MouseHook.Start");
        _hookID = SetHook(_proc);
        Started = true;
    }

    public static void Stop()
    {
        Debug.WriteLine("MouseHook.Stop");
        UnhookWindowsHookEx(_hookID);
        Started = false;
        MouseDown = null;
        MouseUp = null;
        MouseMove = null;
    }

    private static LowLevelMouseProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle("user32"), 0);

        if (hook == IntPtr.Zero)
            throw new System.ComponentModel.Win32Exception();

        return hook;
    }

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0)
        {
            MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
            {
                if(MouseDown!=null)
                    MouseDown(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }
            else if (MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
            {
                if (MouseUp != null)
                    MouseUp(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }
            else
            {
                if (MouseMove != null)
                    MouseMove(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }

        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private const int WH_MOUSE = 7;
    private const int WH_MOUSE_LL = 14;

    private enum MouseMessages
    {
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public uint mouseData;
        public uint flags;
        public uint time;
        public IntPtr 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);
}
使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
公共静态类鼠标指针
{
公共类MSLLHOOKSTRUCTEventArgs:EventArgs
{
私有只读MSLLHOOKSTRUCT\u hookStruct;
公共MSLLHOOKSTRUCTEventArgs(MSLLHOOKSTRUCT-hookStruct)
{
_hookStruct=hookStruct;
}
公共MSLLHOOKSTRUCT HookStruct
{
获取{return\u hookStruct;}
}
}
public static event EventHandler MouseDown=委托{};
公共静态事件EventHandler MouseUp=delegate{};
公共静态事件EventHandler MouseMove=delegate{};
公共静态bool已启动{get;set;}
静态鼠标挂钩()
{
开始=错误;
}
公共静态void Start()
{
Debug.WriteLine(“MouseHook.Start”);
_hookID=SetHook(_proc);
开始=真;
}
公共静态无效停止()
{
Debug.WriteLine(“MouseHook.Stop”);
unhookwindowshookx(_hookID);
开始=错误;
MouseDown=null;
MouseUp=null;
MouseMove=null;
}
私有静态LowLevelMouseProc\u proc=HookCallback;
私有静态IntPtr _hookID=IntPtr.Zero;
私有静态IntPtr SetHook(LowLevelMouseProc)
{
IntPtr hook=setWindowshookx(WH_MOUSE_LL,proc,GetModuleHandle(“user32”),0);
if(hook==IntPtr.Zero)
抛出新的System.ComponentModel.Win32Exception();
回程钩;
}
私有委托IntPtr LowLevelMouseProc(intncode、IntPtr wParam、IntPtr lParam);
专用静态IntPtr钩子回调(int nCode、IntPtr wParam、IntPtr lParam)
{
如果(nCode>=0)
{
MSLLHOOKSTRUCT hookStruct=(MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam,typeof(MSLLHOOKSTRUCT));
if(MouseMessages.WM_LBUTTONDOWN==(MouseMessages)wParam)
{
if(MouseDown!=null)
MouseDown(null,新的MSLLHOOKSTRUCTEventArgs(hookStruct));
}
else if(MouseMessages.WM_LBUTTONUP==(MouseMessages)wParam)
{
if(MouseUp!=null)
MouseUp(null,新的MSLLHOOKSTRUCTEventArgs(hookStruct));
}
其他的
{
if(MouseMove!=null)
MouseMove(null,新的MSLLHOOKSTRUCTEventArgs(hookStruct));
}
}
返回CallNextHookEx(_hookID,nCode,wParam,lParam);
}
私有常量int WH_MOUSE=7;
私有常量int WH_MOUSE_LL=14;
私有枚举鼠标消息
{
WM_LBUTTONDOWN=0x0201,
WM_LBUTTONUP=0x0202,
WM_MOUSEMOVE=0x0200,
WM_鼠标滚轮=0x020A,
WM_RBUTTONDOWN=0x0204,
WM_RBUTTONUP=0x0205
}
[StructLayout(LayoutKind.Sequential)]
公共结构点
{
公共int x;
公共智力;
}
[StructLayout(LayoutKind.Sequential)]
公共结构MSLLHOOKSTRUCT
{
公共点;
公营部门;
国旗;
公共时间;
公共IntPtr dwExtraInfo;
}
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部IntPtr SetWindowsHookEx(intidhook、lovelmouseproc lpfn、IntPtr hMod、uint dwThreadId);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
[返回:Marshallas(UnmanagedType.Bool)]
私有静态外部bool unhookwindowshookx(IntPtr hhk);
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部IntPtr CallNextHookEx(IntPtr hhk、intncode、IntPtr wParam、IntPtr lParam);
[DllImport(“kernel32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
私有静态外部IntPtr GetModuleHandle(字符串lpModuleName);
}

过了一段时间,我用那个代码做了

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public static class MouseHook
{
    public class MSLLHOOKSTRUCTEventArgs : EventArgs
    {
        private readonly MSLLHOOKSTRUCT _hookStruct;

        public MSLLHOOKSTRUCTEventArgs(MSLLHOOKSTRUCT hookStruct)
        {
            _hookStruct = hookStruct;
        }

        public MSLLHOOKSTRUCT HookStruct
        {
            get { return _hookStruct; }
        }
    }

    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseDown = delegate { };
    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseUp = delegate { };
    public static event EventHandler<MSLLHOOKSTRUCTEventArgs> MouseMove = delegate { };
    public static bool Started { get; set; }

    static MouseHook()
    {
        Started = false;
    }
    public static void Start()
    {
        Debug.WriteLine("MouseHook.Start");
        _hookID = SetHook(_proc);
        Started = true;
    }

    public static void Stop()
    {
        Debug.WriteLine("MouseHook.Stop");
        UnhookWindowsHookEx(_hookID);
        Started = false;
        MouseDown = null;
        MouseUp = null;
        MouseMove = null;
    }

    private static LowLevelMouseProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;

    private static IntPtr SetHook(LowLevelMouseProc proc)
    {
        IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle("user32"), 0);

        if (hook == IntPtr.Zero)
            throw new System.ComponentModel.Win32Exception();

        return hook;
    }

    private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0)
        {
            MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
            {
                if(MouseDown!=null)
                    MouseDown(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }
            else if (MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam)
            {
                if (MouseUp != null)
                    MouseUp(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }
            else
            {
                if (MouseMove != null)
                    MouseMove(null, new MSLLHOOKSTRUCTEventArgs(hookStruct));
            }

        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    private const int WH_MOUSE = 7;
    private const int WH_MOUSE_LL = 14;

    private enum MouseMessages
    {
        WM_LBUTTONDOWN = 0x0201,
        WM_LBUTTONUP = 0x0202,
        WM_MOUSEMOVE = 0x0200,
        WM_MOUSEWHEEL = 0x020A,
        WM_RBUTTONDOWN = 0x0204,
        WM_RBUTTONUP = 0x0205
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct MSLLHOOKSTRUCT
    {
        public POINT pt;
        public uint mouseData;
        public uint flags;
        public uint time;
        public IntPtr 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);
}
使用系统;
使用系统诊断;
使用System.Runtime.InteropServices;
公共静态类鼠标指针
{
公共类MSLLHOOKSTRUCTEventArgs:EventArgs
{
私有只读MSLLHOOKSTRUCT\u hookStruct;
公共MSLLHOOKSTRUCTEventArgs(MSLLHOOKSTRUCT-hookStruct)
{
_hookStruct=hookStruct;
}
公共MSLLHOOKSTRUCT HookStruct
{
获取{return\u hookStruct;}
}
}
public static event EventHandler MouseDown=委托{};
公共静态事件EventHandler MouseUp=delegate{};
公共静态事件EventHandler MouseMove=delegate{};
公共静态bool已启动{get;set;}
静态鼠标挂钩()
{
开始=错误;
}
公共静态void Start()
{
调试命令