February 18th, 2013

Display control buttons on your taskbar preview window

Today’s “Little Program” displays a button on the taskbar preview window. For now, the button increments a number, because incrementing numbers is so retro.

Welcome, visitor number  0 0 3 1 4

Start with the program from last week and make these changes:

int g_iCounter;

void
PaintContent(HWND hwnd, PAINTSTRUCT *pps)
{
  RECT rc;                                          
  GetClientRect(hwnd, &rc);                         
  LOGFONTW lf = { 0 };                              
  lf.lfHeight = rc.bottom - rc.top;                 
  wcscpy_s(lf.lfFaceName, L"Verdana");              
  HFONT hf = CreateFontIndirectW(&lf);              
  HFONT hfPrev = SelectFont(pps->hdc, hf);          
  wchar_t wszCount[80];                             
  swprintf_s(wszCount, L"%d", g_iCounter);          
  DrawTextW(pps->hdc, wszCount, -1, &rc,            
            DT_CENTER | DT_VCENTER | DT_SINGLELINE);
  SelectFont(pps->hdc, hfPrev);                     
  DeleteObject(hf);                                 
}

That’s an awful lot of typing just to print a big number on the screen.

#define IDC_INCREMENT 100

void CreateThumbBarButtons(HWND hwnd)
{
  THUMBBUTTON rgtb[1];
  rgtb[0].iId = IDC_INCREMENT;
  rgtb[0].hIcon = g_hicoAlert;
  rgtb[0].dwFlags = THBF_ENABLED;
  rgtb[0].dwMask = THB_ICON | THB_TOOLTIP | THB_FLAGS;
  wcscpy_s(rgtb[0].szTip, L"Increment the value");
  ITaskbarList3Ptr sptb3;
  sptb3.CreateInstance(CLSID_TaskbarList);
  sptb3->ThumbBarAddButtons(hwnd, 1, rgtb);
}

We define only one thumbbar button, and out of laziness, I just reuse that alert icon.

void OnCommand(HWND hwnd, int id, HWND hwndCtl, UINT codeNotify)
{
  switch (id) {
  case IDC_INCREMENT:
    ++g_iCounter;
    InvalidateRect(hwnd, nullptr, TRUE);
    break;
  }
}

When the button is pressed, we increment the counter and invalidate our window so we redraw with the new counter.

    // HANDLE_MSG(hwnd, WM_CHAR, OnChar);
    HANDLE_MSG(hwnd, WM_COMMAND, OnCommand);

    default:
      if (uiMsg != 0 && uiMsg == g_wmTaskbarButtonCreated) {
        CreateThumbBarButtons(hwnd);
      }
      break;

Okay, run the program, and then hover over the taskbar button so that the preview window appears. Hey, look, there’s an alert icon button under the thumbnail.

Click it.

Boom, the number increments.

That’s why I chose a huge font to draw the number: So it’s big enough that you can see the number in the thumbnail.

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.