Rendering menu glyphs is slightly trickier


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.

Skip to main content