Using custom-draw in tooltips to adjust the font


Last time, we looked at in-place tooltips. In that example, we finessed the font problem by simply setting the destination font into the tooltip control. We got away with that since we had only one tool. But if you have multiple tools with different fonts, then you can’t set a font into the tooltip control and expect it to work for every tool. That’s where custom draw comes in.

Start with the program from last time, but this time, we’ll set the font via custom-draw instead of setting it globally.

BOOL
OnCreate(HWND hwnd, LPCREATESTRUCT lpcs)
{
 ...
 // SetWindowFont(g_hwndTT, g_hfTT, FALSE);
 ...
}

LRESULT
OnTooltipCustomDraw(HWND hwnd, NMHDR *pnm)
{
 LPNMTTCUSTOMDRAW pcd = (LPNMTTCUSTOMDRAW)pnm;
 if (pcd->nmcd.dwDrawStage == CDDS_PREPAINT) {
  SelectFont(pcd->nmcd.hdc, g_hfTT);
  return CDRF_NEWFONT;
 }
 return 0;
}

LRESULT
OnNotify(HWND hwnd, int idFrom, NMHDR *pnm)
{
 if (pnm->hwndFrom == g_hwndTT) {
  switch (pnm->code) {
  case NM_CUSTOMDRAW:
   return OnTooltipCustomDraw(hwnd, pnm);
  case TTN_SHOW:
   return OnTooltipShow(hwnd, pnm);
  }
 }
 return 0;
}

Of course, doing this is overkill in our case where we have only one tool, so you’ll have to imagine that the tooltip is managing multiple tool regions, each with a different font. When we get the NM_CUSTOMDRAW notification, we respond to the CDDS_PREPAINT stage by changing the font and returning the CDRF_NEWFONT flag (which is necessary when changing the font).

Comments (2)
  1. AC says:

    Thank you for publishing your code examples. They are really always very instructive.

    I tried them on W2K, and saw something that I can’t explain: even without TTS_NOANIMATE, sometimes the tooltip was drawn without animation and border (at least I believe it’s a tooltip since it’s yellow). And with that flag, it’s always without animation and border. But whenever it’s drawn without border, the window is clipped to the parent (when I resize the partent window).

    Is it possible to eliminate animation and still have tooltip that is not clipped? I hope it’s not a W2K specific problem. Thanks.

  2. Martin Filteau says:

    I found that using WS_EX_TOPMOST instead of WS_EX_TRANSPARENT gives a consistent, black border around the tooltip.  If I combine the two, the border disappears on W2K.  I don’t think WS_EX_TRANSPARENT is useful anyway since the tooltip already relay mouse messages to the parent’s window (as specified by TTF_TRANSPARENT).

Comments are closed.