{"id":28893,"date":"2006-11-28T10:00:06","date_gmt":"2006-11-28T10:00:06","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2006\/11\/28\/what-went-wrong-in-windows-95-if-you-use-a-system-color-brush-as-your-background-brush\/"},"modified":"2006-11-28T10:00:06","modified_gmt":"2006-11-28T10:00:06","slug":"what-went-wrong-in-windows-95-if-you-use-a-system-color-brush-as-your-background-brush","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20061128-06\/?p=28893","title":{"rendered":"What went wrong in Windows 95 if you use a system color brush as your background brush?"},"content":{"rendered":"<p>\nIf you want to register a window class and use a system color\nas its background color, you set the <code>hbrBackground<\/code> member\nto the desired color, plus one, cast to an <code>HBRUSH<\/code>:\n<\/p>\n<pre>\nwc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\n<\/pre>\n<p>\nWindows&nbsp;95 introduced &#8220;system color brushes&#8221;,\nwhich are a magic type of brush which always paint in the\ncorresponding system color, even if the system color changes.\n<\/p>\n<pre>\nHBRUSH hbrWindow = GetSysColorBrush(COLOR_WINDOW);\n<\/pre>\n<p>\nThe <code>hbrWindow<\/code> brush will always paint in the\ncolor corresponding to <code>GetSysColor(COLOR_WINDOW)<\/code>,\neven if the user changes the color scheme later.\n<\/p>\n<p>\nNow, you might be tempted to use a system color brush as\nyour class background brush.\nAfter all, if you want the background to be the system window color,\nwhy not use a brush that is always the system window color?\n<\/p>\n<p>\nWell, because the documentation for <code>GetSysColorBrush<\/code>\nexplicitly tells you not to do that.\nIf you tried this on Windows&nbsp;95, it would have seemed to work\nfor a while, and then bad things would start happening.\n<\/p>\n<p>\nThe system color brushes were added as a convenience to save people\nthe trouble of having to create solid color brushes just to draw\na system color.\nProfiling in Windows&nbsp;95 revealed that a lot of time was\nspent by applications creating solid brushes, using them briefly,\nand then destroying them, so anything that could be done to reduce\nthe rate at which applications needed to do this was a good thing.\n<\/p>\n<p>\nThe system color brushes were implemented in Windows&nbsp;95\nby creating them and then telling GDI, &#8220;Hey, if somebody tries\nto <code>DeleteObject<\/code> this brush, don&#8217;t let them.&#8221;\nThis prevented the system color brushes from being accidentally\ndestroyed.\n<\/p>\n<p>\nExcept when it didn&#8217;t.\n<\/p>\n<p>\nWhen you registered a window class with a background brush\n(and by that, I mean an honest-to-goodness brush and not a\npseudo-brush you get from that\n<code>(HBRUSH)(COLOR_xxx + 1)<\/code> stuff)\nthe window manager did the same thing to the brush as it\ndid to the system color brushes: It told GDI,\n&#8220;Hey, if somebody tries\nto <code>DeleteObject<\/code> this brush, don&#8217;t let them.&#8221;\nThis prevented people from destroying brushes while the window\nmanager was still using them to draw the background of a window.\n<\/p>\n<p>\nWhen you unregistered the window class, the window manager told\nGDI, &#8220;Okay, delete this brush, and yes, I told you not to let\nanyone <code>DeleteObject<\/code> it, but I&#8217;m overriding it\nbecause I was the one who protected it in the first place.\nI&#8217;m just cancelling my previous instructions to protect this brush.&#8221;\nThe window manager takes responsibility for\ndestroying the brush when you register the class;\ntherefore, when you unregister the class, the window manager\nis obligated to clean up and destroy the brush.\n(Actually, it&#8217;s a little more complicated than that, because it is\nlegal to use one brush as the background brush for multiple\nwindow classes, but let&#8217;s ignore that case for now.)\n<\/p>\n<p>\nDo you see the problem yet?\n<\/p>\n<p>\nWhat if you registered a window class with a system color brush\nas the background brush and then unregistered it?\n(Don&#8217;t forget that classes are automatically unregistered when\nthe process exits.)\nWhen you registered the class, the brush got protected,\nand when you unregistered the class, the Windows&nbsp;95 window manager\n<strong>told GDI to override the protection and\ndestroy the brush anyway<\/strong>.\n<\/p>\n<p>\nOops, we just destroyed a system color brush.\nEven though those brushes were protected,\nthe protection didn&#8217;t work here because you went through the\ncode path where the window manager said,\n&#8220;Override the safety interlocks!&#8221;\n<\/p>\n<p>\nBut of course, you didn&#8217;t need to use a system color brush in the\nfirst place.\nYou should have used that\n<code>(HBRUSH)(COLOR_xxx + 1)<\/code> pseudo-brush.\n<\/p>\n<p>\n(Note to nitpickers: This story talked about Windows&nbsp;95.\nIt does not apply to any other version of Windows.\nThe problem may or may not exist in those other versions;\nI make no claims.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to register a window class and use a system color as its background color, you set the hbrBackground member to the desired color, plus one, cast to an HBRUSH: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); Windows&nbsp;95 introduced &#8220;system color brushes&#8221;, which are a magic type of brush which always paint in the corresponding [&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,2],"class_list":["post-28893","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code","tag-history"],"acf":[],"blog_post_summary":"<p>If you want to register a window class and use a system color as its background color, you set the hbrBackground member to the desired color, plus one, cast to an HBRUSH: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); Windows&nbsp;95 introduced &#8220;system color brushes&#8221;, which are a magic type of brush which always paint in the corresponding [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28893","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=28893"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/28893\/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=28893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=28893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=28893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}