{"id":1593,"date":"2014-03-05T07:00:00","date_gmt":"2014-03-05T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/03\/05\/why-do-i-have-to-add-1-to-the-color-index-when-i-set-it-as-the-hbrbackground-of-a-window-class\/"},"modified":"2014-03-05T07:00:00","modified_gmt":"2014-03-05T07:00:00","slug":"why-do-i-have-to-add-1-to-the-color-index-when-i-set-it-as-the-hbrbackground-of-a-window-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140305-00\/?p=1593","title":{"rendered":"Why do I have to add 1 to the color index when I set it as the hbrBackground of a window class?"},"content":{"rendered":"<p>\nOur\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/07\/23\/54576.aspx\">\nscratch program<\/a>\nsets the background color to <code>COLOR_WINDOW<\/code>\nby setting the class background brush as follows:\n<\/p>\n<pre>\n    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);\n<\/pre>\n<p>\nWhat&#8217;s with the <code>+1<\/code>?\n<\/p>\n<p>\nOkay, first of all, let&#8217;s backtrack a bit.\n<\/p>\n<p>\nThe real first question is,\n&#8220;What&#8217;s the deal with taking an integer (<code>COLOR_WINDOW<\/code>)\nand casting it to a <code>HBRUSH<\/code> and expecting anything sane\nto happen?&#8221;\n<\/p>\n<p>\nThe window manager wants to provide multiple ways of setting the class\nbackground brush.\n<\/p>\n<ol>\n<li>The application can request\n    that no automatic background drawing should occur at all.<\/p>\n<li>The application can request\n    custom background drawing and provide that custom drawing by handling\n    the <code>WM_ERASE&shy;BKGND<\/code> message.<\/p>\n<li>The application can request that the background be a specific brush\n    provided by the application.<\/p>\n<li>The application can request that the background be a specific system color.\n<\/ol>\n<p>\nThe first three cases are easy:\nIf you don&#8217;t want automatic background drawing,\nthen pass\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2004\/01\/26\/62991.aspx\">\nthe hollow brush<\/a>.\nIf you want custom background drawing,\nthen pass <code>NULL<\/code> as the brush.\nAnd if you want background drawing with a specific brush,\nthen pass that brush.\nIt&#8217;s the last case that is weird.\n<\/p>\n<p>\nNow, if <code>Register&shy;Class<\/code> were being invented today,\nwe would satisfy the last requirement\nby saying,\n&#8220;If you want the background to be a system color,\nthen use a system color brush like this:\n<\/p>\n<pre>\n    wc.hbrBackground = GetSysColorBrush(COLOR_WINDOW);\n<\/pre>\n<p>System color brushes match the corresponding system color,\nso this sets your background to whatever the current system\nwindow color is.&#8221;\n<\/p>\n<p>\nBut just as\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2011\/01\/19\/10117410.aspx\">\nNASA couldn&#8217;t use the Space Shuttle to rescue the Apollo 13 astronauts<\/a>,\nthe\n<code>Register&shy;Class<\/code> function couldn&#8217;t use\n<code>Get&shy;Sys&shy;Color&shy;Brush<\/code> for class brushes:\nAt the time\n<code>Register&shy;Class<\/code> was designed,\nsystem color brushes had not yet been invented yet.\nIn fact, they won&#8217;t have been invented for over a decade.\n<\/p>\n<p>\nTherefore, <code>Register&shy;Class<\/code> had to find some\nway of smuggling an integer inside a pointer,\nand the traditional way of doing this is to say that\ncertain numerically-small pointer values are actually integers\nin disguise.\nWe&#8217;ve seen this with\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2006\/05\/05\/590749.aspx\">\nthe <code>HINSTANCE<\/code> returned by\n<code>Shell&shy;Execute<\/code><\/a>,\nwith\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2008\/05\/01\/8445487.aspx\">\nthe <code>MAKE&shy;INT&shy;ATOM<\/code> macro<\/a>,\nwith the\n<code>MAKE&shy;INT&shy;RESOURCE<\/code>\/<code>IS_INT&shy;RESOURCE<\/code>\nmacro pair,\nand with the second parameter to the\n<code>Get&shy;Proc&shy;Address<\/code> function.\n(There are plenty of other examples.)\n<\/p>\n<p>\nThe na&iuml;ve solution would therefore be to say,\n&#8220;Well, if you want a system color to be used as the brush color,\nthen just cast the <code>COLOR_XXX<\/code> value to\nan <code>HBRUSH<\/code>,\nand the <code>Register&shy;Class<\/code> function will recognize it\nas a smuggled integer and treat it as a color code rather than an\nactual brush.&#8221;\n<\/p>\n<p>\nAnd then you run into a problem:\nThe numeric value of\n<code>COLOR_SCROLL&shy;BAR<\/code> is zero.\nCasting this to a <code>HBRUSH<\/code> would result in a\n<code>NULL<\/code> pointer,\nbut a <code>NULL<\/code> brush already means something else:\nDon&#8217;t draw any background at all.\n<\/p>\n<p>\nTo avoid this conflict, the <code>Register&shy;Class<\/code>\nfunction artificially adds 1 to the system color number\nso that none of its smuggled integers will be mistaken for\n<code>NULL<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Our scratch program sets the background color to COLOR_WINDOW by setting the class background brush as follows: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); What&#8217;s with the +1? Okay, first of all, let&#8217;s backtrack a bit. The real first question is, &#8220;What&#8217;s the deal with taking an integer (COLOR_WINDOW) and casting it to a HBRUSH and expecting [&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-1593","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Our scratch program sets the background color to COLOR_WINDOW by setting the class background brush as follows: wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); What&#8217;s with the +1? Okay, first of all, let&#8217;s backtrack a bit. The real first question is, &#8220;What&#8217;s the deal with taking an integer (COLOR_WINDOW) and casting it to a HBRUSH and expecting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1593","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=1593"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1593\/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=1593"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1593"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1593"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}