gpt4 book ai didi

c# - 为什么 SetWinEventHook with EVENT_OBJECT_LOCATIONCHANGE 在鼠标移动时触发?

转载 作者:行者123 更新时间:2023-11-30 17:45:12 24 4
gpt4 key购买 nike

我在进程上创建了一个 Hook ,以便在其窗口移动时进行注册。我使用事件常量 EVENT_OBJECT_LOCATIONCHANGE,根据 MSDN

An object has changed location, shape, or size. The system sends this event for the following user interface elements: caret and window objects. Server applications send this event for their accessible objects.

它有效,但它也会在简单的鼠标悬停在应用程序上时触发。谁能解释一下为什么?

这里是一个重现它的例子:

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

namespace WindowsFormsApplication1
{
static class NativeMethods
{
[DllImport("user32.dll")]
public static extern System.IntPtr SetWinEventHook(uint eventMin, uint eventMax, System.IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);

public delegate void WinEventDelegate(System.IntPtr hWinEventHook, uint eventType, System.IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
}

public partial class Form1 : Form
{
private const uint WINEVENT_OUTOFCONTEXT = 0x0000;
private const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;

public Form1()
{
InitializeComponent();
int processId = Process.GetCurrentProcess().Id;

NativeMethods.SetWinEventHook(EVENT_OBJECT_LOCATIONCHANGE, EVENT_OBJECT_LOCATIONCHANGE, System.IntPtr.Zero, WinEventProc, (uint)processId, (uint)0, WINEVENT_OUTOFCONTEXT);
}

private void WinEventProc(System.IntPtr hWinEventHook, uint eventType, System.IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
if (hwnd == IntPtr.Zero)
{
Debug.WriteLine("Mouse moved");
}
else
{
Debug.WriteLine("Location changed");
}
}
}
}

最佳答案

头文件 WinUser.h 有关于 EVENT_OBJECT_LOCATIONCHANGE 的说明:

 * Note also that USER will generate LOCATIONCHANGE notifications for two
* non-window sys objects:
* (1) System caret
* (2) Cursor

这解释了为什么当光标移动时会触发该事件。

正如 Hans 在他的评论中所说,只需按 OBJID_CURSOR 过滤对象 ID。

关于c# - 为什么 SetWinEventHook with EVENT_OBJECT_LOCATIONCHANGE 在鼠标移动时触发?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28195487/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com