{"id":102747,"date":"2019-08-02T07:00:00","date_gmt":"2019-08-02T14:00:00","guid":{"rendered":"http:\/\/devblogs.microsoft.com\/oldnewthing\/?p=102747"},"modified":"2019-08-02T06:51:42","modified_gmt":"2019-08-02T13:51:42","slug":"20190802-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20190802-00\/?p=102747","title":{"rendered":"How can I extract the color from a solid color GDI brush?"},"content":{"rendered":"<p>Suppose you have a GDI <code>HBRUSH<\/code> and you suspect that it is a solid color brush. How can you confirm this suspicion and, if true, get the underlying color?<\/p>\n<p>You can ask the <code>Get\u00adObject<\/code> function to peek inside the brush.<\/p>\n<pre>COLORREF GetBrushColor(HBRUSH brush)\r\n{\r\n  LOGBRUSH lbr;\r\n  if (GetObject(brush, sizeof(lbr), &amp;lbr) != sizeof(lbr)) {\r\n    \/\/ Not even a brush!\r\n    return CLR_NONE;\r\n  }\r\n  if (lbr.lbStyle != BS_SOLID) {\r\n    \/\/ Not a solid color brush.\r\n    return CLR_NONE;\r\n  }\r\n  return lbr.lbColor;\r\n}\r\n<\/pre>\n<p>Given a brush, the <code>Get\u00adObject<\/code> function gives you basic information about the brush. The <code>lbStyle<\/code> member tells you what kind of brush you have. In our case, we are interested in solid color brushes.<\/p>\n<p>If we do have a solid color brush, then the <code>lbColor<\/code> tells the underlying color.<\/p>\n<p>We can run a few simple tests to confirm that this works:<\/p>\n<pre>  COLORREF clr;\r\n\r\n  \/\/ This returns clr == RGB(0,0,0)\r\n  clr = GetBrushColor((HBRUSH)GetStockObject(BLACK_BRUSH));\r\n\r\n  \/\/ This returns clr == RGB(64,64,64)\r\n  clr = GetBrushColor((HBRUSH)GetStockObject(DKGRAY_BRUSH));\r\n\r\n  \/\/ This returns clr == RGB(1,2,3)\r\n  HBRUSH brush = CreateSolidBrush(RGB(1, 2, 3));\r\n  clr = GetBrushColor(brush);\r\n  DeleteObject(brush);\r\n\r\n  \/\/ This returns clr == GetSysColor(COLOR_INFOBK)\r\n  clr = GetBrushColor(GetSysColorBrush(COLOR_INFOBK));\r\n\r\n  \/\/ This returns clr == CLR_NONE, not a solid color brush\r\n  clr = GetBrushColor((HBRUSH)GetStockObject(HOLLOW_BRUSH));\r\n<\/pre>\n<p>If you want to understand brushes that aren&#8217;t solid color brushes, you can dig into the <code>lbHatch<\/code> member. It contains additional information that describes the brush, the format of which varies depending on the <code>lbStyle<\/code>.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can ask for the object information.<\/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-102747","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>You can ask for the object information.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102747","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=102747"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/102747\/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=102747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=102747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=102747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}