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
The reason for this is called out in the documentation for
All we get from 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 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)
Comments are closed. |
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
Um, how about changing DFCS_MENUCHECK to DFCS_MENUARROW?
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);
To the previous poster, where did the value 0x00B8074A come from as its not any of the simple defines like SRCCOPY
mike: http://blogs.msdn.com/oldnewthing/archive/2005/05/24/421440.aspx
Two ways of doing it, who knows which is better.
mike:
http://kkow.net/etep/docs/glyph_blend.html
http://kkow.net/etep/docs/rop.html
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
hey, follow the link that says "start with a new scratch program", and all will be explained.
0x00B8074A is PSDPxax but a more useful way of looking at it is S?D:P
The text foreground and background colors play a role.