{"id":13593,"date":"2010-06-28T07:00:00","date_gmt":"2010-06-28T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/06\/28\/how-do-i-get-a-radio-button-control-to-render-its-text-transparently\/"},"modified":"2010-06-28T07:00:00","modified_gmt":"2010-06-28T07:00:00","slug":"how-do-i-get-a-radio-button-control-to-render-its-text-transparently","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20100628-00\/?p=13593","title":{"rendered":"How do I get a radio button control to render its text transparently?"},"content":{"rendered":"<p>\nCommenter\n<a HREF=\"http:\/\/www.hardcode.ro\">Andrei<\/a>\nasks via the Suggestion Box\nfor help with\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2007\/05\/23\/407234.aspx#1520893\">\nmaking the text transparent using\n<code>WM_CTL&shy;COLOR&shy;STATIC<\/code><\/a>.\n&#8220;Instead of the radio button now there&#8217;s a black background.&#8221;\n<\/p>\n<p>\nLet&#8217;s look at this problem in stages.\nFirst, let&#8217;s ignore the transparent part and figure out\nhow to render text without a black background.\nThe background color of the text comes from the color you\nselected into the DC when handling the\n<code>WM_CTL&shy;COLOR&shy;STATIC<\/code>\nmessage.\nAnd if you forget to set a background color,\nthen you get whatever color is lying around in the DC,\nwhich might very well be black.\nStart with\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nthe scratch program<\/a>\nand make these changes, which I&#8217;m going to write in the way\nI think Andrei wrote it,\neven though it doesn&#8217;t fit the style of the rest of the\nscratch program.\n<\/p>\n<pre>\n<font COLOR=\"blue\">HBRUSH g_hbr;<\/font>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n<font COLOR=\"blue\">    g_hwndChild = CreateWindow(TEXT(\"button\"), TEXT(\"Bingo\"),\n        WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,\n        0, 0, 0, 0, hwnd, (HMENU)1, g_hinst, 0);\n    g_hbr = CreateSolidBrush(RGB(0xFF, 0x00, 0xFF)); \/\/ hot pink<\/font>\n    return TRUE;\n}\nvoid\nOnDestroy(HWND hwnd)\n{\n    <font COLOR=\"blue\">if (g_hbr) DeleteObject(g_hbr);<\/font>\n    PostQuitMessage(0);\n}\n\/\/ add to WndProc\n<font COLOR=\"blue\">  case WM_CTLCOLORSTATIC:\n    if (GetDlgCtrlID(\n             GET_WM_CTLCOLOR_HWND(wParam, lParam, uiMsg)) == 1) {\n      return (LRESULT)g_hbr; \/\/ override default background color\n    }\n    break;<\/font>\n<\/pre>\n<p>\nIf you run this program, the radio button&#8217;s background is indeed\nhot pink, well except for the text, where the color is,\nI dunno, it&#8217;s white on my machine, but who knows what it is on yours.\nSince we didn&#8217;t specify a color, the result is undefined.\nThe bug here is that we handled the\n<code>WM_CTL&shy;COLOR&shy;STATIC<\/code>\nmessage incompletely.\nThe <code>WM_CTL&shy;COLOR<\/code> family of messages requires that\nthe message handler do three things:\n<\/p>\n<ol>\n<li>Set the DC text color.\n<\/li>\n<li>Set the DC background color.\n<\/li>\n<li>Return a background brush.\n<\/li>\n<\/ol>\n<p>\nWe got so excited about the background brush that we forgot the other\ntwo steps. Let&#8217;s fix that.\n<\/p>\n<pre>\ncase WM_CTLCOLORSTATIC:\n    if (GetDlgCtrlID(\n             GET_WM_CTLCOLOR_HWND(wParam, lParam, uiMsg)) == 1) {\n      <font COLOR=\"blue\">HDC hdc = GET_WM_CTLCOLOR_HDC(wParam, lParam, uiMsg);\n      SetTextColor(hdc, RGB(0xFF, 0xFF, 0x00)); \/\/ yellow\n      SetBkColor(hdc, RGB(0xFF, 0x00, 0xFF)); \/\/ hot pink<\/font>\n      return (LRESULT)g_hbr; \/\/ override default background color\n    }\n    break;\n<\/pre>\n<p>\n(Just for fun, I chose yellow as the text color.)\nNow that we specified the text color and the background color,\nthe text appears in the correct colors.\n<\/p>\n<p>\nNote that we didn&#8217;t actually do anything transparently here.\nWe just made sure that the background color we told the control\nto use for text matches the color we told the control\nto use for erasing the background.\nThe effect looks transparent since the two colors match.\n<\/p>\n<p>\nBut what if you really wanted transparency instead of fake\ntransparency?\nTo illustrate, let&#8217;s give the control a background that is not\na solid color:\n<\/p>\n<pre>\nBOOL\nOnCreate(HWND hwnd, LPCREATESTRUCT lpcs)\n{\n    g_hwndChild = CreateWindow(TEXT(\"button\"), TEXT(\"Bingo\"),\n        WS_CHILD | WS_VISIBLE | BS_RADIOBUTTON,\n        0, 0, 0, 0, hwnd, (HMENU)1, g_hinst, 0);\n    <font COLOR=\"blue\">g_hbr = <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/10\/09\/55243.aspx\">CreatePatternBrushFromFile<\/a>(\n                          TEXT(\"C:\\\\Windows\\\\Gone Fishing.bmp\"));<\/font>\n    return TRUE;\n}\n<\/pre>\n<p>\nWhen you run this version of the program, the radio button background\nconsists of the Gone Fishing bitmap.\n(Of course, if you don&#8217;t have that bitmap, then feel free to substitute\nanother bitmap.\nI can&#8217;t believe I had to write that.)\nBut the text is still yellow on pink.\nHow do we get it to be yellow on the complex background?\n<\/p>\n<p>\nBy setting the background mix mode to <code>TRANSPARENT<\/code>.\n<\/p>\n<pre>\ncase WM_CTLCOLORSTATIC:\n    if (GetDlgCtrlID(\n             GET_WM_CTLCOLOR_HWND(wParam, lParam, uiMsg)) == 1) {\n      HDC hdc = GET_WM_CTLCOLOR_HDC(wParam, lParam, uiMsg);\n      SetTextColor(hdc, RGB(0xFF, 0xFF, 0x00)); \/\/ yellow\n      SetBkColor(hdc, RGB(0xFF, 0x00, 0xFF)); \/\/ hot pink\n      <font COLOR=\"blue\">SetBkMode(hdc, TRANSPARENT);<\/font>\n      return (LRESULT)g_hbr; \/\/ override default background color\n    }\n    break;\n<\/pre>\n<p>\nAccording to the documentation, the background mix mode\n&#8220;is used with text, hatched brushes,\nand pen styles that are not solid lines.&#8221;\nIt&#8217;s the text part we care about here.\nWhen the control does its <code>Text&shy;Out<\/code>\nto draw the control text, the background mix mode\ncauses the text to be rendered transparently.\n<\/p>\n<p>\n<b>Exercise<\/b>:\nThere&#8217;s actually one more thing you need to do,\nbut I conveniently arranged the program so you didn&#8217;t notice.\nWhat other step did I forget?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commenter Andrei asks via the Suggestion Box for help with making the text transparent using WM_CTL&shy;COLOR&shy;STATIC. &#8220;Instead of the radio button now there&#8217;s a black background.&#8221; Let&#8217;s look at this problem in stages. First, let&#8217;s ignore the transparent part and figure out how to render text without a black background. The background color of the [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-13593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Commenter Andrei asks via the Suggestion Box for help with making the text transparent using WM_CTL&shy;COLOR&shy;STATIC. &#8220;Instead of the radio button now there&#8217;s a black background.&#8221; Let&#8217;s look at this problem in stages. First, let&#8217;s ignore the transparent part and figure out how to render text without a black background. The background color of the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13593","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=13593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/13593\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=13593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=13593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=13593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}