1

我试图在关闭外部应用程序时收到警报。对于我的测试,我检索了记事本的 hwnd 并将其设置到我的显示器中。奇怪的是,每当我从记事本点击到其他应用程序时,即使记事本没有关闭,我也会收到系统事件。有谁知道为什么?

一些快速示例调用代码:

uint EVENT_OBJECT_DESTROY = 0x8001;
IntPtr notepadHwnd = <your pointer>
var mon = new WindowMonitor(notepadHwnd, EVENT_OBJECT_DESTROY);
mon.EventOccurred += (sender, args) => Console.WriteLine("closed");

监视器:

  public class WindowMonitor : IDisposable
  {
    //store delegate to prevent GC
    private User32.WinEventDelegate dEvent;

    private IntPtr _hook;
    private readonly IntPtr _window;
    private readonly List<uint> _watchedEvents = new List<uint>();

    public event EventHandler<AccessibleEventTypeEventArgs> EventOccurred;

    public WindowMonitor(IntPtr windowToMonitor, params User32.AccessibleEventType[] eventsToMonitor)
    {
      //prevent junk
      if (eventsToMonitor == null || eventsToMonitor.Length == 0)
        throw new ArgumentNullException("eventsToMonitor", "Must specify events to monitor");
      if (windowToMonitor == IntPtr.Zero)
        throw new ArgumentNullException("windowToMonitor", "Must specify a valid window handle to monitor");

      _window = windowToMonitor;

      //cast them now so we dont have to cast each one when evaluting events
      uint lowest = (uint) User32.AccessibleEventType.EVENT_MAX;
      uint highest = (uint) User32.AccessibleEventType.EVENT_MIN;
      foreach (User32.AccessibleEventType eventType in eventsToMonitor)
      {
        var castType = (uint) eventType;
        _watchedEvents.Add(castType);

        //need the range of events to subscribe to
        if (castType > highest)
          highest = castType;
        if (castType < lowest)
          lowest = castType;
      }

      //sign up for the event
      dEvent = this.winEvent;
      _hook = User32.SetWinEventHook(lowest, highest, IntPtr.Zero, dEvent, 0, 0, User32.WINEVENT_OUTOFCONTEXT);

      //ensure it worked
      if (IntPtr.Zero.Equals(_hook)) throw new Win32Exception();

      //no kill callback
      GC.KeepAlive(dEvent);
    }

    private void winEvent(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
    {
      //only care about the events for the specified window
      if (hWnd != _window)
        return;

      if (_watchedEvents.Contains(eventType))
      {
        if (EventOccurred != null)
          EventOccurred(this, new AccessibleEventTypeEventArgs((User32.AccessibleEventType) eventType));
      }
    }

    public void Dispose()
    {
      //unhook the listener
      if (!IntPtr.Zero.Equals(_hook))
        User32.UnhookWinEvent(_hook);

      //clear variables
      _hook = IntPtr.Zero;
      dEvent = null;

      //kill any event listeners
      EventOccurred = null;

      GC.SuppressFinalize(this);
    }
  }

和一些winapi(我省略了事件常量列表):

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

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

[DllImport("user32.dll")]
public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
4

1 回答 1

2

您可能会看到插入符号的事件。当你得到一个 winevent 时,你通常需要检查 HWNDidObject 以确保它实际上是一个关于你关心的窗口部分的事件;无论是窗口本身 (OBJID_WINDOW) 还是它的某些其他方面,例如滚动条、插入符号或其他内容。

要调试 wevents,请查看Accessible Event Watcher 工具- 将其配置为通过 Mode 菜单监听 WinEvents,然后在 Mode/Settings... 中选择您想要的事件 - 例如。EVENT_OBJECT_DESTROY,然后是要显示的事件信息,通常是hwnd、idObject和idChild。

--

顺便说一句,对您的代码有一条评论:如果您只是从特定窗口中查找事件,那么获取该窗口的线程(GetWindowThreadProcessId)会更有效,然后将 SetWinEventHook 与该 idThread 一起使用以仅从该特定线程获取事件传递 0 来监听所有线程;这允许 Win32 在事件源为您进行过滤,这比从整个桌面上的所有线程获取该类型的事件然后忽略您不需要的事件更有效。

于 2012-10-28T22:39:45.260 回答