Rendering menu glyphs is slightly trickier

Date:August 2, 2005 / year-entry #209
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050802-13/?p=34743
Comments:    11
Summary:Last time, we saw how to draw themed and unthemed radio buttons, and I mentioned that menu glyphs are trickier. They're trickier because they are provided as raw monochrome bitmaps instead of fully-formed color-coordinated bitmaps. First, let's do it wrong in order to see what we get. Then we'll try to fix it. Start with...

Last time, we saw how to draw themed and unthemed radio buttons, and I mentioned that menu glyphs are trickier. They're trickier because they are provided as raw monochrome bitmaps instead of fully-formed color-coordinated bitmaps. First, let's do it wrong in order to see what we get. Then we'll try to fix it. Start with a clean new scratch program

class RootWindow : public Window
{
 ...
protected:
 void PaintContent(PAINTSTRUCT *pps);
 BOOL WinRegisterClass(WNDCLASS *pwc)
 {
  pwc->hbrBackground = (HBRUSH)(COLOR_MENU + 1);
  return __super::WinRegisterClass(pwc);
 }
 ...
};

void RootWindow::PaintContent(PAINTSTRUCT *pps)
{
 int cxCheck = GetSystemMetrics(SM_CXMENUCHECK);
 int cyCheck = GetSystemMetrics(SM_CYMENUCHECK);
 RECT rc = { 0, 0, cxCheck, cyCheck };
 DrawFrameControl(pps->hdc, &rc, DFC_MENU, DFCS_MENUCHECK);
}

This naïvely uses the DrawFrameControl function to draw the menu check mark directly into the paint DC. If you are running with the default Windows XP theme you probably won't notice anything amiss, but switch to the Windows Classic theme and you'll see that the check mark is drawn in black and white even though the Classic menu background color is gray.

The reason for this is called out in the documentation for DrawFrameControl:

If uType is either DFC_MENU or DFC_BUTTON and uState is not DFCS_BUTTONPUSH, the frame control is a black-on-white mask (that is, a black frame control on a white background).

All we get from DrawFrameControl is a monochrome mask. It is our responsibility to colorize it as necessary. To do this, we draw the mask into a monochrome bitmap, and then use the BitBlt function to colorize it. Recall that when blitting from a monochrome bitmap to a color bitmap, the color black in the source bitmap becomes the destination DC's text color, and the color white in the source bitmap becomes the destination DC's background color.

void RootWindow::PaintContent(PAINTSTRUCT *pps)
{
 HDC hdcMem = CreateCompatibleDC(pps->hdc);
 if (hdcMem) {
  int cxCheck = GetSystemMetrics(SM_CXMENUCHECK);
  int cyCheck = GetSystemMetrics(SM_CYMENUCHECK);
  HBITMAP hbmMono = CreateBitmap(cxCheck, cyCheck, 1, 1, NULL);
  if (hbmMono) {
   HBITMAP hbmPrev = SelectBitmap(hdcMem, hbmMono);
   if (hbmPrev) {
    RECT rc = { 0, 0, cxCheck, cyCheck };
    DrawFrameControl(hdcMem, &rc, DFC_MENU, DFCS_MENUCHECK);
    COLORREF clrTextPrev = SetTextColor(pps->hdc,
                                     GetSysColor(COLOR_MENUTEXT));
    COLORREF clrBkPrev = SetBkColor(pps->hdc,
                                         GetSysColor(COLOR_MENU));
    BitBlt(pps->hdc, 0, 0, cxCheck, cyCheck,
           hdcMem, 0, 0, SRCCOPY);
    SetBkColor(pps->hdc, clrBkPrev);
    SetTextColor(pps->hdc, clrTextPrev);
    SelectBitmap(hdcMem, hbmPrev);
   }
   DeleteObject(hbmMono);
  }
  DeleteDC(hdcMem);
 }
}

The key steps here are (1) drawing into a temporary monochrome bitmap to generate the mask, (2) setting the text and background colors of the destination DC, (3) using BitBlt to do the color mapping. The rest of the function is just boring bookkeeping.

Observe that the checkmark's colors now match the system menu colors because we set them as the text and background colors for the mono-to-color blit.

Armed with this knowledge, perhaps you can help this person, who is trying to draw the menu check marks transparently. I can think of two different solutions off the top of my head.


Comments (11)
  1. Nimda says:

    Nice!

    Could you also explain how to draw submenu arrows? As far as I can see the font marlett is used for that purpose. But what size should I use for the marlet font? Let’s say the menu font is 8pt. Using a 8pt marlett font however results in a pretty much to small arrow.

    tia

  2. Um, how about changing DFCS_MENUCHECK to DFCS_MENUARROW?

  3. asdf says:

    The canonical way to make the bg transparent is to get rid of the text/bg color code and replace the BitBlt with:

    HBRUSH br = CreateSolidBrush(GetSysColor(COLOR_MENUTEXT));

    if (HBRUSH oldbr = SelectBrush(pps->hdc, br)) {

    BitBlt(pps->hdc, 0, 0, cxCheck, cyCheck, hdcMem, 0, 0, 0x00B8074A);

    SelectBrush(pps->hdc, oldbr);

    }

    DeleteObject(br);

  4. mike says:

    To the previous poster, where did the value 0x00B8074A come from as its not any of the simple defines like SRCCOPY

  5. Two ways of doing it, who knows which is better.

  6. hey says:

    Do you mind if I ask what you are programming in?

    It looks like C++ but stuff like

    "class RootWindow : public Window"

    tell me its not MFC since that has CWnd, etc.

    Thanks

  7. GregM says:

    hey, follow the link that says "start with a new scratch program", and all will be explained.

  8. Neil says:

    0x00B8074A is PSDPxax but a more useful way of looking at it is S?D:P

  9. The text foreground and background colors play a role.

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