{"id":3083,"date":"2013-09-30T07:00:00","date_gmt":"2013-09-30T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/09\/30\/playing-a-sound-every-time-the-foreground-window-changes\/"},"modified":"2013-09-30T07:00:00","modified_gmt":"2013-09-30T07:00:00","slug":"playing-a-sound-every-time-the-foreground-window-changes","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130930-00\/?p=3083","title":{"rendered":"Playing a sound every time the foreground window changes"},"content":{"rendered":"<p>\nToday&#8217;s Little Program plays a little sound every time the foreground\nwindow changes.\nOne of my colleagues wondered if such a program was possible,\n&#8220;so that I stop accidentally typing the second halves of paragraphs\ninto windows that pop up and steal focus.&#8221;\nIt&#8217;s not clear whether this program will actually solve the bigger\nproblem,\nbut it was fun writing the program,\nand maybe you can use it for something.\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;mmsystem.h&gt;\nvoid CALLBACK WinEventProc(\n    HWINEVENTHOOK hWinEventHook,\n    DWORD event,\n    HWND hwnd,\n    LONG idObject,\n    LONG idChild,\n    DWORD dwEventThread,\n    DWORD dwmsEventTime\n)\n{\n  if (hwnd &amp;&amp;\n      idObject == OBJID_WINDOW &amp;&amp;\n      idChild == CHILDID_SELF &amp;&amp;\n      event == EVENT_SYSTEM_FOREGROUND) {\n   PlaySound(TEXT(\"C:\\\\Windows\\\\Media\\\\Speech Misrecognition.wav\"),\n             NULL, SND_FILENAME | SND_ASYNC);\n }\n}\nint WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev,\n                   LPSTR lpCmdLine, int nShowCmd)\n{\n  HWINEVENTHOOK hWinEventHook = SetWinEventHook(\n     EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND,\n     NULL, WinEventProc, 0, 0,\n     WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);\n  MSG msg;\n  while (GetMessage(&amp;msg, NULL, 0, 0)) {\n   TranslateMessage(&amp;msg);\n   DispatchMessage(&amp;msg);\n  }\n  if (hWinEventHook) UnhookWinEvent(hWinEventHook);\n  return 0;\n}\n<\/pre>\n<p>\nThis program installs an accessibility hook that\nlistens for changes to the system foreground.\nAnd when it happens, we play a little sound.\n<\/p>\n<p>\nI chose the Windows&nbsp;7 Speech Misrecognition sound\nbecause it&#8217;s relatively unobtrusive.\nAnd the sound is played asynchronously so as not to\nblock the message pump thread.\nIt also has as a pleasant side-effect that if\nthe foreground changes many times rapidly,\nthe new sound will interrupt the old one rather than\nqueueing up behind it.\n<\/p>\n<p>\nNote that there is no way to exit this program short of\nkilling it in Task Manager.\nThat&#8217;s why it&#8217;s a Little Program rather than a real program.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program plays a little sound every time the foreground window changes. One of my colleagues wondered if such a program was possible, &#8220;so that I stop accidentally typing the second halves of paragraphs into windows that pop up and steal focus.&#8221; It&#8217;s not clear whether this program will actually solve the bigger problem, [&hellip;]<\/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-3083","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Today&#8217;s Little Program plays a little sound every time the foreground window changes. One of my colleagues wondered if such a program was possible, &#8220;so that I stop accidentally typing the second halves of paragraphs into windows that pop up and steal focus.&#8221; It&#8217;s not clear whether this program will actually solve the bigger problem, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3083","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=3083"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/3083\/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=3083"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=3083"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=3083"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}