Using custom-draw in tooltips to adjust the font

Date:June 27, 2006 / year-entry #214
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20060627-22/?p=30723
Comments:    2
Summary: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...

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.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index