{"id":64,"date":"2009-06-09T11:53:13","date_gmt":"2009-06-09T19:53:13","guid":{"rendered":"http:\/\/blog.gerrior.com\/?p=64"},"modified":"2011-11-04T09:56:52","modified_gmt":"2011-11-04T17:56:52","slug":"changing-background-color-in-a-cstatic-object","status":"publish","type":"post","link":"https:\/\/blog.gerrior.com\/?p=64","title":{"rendered":"Changing Background Color in a CStatic Object"},"content":{"rendered":"<p style=\"text-align: left;\">Here&#8217;s a classic example of a seemingly easy problem actually being harder to implement. \u00a0I wanted a dialog with white static controls in MFC. \u00a0The resulting code is simple but divining the answer was no easy feat. \u00a0Ultimately it&#8217;s more a failure of documentation that it is a engineering\u00a0challenge. \u00a0More details in my commented code which follows:<\/p>\n<div class=\"mceTemp\" style=\"text-align: left;\">\n<dl id=\"attachment_66\" class=\"wp-caption alignright\" style=\"width: 449px;\">\n<dt class=\"wp-caption-dt\"><a href=\"http:\/\/blog.gerrior.com\/wp-content\/uploads\/2009\/06\/background.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-66\" title=\"Before and After\" src=\"http:\/\/blog.gerrior.com\/wp-content\/uploads\/2009\/06\/background.png\" alt=\"Before and After\" width=\"439\" height=\"199\" srcset=\"https:\/\/blog.gerrior.com\/wp-content\/uploads\/2009\/06\/background.png 439w, https:\/\/blog.gerrior.com\/wp-content\/uploads\/2009\/06\/background-300x135.png 300w\" sizes=\"auto, (max-width: 439px) 100vw, 439px\" \/><\/a><\/dt>\n<dd class=\"wp-caption-dd\">Before and After<\/dd>\n<\/dl>\n<\/div>\n<p style=\"text-align: left;\"><code><br \/>\nHBRUSH TReaderDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)<br \/>\n{<br \/>\nHBRUSH hbr = __super::OnCtlColor(pDC, pWnd, nCtlColor);<\/code><\/p>\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\"><code><br \/>\n\/\/ For static controls, draw black text on a white<br \/>\n\/\/ background (instead of the default Windows<br \/>\n\/\/ dialog background color)<br \/>\nif (nCtlColor == CTLCOLOR_STATIC)<br \/>\n{<br \/>\n\/\/ Set the background color to white<br \/>\n\/\/ This is a red herring. You will only affect the text<br \/>\n\/\/ drawn and the static control is certainly larger than<br \/>\n\/\/ the text drawn so you will have the original color<br \/>\n\/\/ \"leaking\" out. (see pic)<br \/>\n\/\/pDC-&gt;SetBkColor(RGB(255, 255, 255));<\/code><\/p>\n<p style=\"text-align: left;\"><code> <\/code><\/p>\n<p style=\"text-align: left;\"><code><br \/>\n\/\/ Set the background mode for the drawn text to<br \/>\n\/\/ transparent<br \/>\n\/\/   If you use SetBkColor with SetBkMode you will further<br \/>\n\/\/ compound the confusion\/frustrtion.  Using<br \/>\n\/\/ SetBkMode(TRANSPARENT) after SetBkColor cancels<br \/>\n\/\/ out SetBkColor.<br \/>\npDC-&gt;SetBkMode(TRANSPARENT);<\/code><\/p>\n<p><code><br \/>\n\/\/ The real solution is to create a brush with a solid<br \/>\n\/\/ white background and return that from this function.<br \/>\n\/\/ Also set the background mode to transparent.<\/code><\/p>\n<p><code>static CBrush mBrush(RGB(255, 255, 255)); \/\/ Solid white brush.<\/code><\/p>\n<p><code>hbr = mBrush;<br \/>\n}<\/code><\/p>\n<p><code> <\/code><\/p>\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\">\n<p style=\"text-align: left;\"><code><br \/>\n\/\/ Return a different brush if the default is<br \/>\n\/\/ not desired<br \/>\nreturn hbr;<br \/>\n}<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a classic example of a seemingly easy problem actually being harder to implement. \u00a0I wanted a dialog with white static controls in MFC. \u00a0The resulting code is simple but divining the answer was no easy feat. \u00a0Ultimately it&#8217;s more a failure of documentation that it is a engineering\u00a0challenge. \u00a0More details in my commented code &hellip; <a href=\"https:\/\/blog.gerrior.com\/?p=64\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Changing Background Color in a CStatic Object<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,6],"tags":[],"class_list":["post-64","post","type-post","status-publish","format-standard","hentry","category-mfc","category-windows"],"_links":{"self":[{"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/posts\/64","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=64"}],"version-history":[{"count":13,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=\/wp\/v2\/posts\/64\/revisions\/405"}],"wp:attachment":[{"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=64"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=64"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gerrior.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=64"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}