显示指针的位置

时间:2016-01-21 19:48:33

标签: c# cursor mouse

我正在尝试实施截屏视频应用,我需要在点击时显示鼠标的位置。

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

是否有任何好的解决方案而不是仅仅在画布上绘制一个带有故事板的圆圈来变小然后消失?

2 个答案:

答案 0 :(得分:0)

您可以使用Cursor.Position来获取鼠标位置 然后,您可以围绕光标位置绘制圆圈。

因此,如果您需要按键操作,就像使用ctrl场景一样,您可以为按键绑定事件侦听器,或者如果您需要常量,则可以在鼠标移动时设置事件侦听器。

希望它会有所帮助 Liron

答案 1 :(得分:0)

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

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);
}