{"id":106044,"date":"2021-12-20T07:00:00","date_gmt":"2021-12-20T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=106044"},"modified":"2021-12-17T21:16:18","modified_gmt":"2021-12-18T05:16:18","slug":"20211220-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20211220-00\/?p=106044","title":{"rendered":"Why do I have to add one when setting a class background brush to a system color?"},"content":{"rendered":"<p>When you register a window class, you can specify that the background color is a system color by adding one to the system color index, and then casting the result to <code>HBRUSH<\/code>:<\/p>\n<pre>WNDCLASS wc;\r\n...\r\nwc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\r\n<\/pre>\n<p>Why do we add one?<\/p>\n<p>To make sure that the result is not zero.<\/p>\n<p>The <code>COLOR_<wbr \/>SCROLL\u00adBAR<\/code> system color is index zero. If we didn&#8217;t add one to the system color indices, then an attempt to set the background color to the scroll bar color would end up with <code>(HBRUSH)0<\/code>, which is a null pointer. When the system saw that you set <code>hbrBackground<\/code> to a null pointer, it wouldn&#8217;t know whether you meant the window to have no background color (null pointer), or whether you wanted it to have the system scroll bar color (<code>COLOR_<wbr \/>SCROLL\u00adBAR<\/code>).<\/p>\n<p>In other words, adding one ensures that the space of &#8220;system colors smuggled inside a brush handle&#8221; does not overlap with the space of regular brush handles.<\/p>\n<p>In retrospect, this was one of those &#8220;too clever&#8221; hacks born out of the days of 16-bit Windows and systems with only 256KB of memory. Nowadays, we would say, &#8220;Just set the background to <code>GetSysColorBrush(COLOR_WINDOW)<\/code>.&#8221; But back then, <code>GetSysColorBrush(COLOR_WINDOW)<\/code> didn&#8217;t exist.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To make sure it&#8217;s not null.<\/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-106044","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>To make sure it&#8217;s not null.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106044","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=106044"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/106044\/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=106044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=106044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=106044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}