{"id":98895,"date":"2018-06-01T07:00:00","date_gmt":"2018-06-01T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=98895"},"modified":"2019-03-13T00:41:28","modified_gmt":"2019-03-13T07:41:28","slug":"20180601-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20180601-00\/?p=98895","title":{"rendered":"How can I write a program that monitors another window for a title change?"},"content":{"rendered":"<p>A customer was writing a monitoring application and wanted to be notified if a window&#8217;s title changes. <\/p>\n<p>Sure, we can use accessibility to do that. <\/p>\n<pre>\n#define UNICODE\n#define <a HREF=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/\">_UNICODE<\/a>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;stdio.h&gt;\n\nHWND g_hwndMonitor;\n\nvoid CALLBACK WinEventProc(\n  HWINEVENTHOOK hook,\n  DWORD event,\n  HWND hwnd,\n  LONG idObject,\n  LONG idChild,\n  DWORD idEventThread,\n  DWORD time)\n{\n  if (hwnd == g_hwndMonitor &amp;&amp;\n      idObject == OBJID_WINDOW &amp;&amp;\n      idChild == CHILDID_SELF &amp;&amp;\n      event == EVENT_OBJECT_NAMECHANGE) {\n      printf(\"title changed\\n\");\n  }\n}\n\nint __cdecl main(int, char**)\n{\n g_hwndMonitor = FindWindow(L\"Awesome Program\", nullptr);\n DWORD processId;\n DWORD threadId = GetWindowThreadProcessId(g_hwndMonitor, &amp;processId);\n HWINEVENTHOOK hook = SetWinEventHook(\n    EVENT_OBJECT_NAMECHANGE,\n    EVENT_OBJECT_NAMECHANGE,\n    nullptr,\n    WinEventProc,\n    processId,\n    threadId,\n    WINEVENT_OUTOFCONTEXT);\n MessageBox(nullptr, L\"Press OK when bored\", L\"Title\", MB_OK);\n\n UnhookWinEvent(hook);\n return 0;\n}\n<\/pre>\n<p>The program starts by identifying the window it wants to monitor. Presumably the customer will use some domain-specific knowledge to find the window, but here, we&#8217;ll just demonstrate with the <code>Find<\/code><code>Window<\/code> function. <\/p>\n<p>We get the thread and process ID for the window and use it to register a thread-specific accessibility event hook, filtered to name changes. <\/p>\n<p>In the event callback, we see if the notification is for the window we are monitoring. If so, we print a message. The customer&#8217;s program would presumably do something more interesting than just print a message. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Accessibility saves the day once again.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-98895","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Accessibility saves the day once again.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98895","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=98895"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/98895\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=98895"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=98895"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=98895"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}