{"id":5223,"date":"2013-02-18T07:00:00","date_gmt":"2013-02-18T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/02\/18\/display-control-buttons-on-your-taskbar-preview-window\/"},"modified":"2023-12-05T10:20:02","modified_gmt":"2023-12-05T18:20:02","slug":"20130218-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130218-00\/?p=5223","title":{"rendered":"Display control buttons on your taskbar preview window"},"content":{"rendered":"<p>Today&#8217;s &#8220;Little Program&#8221; displays a button on the taskbar preview window. For now, the button increments a number, because incrementing numbers is so retro.<\/p>\n<table style=\"border-collapse: collapse; font-family: monospace;\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td style=\"font-family: fantasy;\">Welcome, visitor number\u00a0<\/td>\n<td id=\"p20130218_counter0\">0<\/td>\n<td id=\"p20130218_counter1\">0<\/td>\n<td id=\"p20130218_counter2\">3<\/td>\n<td id=\"p20130218_counter3\">1<\/td>\n<td id=\"p20130218_counter4\">4<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\n<script>\nvar count = ((Date.now()\/1000 - 25200) % 86400 + 100000) + \"\";\ndocument.getElementById(\"p20130218_counter0\").innerHTML = count.charAt(1);\ndocument.getElementById(\"p20130218_counter1\").innerHTML = count.charAt(2);\ndocument.getElementById(\"p20130218_counter2\").innerHTML = count.charAt(3);\ndocument.getElementById(\"p20130218_counter3\").innerHTML = count.charAt(4);\ndocument.getElementById(\"p20130218_counter4\").innerHTML = count.charAt(5);\n<\/script>\n<\/p>\n<p>Start with the <a title=\"Display an overlay on the taskbar button\" href=\"https:\/\/devblogs.microsoft.com\/oldnewthing\/20130211-00\/?p=5283\"> program from last week<\/a> and make these changes:<\/p>\n<pre><span style=\"border: solid 1px currentcolor;\">int g_iCounter;<\/span>\r\n\r\nvoid\r\nPaintContent(HWND hwnd, PAINTSTRUCT *pps)\r\n{\r\n  <span style=\"border: solid 1px currentcolor; border-bottom: none;\">RECT rc;                                          <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">GetClientRect(hwnd, &amp;rc);                         <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">LOGFONTW lf = { 0 };                              <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">lf.lfHeight = rc.bottom - rc.top;                 <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">wcscpy_s(lf.lfFaceName, L\"Verdana\");              <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">HFONT hf = CreateFontIndirectW(&amp;lf);              <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">HFONT hfPrev = SelectFont(pps-&gt;hdc, hf);          <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">wchar_t wszCount[80];                             <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">swprintf_s(wszCount, L\"%d\", g_iCounter);          <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">DrawTextW(pps-&gt;hdc, wszCount, -1, &amp;rc,            <\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">          DT_CENTER | DT_VCENTER | DT_SINGLELINE);<\/span>\r\n  <span style=\"border: 1px currentcolor; border-style: none solid;\">SelectFont(pps-&gt;hdc, hfPrev);                     <\/span>\r\n  <span style=\"border: solid 1px currentcolor; border-top: none;\">DeleteObject(hf);                                 <\/span>\r\n}\r\n<\/pre>\n<p>That&#8217;s an awful lot of typing just to print a big number on the screen.<\/p>\n<pre>#define IDC_INCREMENT 100\r\n\r\nvoid CreateThumbBarButtons(HWND hwnd)\r\n{\r\n  THUMBBUTTON rgtb[1];\r\n  rgtb[0].iId = IDC_INCREMENT;\r\n  rgtb[0].hIcon = g_hicoAlert;\r\n  rgtb[0].dwFlags = THBF_ENABLED;\r\n  rgtb[0].dwMask = THB_ICON | THB_TOOLTIP | THB_FLAGS;\r\n  wcscpy_s(rgtb[0].szTip, L\"Increment the value\");\r\n  ITaskbarList3Ptr sptb3;\r\n  sptb3.CreateInstance(CLSID_TaskbarList);\r\n  sptb3-&gt;ThumbBarAddButtons(hwnd, 1, rgtb);\r\n}\r\n<\/pre>\n<p>We define only one thumbbar button, and out of laziness, I just reuse that alert icon.<\/p>\n<pre>void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)\r\n{\r\n  switch (id) {\r\n  case IDC_INCREMENT:\r\n    ++g_iCounter;\r\n    InvalidateRect(hwnd, nullptr, TRUE);\r\n    break;\r\n  }\r\n}\r\n<\/pre>\n<p>When the button is pressed, we increment the counter and invalidate our window so we redraw with the new counter.<\/p>\n<pre>    \/\/ <span style=\"border: dashed 1px currentcolor;\"><span style=\"text-decoration: line-through;\">HANDLE_MSG(hwnd, WM_CHAR, OnChar);<\/span><\/span>\r\n    <span style=\"border: solid 1px currentcolor;\">HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);<\/span>\r\n\r\n    default:\r\n      if (uiMsg != 0 &amp;&amp; uiMsg == g_wmTaskbarButtonCreated) {\r\n        <span style=\"border: solid 1px currentcolor;\">CreateThumbBarButtons<\/span>(hwnd);\r\n      }\r\n      break;\r\n<\/pre>\n<p>Okay, run the program, and then hover over the taskbar button so that the preview window appears. Hey, look, there&#8217;s an alert icon button under the thumbnail.<\/p>\n<p>Click it.<\/p>\n<p>Boom, the number increments.<\/p>\n<p>That&#8217;s why I chose a huge font to draw the number: So it&#8217;s big enough that you can see the number in the thumbnail.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A tiny control surface.<\/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-5223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A tiny control surface.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5223","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=5223"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/5223\/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=5223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=5223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=5223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}