winforms 在C#中用鼠标全局钩子获取鼠标左键状态的整数

okxuctiv  于 2023-03-03  发布在  C#
关注(0)|答案(1)|浏览(203)

我是c#/winforms新手,我想把鼠标状态作为一个int来检查“if”语句(例如,左键单击)。
所以我找到了一个全局鼠标钩子[这里][1],效果很好。
但是我不明白我怎么能把鼠标的状态作为一个整型,我在论坛和维基上找了很长时间。
我在hookCallback中创建int时出错,其中检查了单击状态(如果我也理解)

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            MouseHook.Start();
            MouseHook.MouseAction += ne

w EventHandler(Event);
        }

    private void Event(object sender, EventArgs e) { Console.WriteLine("Left mouse click!"); }
}

public static class MouseHook
{
    public static event EventHandler MouseAction = delegate { };

    public static void Start()
    {
        _hookID = SetHook(_proc);

    }
    public static void stop()
    {
        UnhookWindowsHookEx(_hookID);
    }

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

    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)
    {
        if (nCode >= 0 && MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam)
        {
            MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
            MouseAction(null, new EventArgs());
           ******** public int LeftClick = 1; *********
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    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)]
    private struct POINT
    {
        public int x;
        public int y;
    }

    [StructLayout(LayoutKind.Sequential)]
    private 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);

}

}

q5lcpyga

q5lcpyga1#

将我的类添加到您的项目

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Input;

namespace Hector.Framework.Utils
{
    public class Peripherals
    {
        public class Mouse
        {
            public static int X
            {
                get => Cursor.Position.X;
            }

            public static int Y
            {
                get => Cursor.Position.Y;
            }

            public static void MoveToPoint(int X, int Y)
            {
                Win32.SetCursorPos(X, Y);
            }

            public static void Hide()
            {
                Cursor.Hide();
            }

            public static void Show()
            {
                Cursor.Show();
            }

            public static bool IsHidden
            {
                get => Cursor.Current == null;
            }

            /// <summary>
            /// ButtonNumber: 0-None 1-Left 2-Middle 3-Right 4-XButton1 5-XButton2
            /// </summary>
            public static bool MouseButtonIsDown(int ButtonNumber)
            {
                bool outValue = false;
                bool isLeft = Control.MouseButtons == MouseButtons.Left;
                bool isRight = Control.MouseButtons == MouseButtons.Right;
                bool isMiddle = Control.MouseButtons == MouseButtons.Middle;
                bool isXButton1 = Control.MouseButtons == MouseButtons.XButton1;
                bool isXButton2 = Control.MouseButtons == MouseButtons.XButton2;

                switch (ButtonNumber)
                {
                    case 0:
                        outValue = false;
                        break;
                    case 1:
                        outValue = isLeft;
                        break;
                    case 2:
                        outValue = isMiddle;
                        break;
                    case 3:
                        outValue = isRight;
                        break;
                    case 4:
                        outValue = isXButton1;
                        break;
                    case 5:
                        outValue = isXButton2;
                        break;
                }

                return outValue;
            }

            /// <summary>
            /// This function is in Alpha Phase
            /// </summary>
            /// <param name="FocusedControl">The control that is scrolled; If the control has no focus, it will be applied automatically</param>
            /// <param name="FontSize">This is used to calculate the number of pixels to move, its default value is 20</param>
            static int delta = 0;
            static int numberOfTextLinesToMove = 0;
            static int numberOfPixelsToMove = 0;
            public static bool GetWheelValues(Control FocusedControl, out int Delta, out int NumberOfTextLinesToMove, out int NumberOfPixelsToMove, int FontSize = 20)
            {
                try
                {
                    if (FocusedControl == null) throw new NullReferenceException("The FocusedControl can not bel null");
                    if (!FocusedControl.Focused) FocusedControl.Focus();
                    FocusedControl.MouseWheel += (object sender, MouseEventArgs e) =>
                    {
                        delta = e.Delta;
                        numberOfTextLinesToMove = e.Delta * SystemInformation.MouseWheelScrollLines / 120;
                        numberOfPixelsToMove = numberOfTextLinesToMove * FontSize;
                    };

                    Delta = delta;
                    NumberOfTextLinesToMove = numberOfTextLinesToMove;
                    NumberOfPixelsToMove = numberOfPixelsToMove;

                    return true;
                }
                catch
                {
                    Delta = 0;
                    NumberOfTextLinesToMove = 0;
                    NumberOfPixelsToMove = numberOfPixelsToMove;

                    return false;
                }
            }

            private class Win32
            {
                [DllImport("User32.Dll")]
                public static extern long SetCursorPos(int x, int y);

                [DllImport("User32.Dll")]
                public static extern bool ClientToScreen(IntPtr hWnd, ref POINT point);

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

然后,在需要检查鼠标状态的地方,编写以下代码:

bool MouseLeftButton = Hector.Framework.Utils.Peripherals.Mouse.MouseButtonIsDown(1);

记住:按下选定按钮时,bool为真,释放按钮时,bool为假
按钮编号:

  • 0 -无,
  • 1 -左侧,
  • 2 -中间,
  • 3 -右,
  • 4 -X按钮1,
  • 5 -X按钮2

相关问题