我正在开发一个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发布于 2021-08-18 21:28:31
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);
}https://stackoverflow.com/questions/68828020
复制相似问题