【问题标题】:VSTO Word addin - Detect mouse clickVSTO Word 插件 - 检测鼠标点击
【发布时间】:2021-08-18 07:18:10
【问题描述】:

我正在开发 VSTO Word 插件。 vsto 插件是否可以检测用户是否在文档区域之外单击。

如下图示例:检测是否单击了黑框内的任意位置(功能区和底部区域)

我不需要知道点击的是什么控件...只要点击发生在这两个黑框内。

对我有什么办法吗?谢谢。

更新:

Private Function MouseHookCallback(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    If nCode = 0 Then
        Dim mouseHookStruct = CType(Marshal.PtrToStructure(lParam, GetType(SafeNativeMethods.MouseHookStructEx)), SafeNativeMethods.MouseHookStructEx)
        Dim message = CType(wParam, SafeNativeMethods.WindowMessages)
        Debug.WriteLine("{0} event detected at position {1} - {2}", message, mouseHookStruct.pt.X, mouseHookStruct.pt.Y)
        If message = WindowMessages.WM_LBUTTONDOWN Or message = WindowMessages.WM_MBUTTONDOWN Or message = WindowMessages.WM_RBUTTONDOWN Or _
            message = WindowMessages.WM_MOUSEHWHEEL Or message = WindowMessages.WM_MOUSEWHEEL Then
            'do something here
        End If
        If message = WindowMessages.WM_MOUSEMOVE Then
            Debug.WriteLine("mouse move!!!!!!!!!!!!!!!!!!a with ncode=> " & nCode)
        End If
    End If

    Return SafeNativeMethods.CallNextHookEx(_hookIdKeyboard, nCode, wParam, lParam)
End Function

【问题讨论】:

    标签: vsto word


    【解决方案1】:

    VSTO 没有为此提供任何东西。但是您可以尝试为此设置一个键盘挂钩,请参阅Low-Level Keyboard Hook in C# 了解更多信息。

     using System;
    using System.Diagnostics;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    class InterceptKeys
    {
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
    
        public static void Main()
        {
            _hookID = SetHook(_proc);
            Application.Run();
            UnhookWindowsHookEx(_hookID);
        }
    
        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }
    
        private delegate IntPtr LowLevelKeyboardProc(
            int nCode, IntPtr wParam, IntPtr lParam);
    
        private static IntPtr HookCallback(
            int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine((Keys)vkCode);
            }
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook,
            LowLevelKeyboardProc 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);
    }
    

    【讨论】:

    • 谢谢@Eugene。好的,我已经实现了一个鼠标钩子(WH_MOUSE)而不是低级鼠标钩子(WH_MOUSE_LL)。设法检测到点击。但是现在的问题是,如何区分是点击发生在这两个黑框还是点击是在文档区域?
    • 好像不管我点击哪里,鼠标钩子回调都会被调用。不知道如何检测哪个区域发生了哪个点击。
    • 您可以尝试对可以处理点击事件的这两个元素进行子类化。另一种方法是检查鼠标指针的位置,更多信息请参见Getting mouse position in c#
    猜你喜欢
    • 2020-04-18
    • 2021-09-06
    • 2015-08-03
    相关资源
    最近更新 更多