{"id":1383,"date":"2014-03-27T07:00:00","date_gmt":"2014-03-27T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2014\/03\/27\/functions-that-return-gdi-regions-rarely-actually-return-regions\/"},"modified":"2014-03-27T07:00:00","modified_gmt":"2014-03-27T07:00:00","slug":"functions-that-return-gdi-regions-rarely-actually-return-regions","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20140327-00\/?p=1383","title":{"rendered":"Functions that return GDI regions rarely actually return regions"},"content":{"rendered":"<p>\nFor reasons I don&#8217;t quite understand,\nthe only functions in GDI and USER which create GDI regions\nare the functions with <code>Create<\/code> in their name,\nlike\n<code>Create&shy;Rect&shy;Rgn<\/code> or\n<code>Create&shy;Polygon&shy;Rgn<\/code>.\nAll the other functions which return a region require you\nto pass an existing region to use as the output.\n<\/p>\n<p>\nI can see this being useful for\n<code>Combine&shy;Rgn<\/code>,\nbecause you can set your output region to be equal\nto one of the input regions in order to update a region\nin place.\n<\/p>\n<pre>\n\/\/ hrgnTotal |= hrgnSegment\nCombineRgn(hrgnTotal, hrgnTotal, hrgnSegment, RGN_OR);\n\/\/ hrgnTotal &amp;= hrgnClip\nCombineRgn(hrgnTotal, hrgnTotal, hrgnClip, RGN_AND);\n<\/pre>\n<p>\nBut for all of the <code>Get<\/code> functions,\nhaving to create an output region is usually just\nan annoyance.\n<\/p>\n<pre>\n\/\/ Create a dummy region - contents not important\nHRGN hrgnClip = CreateRectRgn(0, 0, 0, 0);\n\/\/ Ask for the clipping region to be copied to the dummy region\nint status = GetClipRgn(hdc, hrgnClip);\n<\/pre>\n<p>\nI guess it lets you reuse a single dummy region over and over\nagain, but in practice, you&#8217;re just going to destroy the region\nwhen you&#8217;re done to free up the GDI region memory.\n<\/p>\n<p>\nWhatever the historical reason for this, we&#8217;re stuck with it.\nIt may be an ugly pattern, but at least it&#8217;s a pattern.\n<\/p>\n<p>\n<b>The things I do for you<\/b>:\nI asked a colleague who worked on Windows&nbsp;3.0\nif he knew the reason for this design pattern.\nHe didn&#8217;t know but suggested that I ask another person\nwho retired from Microsoft many, many years ago.\nFortunately, I happened to have his email address even though we\naren&#8217;t really that close.\nAnd the second person also didn&#8217;t know.\n&#8220;This behavior was already in place when I joined the Windows 1.03 project.\nMaybe you can ask\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/06\/18\/10321212.aspx\">\nRao<\/a>.&#8221;\nUnfortunately, I don&#8217;t have Rao&#8217;s email address,\nso the trail ran cold.\nBut I burned a favor for you guys.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>For reasons I don&#8217;t quite understand, the only functions in GDI and USER which create GDI regions are the functions with Create in their name, like Create&shy;Rect&shy;Rgn or Create&shy;Polygon&shy;Rgn. All the other functions which return a region require you to pass an existing region to use as the output. I can see this being useful [&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-1383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>For reasons I don&#8217;t quite understand, the only functions in GDI and USER which create GDI regions are the functions with Create in their name, like Create&shy;Rect&shy;Rgn or Create&shy;Polygon&shy;Rgn. All the other functions which return a region require you to pass an existing region to use as the output. I can see this being useful [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1383","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=1383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/1383\/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=1383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=1383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=1383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}