{"id":9243,"date":"2011-10-28T07:00:00","date_gmt":"2011-10-28T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/10\/28\/why-isnt-my-transparent-static-control-transparent\/"},"modified":"2011-10-28T07:00:00","modified_gmt":"2011-10-28T07:00:00","slug":"why-isnt-my-transparent-static-control-transparent","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20111028-00\/?p=9243","title":{"rendered":"Why isn&#039;t my transparent static control transparent?"},"content":{"rendered":"<p>\nA customer reported that their application uses transparent\nstatic controls positioned over a bitmap image control,\nbut even though they set the Transparent property on the static\ncontrol, the static control isn&#8217;t transparent.\nThe customer was kind enough to provide clear steps to illustrate\nthe problem:\n<\/p>\n<ul>\n<li>Open Visual Studio 2005 or 2008.\n<li>From the menu, select <i>File<\/i>,\n    <i>New File<\/i>, Visual C++<\/i>,\n    <i>Resource Template File (RCT)<\/i>.<\/p>\n<li>Right-click on the RCT file, select <i>Add Resource<\/i>,\n    and add a bitmap named <code>IDB_BITMAP1<\/code>.<\/p>\n<li>Open the dialog box (<code>IDD_DIALOG1<\/code>) and add a\n    &#8220;Picture Control&#8221;, specifying <code>IDC_BITMAP_1<\/code> as its ID.<\/p>\n<li>Change the <code>IDC_BITMAP_1<\/code> type to <i>Bitmap<\/i>\n    and change the value of the Image property to <code>IDB_BITMAP1<\/code>.<\/p>\n<li>Add a &#8220;Static Text&#8221; control <code>IDC_TEST_STATIC<\/code> and\n    set its caption to &#8220;This is a test&#8221;.<\/p>\n<li>Reposition the static control so it overlaps the\n    <code>IDC_BITMAP_1<\/code> control.<\/p>\n<li>On the <code>IDC_TEST_STATIC<\/code> control,\n    set the <i>Transparent<\/i> property to <i>True<\/i>.<\/p>\n<li>Type Ctrl+T to test the dialog and observe that the\n    static control is not transparent.\n<\/ul>\n<table BORDER=\"0\" CELLPADDING=\"0\" CELLSPACING=\"0\" STYLE=\"border-width: 2px;border-style: outset;width: 25em\">\n<tr STYLE=\"background: activecaption;color: captiontext;padding: 2px\">\n<td><b>Dialog<\/b><\/td>\n<td ALIGN=\"right\" VALIGN=\"middle\">\n    <button STYLE=\"height:1.5em\">\n        <font FACE=\"Marlett\"><!--[if IE]&gt;r&lt;![endif]-->&#xF072;<\/font>\n    <\/button><\/td>\n<\/tr>\n<tr STYLE=\"height: 10em;background: threedface\">\n<td VALIGN=\"top\" COLSPAN=\"2\">\n<div>\n<div STYLE=\"background: window;color: windowtext;height: 5em;width: 5em\">\n    <\/div>\n<div STYLE=\"background: threedface;color: buttontext\">This is a test<\/div>\n<\/div>\n<\/td>\n<\/table>\n<p>\nThe <i>Transparent<\/i> property in Visual Studio corresponds to\nthe <code>WS_EX_TRANSPARENT<\/code> window style,\nand\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/ff700543.aspx\">\nthe documentation explains<\/a>\nthat\n<\/p>\n<blockquote CLASS=\"q\"><p>\n<code>WS_EX_TRANSPARENT<\/code>:\nThe window should not be painted until siblings beneath the window\n(that were created by the same thread) have been painted.\nThe window appears transparent because\nthe bits of underlying sibling windows have already been painted.\n<\/p><\/blockquote>\n<p>\nThe observed behavior, therefore, matches the documentation:\nThe control underneath (the bitmap control) paints first,\nand then the static control paints on top of it.\nAnd a static text control paints by filling with the background brush\nand drawing the text on top of it.\nYou can customize this behavior by responding to the\n<code>WM_CTL&shy;COLOR&shy;STATIC<\/code> message:\n<\/p>\n<pre>\nHBRUSH CTestDlg::<a HREF=\"http:\/\/msdn.microsoft.com\/library\/0wwk06hc.aspx\">OnCtlColor<\/a>(CDC* pDC, CWnd* pWnd, UINT nCtlColor)\n{\n HBRUSH hbr = <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/11\/12\/55659.aspx\">__super<\/a>::OnCtlColor(pDC, pWnd, nCtlColor);\n if (pWnd-&gt;GetExStyle() &amp; WS_EX_TRANSPARENT) {\n  pDC-&gt;SetBkMode(TRANSPARENT);\n  hbr = GetStockBrush(<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/01\/26\/62991.aspx\">HOLLOW_BRUSH<\/a>);\n  \/\/ <!-- How do I get a radio button control to render its text transparently? -->even better would be to use a pattern brush, if the background is fixed<\/a>\n }\n return hbr;\n}\n<\/pre>\n<p>\nThe customer appreciated the explanation but was puzzled as to\nwhy the <i>Transparent<\/i> is available if it doesn&#8217;t work.\n&#8220;We understand that we can use the <code>WS_EX_TRANSPARENT<\/code>\nwindow style to create a transparent window and it will be painted\nafter its underlying siblings, but the window style by itself doesn&#8217;t\nmake the static control transparent.\nBut if we have to write the code above,\nwhy is the <i>Transparent<\/i> property available in the Properties box?&#8221;\nThey included a screen shot from Visual Studio where the built-in help\ntext for the <i>Transparent<\/i> property reads\n&#8220;Specifies that the control will have a transparent background.&#8221;\n<\/p>\n<p>\nThe <code>WS_EX_TRANSPARENT<\/code> style doesn&#8217;t mean\n&#8220;transparent&#8221;; it means &#8220;paint over siblings.&#8221;\nThe style is called &#8220;transparent&#8221; not because it makes\nthe window transparent but because it makes transparency <i>possible<\/i>.\nIt is one of the steps (but not the only one)\nfor making child controls render transparently.\nAnother important step is ensuring that the\ncontrol does not erase its background\nin its <code>WM_ERASE&shy;BKGND<\/code>,\nand that&#8217;s the step that the <code>On&shy;Ctl&shy;Color<\/code>\noverride performs.\n<\/p>\n<p>\nWhy is the <i>Transparent<\/i> property offered for static controls\nwhen it doesn&#8217;t actually work?\nShouldn&#8217;t it be disabled for static controls?\n<\/p>\n<p>\nThe reason why it is offered is that it is a general window style\nthat can be set on any control.\nVisual Studio doesn&#8217;t know which controls can render transparently\nand which ones don&#8217;t,\nor what extra steps are necessary to get\nthe ones who can render transparently to actually do so.\nIt just exposes the <code>WS_EX_TRANSPARENT<\/code> style and hopes\nthat you know what you&#8217;re doing.\n<\/p>\n<p>\nIn retrospect, it was a poor chose of name for the style.\nAnd the incorrect online help doesn&#8217;t make things any better.\n<\/p>\n<p>\n<b>Bonus chatter<\/b>:\nNote that the <code>WS_EX_TRANSPARENT<\/code>\nextended style is overloaded.\nIn addition to affecting painting,\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/12\/30\/10110077.aspx\">\nit also affects hit-testing<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A customer reported that their application uses transparent static controls positioned over a bitmap image control, but even though they set the Transparent property on the static control, the static control isn&#8217;t transparent. The customer was kind enough to provide clear steps to illustrate the problem: Open Visual Studio 2005 or 2008. From the menu, [&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-9243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>A customer reported that their application uses transparent static controls positioned over a bitmap image control, but even though they set the Transparent property on the static control, the static control isn&#8217;t transparent. The customer was kind enough to provide clear steps to illustrate the problem: Open Visual Studio 2005 or 2008. From the menu, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9243","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=9243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/9243\/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=9243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=9243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=9243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}