{"id":683,"date":"2014-06-23T07:00:00","date_gmt":"2014-06-23T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/06\/23\/adding-a-sound-to-the-alttab-window\/"},"modified":"2014-06-23T07:00:00","modified_gmt":"2014-06-23T07:00:00","slug":"adding-a-sound-to-the-alttab-window","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140623-00\/?p=683","title":{"rendered":"Adding a sound to the Alt+Tab window"},"content":{"rendered":"<p>\nToday&#8217;s Little Program plays a sound when the\n<kbd>Alt<\/kbd>+<kbd>Tab<\/kbd> window appears and disappears.\n<\/p>\n<pre>\n#define STRICT\n#include &lt;windows.h&gt;\n#include &lt;mmsystem.h&gt;\nHWND g_hwndAltTab = nullptr;\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 PCTSTR pszSound = nullptr;\n switch (event) {\n case EVENT_SYSTEM_SWITCHSTART:\n  if (!g_hwndAltTab) {\n   g_hwndAltTab = hwnd;\n   pszSound = \"C:\\\\Windows\\\\Media\\\\Speech on.wav\";\n  }\n  break;\n case EVENT_SYSTEM_SWITCHEND:\n  if (g_hwndAltTab) {\n   g_hwndAltTab = nullptr;\n   pszSound = \"C:\\\\Windows\\\\Media\\\\Speech sleep.wav\";\n  }\n  break;\n }\n if (pszSound) {\n  PlaySound(pszSound, nullptr, 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_SWITCHSTART, EVENT_SYSTEM_SWITCHEND,\n     nullptr, WinEventProc, 0, 0,\n     WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);\n if (hWinEventHook) {\n  MessageBox(nullptr, \"Close this window when sufficiently annoyed.\",\n             \"Hello\", MB_OK);\n  UnhookWinEvent(hWinEventHook);\n }\n return 0;\n}\n<\/pre>\n<p>\nThe program registers an accessibility event hook for the\n<code>EVENT_SYSTEM_SWITCH&shy;START<\/code> and\n<code>EVENT_SYSTEM_SWITCH&shy;END<\/code> events.\nThe Start event fires when an <kbd>Alt<\/kbd>+<kbd>Tab<\/kbd> operation\nbegins, and the\nEnd event fires when an <kbd>Alt<\/kbd>+<kbd>Tab<\/kbd> operation\ncompletes.\nAs noted in the documentation,\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/ms697187\">\nyou can get spurious End events<\/a>,\nso we keep track of our current state to avoid\nany surprises.\n<\/p>\n<p>\nIn addition to adding an annoying sound to the\n<kbd>Alt<\/kbd>+<kbd>Tab<\/kbd> window,\nlet&#8217;s also add an annoying sound each time you move focus\nto a new item.\n<\/p>\n<pre>\n<font COLOR=\"blue\">HWINEVENT g_hWinEventHookFocus = nullptr;<\/font>\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 PCTSTR pszSound = nullptr;\n switch (event) {\n case EVENT_SYSTEM_SWITCHSTART:\n  if (!g_hwndAltTab) {\n   g_hwndAltTab = hwnd;\n   <font COLOR=\"blue\">g_hWinEventHookFocus = SetWinEventHook(\n     EVENT_OBJECT_FOCUS, EVENT_OBJECT_FOCUS,\n     NULL, WinEventProc, 0, 0,\n     WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);<\/font>\n   pszSound = \"C:\\\\Windows\\\\Media\\\\Speech on.wav\";\n  }\n  break;\n case EVENT_SYSTEM_SWITCHEND:\n  if (g_hwndAltTab) {\n   g_hwndAltTab = nullptr;\n   <font COLOR=\"blue\">UnhookWinEvent(g_hWinEventHookFocus);\n   g_hWinEventHookFocus = nullptr;<\/font>\n   pszSound = \"C:\\\\Windows\\\\Media\\\\Speech sleep.wav\";\n  }\n  break;\n <font COLOR=\"blue\">case EVENT_OBJECT_FOCUS:\n  if (hwnd == g_hwndAltTab) {\n   pszSound = TEXT(\"C:\\\\Windows\\\\Media\\\\Speech Misrecognition.wav\");\n  }\n  break;<\/font>\n }\n if (pszSound) {\n  PlaySound(pszSound, nullptr, 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_SWITCHSTART, EVENT_SYSTEM_SWITCHEND,\n     nullptr, WinEventProc, 0, 0,\n     WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);\n if (hWinEventHook) {\n  MessageBox(nullptr, \"Close this window when sufficiently annoyed.\",\n             \"Hello\", MB_OK);\n  UnhookWinEvent(hWinEventHook);\n  <font COLOR=\"blue\">if (g_hWinEventHookFocus) {\n   UnhookWinEvent(g_hWinEventHookSelect);\n  }<\/font>\n }\n return 0;\n}\n<\/pre>\n<p>\nOkay, this was a pretty annoying program,\nbut maybe you can use it for something better.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today&#8217;s Little Program plays a sound when the Alt+Tab window appears and disappears. #define STRICT #include &lt;windows.h&gt; #include &lt;mmsystem.h&gt; HWND g_hwndAltTab = nullptr; void CALLBACK WinEventProc( HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime ) { PCTSTR pszSound = nullptr; switch (event) { case EVENT_SYSTEM_SWITCHSTART: if (!g_hwndAltTab) { g_hwndAltTab [&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-683","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 sound when the Alt+Tab window appears and disappears. #define STRICT #include &lt;windows.h&gt; #include &lt;mmsystem.h&gt; HWND g_hwndAltTab = nullptr; void CALLBACK WinEventProc( HWINEVENTHOOK hWinEventHook, DWORD event, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime ) { PCTSTR pszSound = nullptr; switch (event) { case EVENT_SYSTEM_SWITCHSTART: if (!g_hwndAltTab) { g_hwndAltTab [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/683","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=683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/683\/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=683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}