RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 628181
Accepted
QuaternioNoir
QuaternioNoir
Asked:2020-02-15 21:26:06 +0000 UTC2020-02-15 21:26:06 +0000 UTC 2020-02-15 21:26:06 +0000 UTC

WinForm C# 中的事件处理

  • 772

存在以下问题:组件位于窗体上,窗体是在没有控件的情况下创建的,桌面上窗体移动的处理由“自己的”代码执行。特别是,处理单击窗体(按下/释放鼠标左键)和鼠标指针移动的事件。位于窗体上的组件占据窗体的整个空间,分别为它处理鼠标事件,而不是为窗体处理鼠标事件。有没有什么办法可以忽略组件的这些事件,但为表单处理它们?

c#
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    QuaternioNoir
    2020-02-16T16:12:44Z2020-02-16T16:12:44Z

    最好的结果来自使用鼠标挂钩。我检查鼠标是否在申请表上,然后经典地处理鼠标事件(向下、向上、移动)。我这里给出钩子代码,说不定有人需要。代码是在网上找到的。

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Linq;
    using System.Runtime.InteropServices;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace MouseHook
    {
        public static class MouseHook
        {
            #region Declarations
            public static event MouseEventHandler MouseDown;
            public static event MouseEventHandler MouseUp;
            public static event MouseEventHandler MouseMove;
    
            [StructLayout(LayoutKind.Sequential)]
            struct MOUSEHOOKSTRUCT
            {
                public POINT pt;
                public IntPtr hwnd;
                public int wHitTestCode;
                public IntPtr dwExtraInfo;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            struct MSLLHOOKSTRUCT
            {
                public POINT pt;
                public int mouseData;
                public int flags;
                public int time;
                public IntPtr dwExtraInfo;
            }
    
            [StructLayout(LayoutKind.Sequential)]
            struct POINT
            {
                public int X;
                public int Y;
    
                public POINT(int x, int y)
                {
                    this.X = x;
                    this.Y = y;
                }
    
                public static implicit operator Point(POINT p)
                {
                    return new Point(p.X, p.Y);
                }
    
                public static implicit operator POINT(Point p)
                {
                    return new POINT(p.X, p.Y);
                }
            }
    
            const int WM_LBUTTONDOWN = 0x201;
            const int WM_LBUTTONUP = 0x202;
            const int WM_MOUSEMOVE = 0x0200;
            const int WM_MOUSEWHEEL = 0x020A;
            const int WM_RBUTTONDOWN = 0x0204;
            const int WM_RBUTTONUP = 0x0205;
            const int WM_MBUTTONUP = 0x208;
            const int WM_MBUTTONDOWN = 0x207;
            const int WM_XBUTTONDOWN = 0x20B;
            const int WM_XBUTTONUP = 0x20C;
    
            static IntPtr hHook = IntPtr.Zero;
            static IntPtr hModule = IntPtr.Zero;
            static bool hookInstall = false;
            static bool localHook = true;
            static API.HookProc hookDel;
            #endregion
    
            /// <summary>
            /// Hook install method.
            /// </summary>
            public static void InstallHook()
            {
                if (IsHookInstalled)
                    return;
    
                hModule = Marshal.GetHINSTANCE(AppDomain.CurrentDomain.GetAssemblies()[0].GetModules()[0]);
                hookDel = new API.HookProc(HookProcFunction);
    
                if (localHook)
                    hHook = API.SetWindowsHookEx(API.HookType.WH_MOUSE,
                        hookDel, IntPtr.Zero, AppDomain.GetCurrentThreadId()); // Если подчеркивает необращай внимание, так надо.
                else
                    hHook = API.SetWindowsHookEx(API.HookType.WH_MOUSE_LL,
                        hookDel, hModule, 0);
    
                if (hHook != IntPtr.Zero)
                    hookInstall = true;
                else
                    throw new Win32Exception("Can't install low level keyboard hook!");
            }
            /// <summary>
            /// If hook installed return true, either false.
            /// </summary>
            public static bool IsHookInstalled
            {
                get { return hookInstall && hHook != IntPtr.Zero; }
            }
            /// <summary>
            /// Module handle in which hook was installed.
            /// </summary>
            public static IntPtr ModuleHandle
            {
                get { return hModule; }
            }
            /// <summary>
            /// If true local hook will installed, either global.
            /// </summary>
            public static bool LocalHook
            {
                get { return localHook; }
                set
                {
                    if (value != localHook)
                    {
                        if (IsHookInstalled)
                            throw new Win32Exception("Can't change type of hook than it install!");
                        localHook = value;
                    }
                }
            }
            /// <summary>
            /// Uninstall hook method.
            /// </summary>
            public static void UnInstallHook()
            {
                if (IsHookInstalled)
                {
                    if (!API.UnhookWindowsHookEx(hHook))
                        throw new Win32Exception("Can't uninstall low level keyboard hook!");
                    hHook = IntPtr.Zero;
                    hModule = IntPtr.Zero;
                    hookInstall = false;
                }
            }
            /// <summary>
            /// Hook process messages.
            /// </summary>
            /// <param name="nCode"></param>
            /// <param name="wParam"></param>
            /// <param name="lParam"></param>
            /// <returns></returns>
            static IntPtr HookProcFunction(int nCode, IntPtr wParam, [In] IntPtr lParam)
            {
                if (nCode == 0)
                {
                    if (localHook)
                    {
                        MOUSEHOOKSTRUCT mhs = (MOUSEHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSEHOOKSTRUCT));
                        #region switch
                        switch (wParam.ToInt32())
                        {
                            case WM_LBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Left,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_LBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Left,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Middle,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Middle,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MOUSEMOVE:
                                if (MouseMove != null)
                                    MouseMove(null,
                                        new MouseEventArgs(MouseButtons.None,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MOUSEWHEEL:
                                // Данный хук не позволяет узнать куда вращается колесо мыши.
                                break;
                            case WM_RBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Right,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_RBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Right,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            default:
                                //Debug.WriteLine(string.Format("X:{0}; Y:{1}; Handle:{2}; HitTest:{3}; EI:{4}; wParam:{5}; lParam:{6}",
                                //    mhs.pt.X, mhs.pt.Y, mhs.hwnd, mhs.wHitTestCode, mhs.dwExtraInfo, wParam.ToString(), lParam.ToString()));
                                break;
                        }
                        #endregion
                    }
                    else
                    {
                        MSLLHOOKSTRUCT mhs = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
    
                        #region switch
                        switch (wParam.ToInt32())
                        {
                            case WM_LBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Left,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_LBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Left,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Middle,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Middle,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MOUSEMOVE:
                                if (MouseMove != null)
                                    MouseMove(null,
                                        new MouseEventArgs(MouseButtons.None,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_MOUSEWHEEL:
                                if (MouseMove != null)
                                    MouseMove(null,
                                        new MouseEventArgs(MouseButtons.None, mhs.time,
                                            mhs.pt.X, mhs.pt.Y, mhs.mouseData >> 16));
                                //Debug.WriteLine(string.Format("X:{0}; Y:{1}; MD:{2}; Time:{3}; EI:{4}; wParam:{5}; lParam:{6}",
                                //            mhs.pt.X, mhs.pt.Y, mhs.mouseData, mhs.time, mhs.dwExtraInfo, wParam.ToString(), lParam.ToString()));
                                break;
                            case WM_RBUTTONDOWN:
                                if (MouseDown != null)
                                    MouseDown(null,
                                        new MouseEventArgs(MouseButtons.Right,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            case WM_RBUTTONUP:
                                if (MouseUp != null)
                                    MouseUp(null,
                                        new MouseEventArgs(MouseButtons.Right,
                                            1,
                                            mhs.pt.X,
                                            mhs.pt.Y,
                                            0));
                                break;
                            default:
    
                                break;
                        }
                        #endregion
                    }
                }
    
                return API.CallNextHookEx(hHook, nCode, wParam, lParam);
            }
        }
    
        static class API
        {
            public delegate IntPtr HookProc(int nCode, IntPtr wParam, [In] IntPtr lParam);
    
            [DllImport("user32.dll")]
            public static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, [In] IntPtr lParam);
    
            [DllImport("user32.dll", SetLastError = true)]
            public static extern IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn,
            IntPtr hMod, int dwThreadId);
    
            [DllImport("user32.dll", SetLastError = true)]
            public static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
            [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr GetModuleHandle(string lpModuleName);
    
            public enum HookType : int
            {
                WH_JOURNALRECORD = 0,
                WH_JOURNALPLAYBACK = 1,
                WH_KEYBOARD = 2,
                WH_GETMESSAGE = 3,
                WH_CALLWNDPROC = 4,
                WH_CBT = 5,
                WH_SYSMSGFILTER = 6,
                WH_MOUSE = 7,
                WH_HARDWARE = 8,
                WH_DEBUG = 9,
                WH_SHELL = 10,
                WH_FOREGROUNDIDLE = 11,
                WH_CALLWNDPROCRET = 12,
                WH_KEYBOARD_LL = 13,
                WH_MOUSE_LL = 14
            }
        }
    }
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5