窗口标题更改事件 [英] Window Title Changed Event

查看:232
本文介绍了窗口标题更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个窗口中,用于例如火狐,改变它的标题,从火狐堆栈溢出 - 火狐然后我想我的应用程序来记录火狐改变了称号。

If a window, for e.g. Firefox, changes its title, from Firefox to Stack Overflow - Firefox then I want my app to record that Firefox changed its title.

这是可能的,而无需使用一个钩和环(EnumWindows的)?如果可以用一个钩子,其中钩子类型?

Is this possible without the use of a hook and loop (EnumWindows)? If can be only done with a hook, which type of hook?

推荐答案

WinEvents是要走的路在这里。你需要的API是 SetWinEventHook() - 如果你关心一个特定的窗口,使用GetWindowThreadProcessId()来获取HWND的threadId的再听听只能从特定线程的事件。对于窗口标题的变化,你会希望EVENT_OBJECT_NAMECHANGE事件。

WinEvents is the way to go here. The API you need is SetWinEventHook() - if you care about a specific window, use GetWindowThreadProcessId() to get the HWND's threadId and then listen to events only from that specific thread. For window title changes, you'll want the EVENT_OBJECT_NAMECHANGE event.

您可以上下文勾或出的背景下 - 后者是最简单的,并表示该事件被传递回自己的过程,所以你并不需要一个单独的DLL - 这使得它可以做这一切在C#;但是调用SetWinEventHook线程必须有一个消息循环(的GetMessage /的TranslateMessage / DispatchMessage函数)中,由于事件是使用幕后消息的形式传递。

You can hook either "in context" or "out of context" - the latter is the simplest, and means the event gets delivered back to your own process, so you don't need a separate DLL - which makes it possible to do it all in C#; but the thread that calls SetWinEventHook must have a message loop (GetMessage/TranslateMessage/DispatchMessage), since the events are delivered using a form of messages behind the scenes.

在你WinEvent回调,则需要检查HWND是你关心的之一,因为你会得到的名称更改为任何用户界面上目标线程,其中可能包括子窗口名称更改,或其他的东西你不关心。

In your WinEvent callback, you'll need to check that the HWND is the one you care about, since you'll get name changes for any UI on that target thread, possibly including child window name changes, or other things you don't care about.

-

顺便说一句,您可以检查这个答案为使用WinEvents一些示例C#code;它使用它们来跟踪整个桌面上的所有窗口前台窗口的变化;但应该只是采取上述几个简单的修改来跟踪特定窗口名称的变化。

By the way, you can check this answer for some sample C# code that uses WinEvents; it's using them to track foreground window changes across all windows on the desktop; but should just take a few simple modifications outlined above to track name changes on a specific window.

这篇关于窗口标题更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆