{"id":2903,"date":"2013-10-17T07:00:00","date_gmt":"2013-10-17T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2013\/10\/17\/what-is-the-inverse-of-adjustwindowrect-and-adjustwindowrectex\/"},"modified":"2013-10-17T07:00:00","modified_gmt":"2013-10-17T07:00:00","slug":"what-is-the-inverse-of-adjustwindowrect-and-adjustwindowrectex","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20131017-00\/?p=2903","title":{"rendered":"What is the inverse of AdjustWindowRect and AdjustWindowRectEx?"},"content":{"rendered":"<p>\nWe saw\n<a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2003\/09\/11\/54885.aspx\">\nover a decade ago<\/a>\n(my goodness I&#8217;ve been doing this way too long)\nthat the\n<code>Adjust&shy;Window&shy;Rect<\/code> and\n<code>Adjust&shy;Window&shy;Rect&shy;Ex<\/code> functions\ndo not take menu wrapping into account because they don&#8217;t\ntake a window handle parameter,\nso they don&#8217;t know what menu to test for wrapping.\nStill, they are useful functions if you aren&#8217;t worried about\nmenu wrapping\nbecause they let you do window size calculations without\na window handle (say, before you create your window).\n<\/p>\n<p>\nBut those functions take a proposed client rectangle\nand return the corresponding non-client rectangle\nby inflating the rectangle by the appropriate borders,\ncaption, scroll bars, and other non-client goo.\nBut how do you go the other way?\nSay you have a proposed window rectangle and you want to know\nwhat client rectangle would result from it?\n<\/p>\n<p>\n<code>Adjust&shy;Window&shy;Rect<\/code> and\n<code>Adjust&shy;Window&shy;Rect&shy;Ex<\/code> can do that too.\nYou just have to apply a negative sign.\n<\/p>\n<p>\nThe idea here is that we use the\n<code>Adjust&shy;Window&shy;Rect&shy;Ex<\/code>\nfunction to calculate how much additional non-client area gets\nadded due to the styles we passed.\nTo make the math simple, we ask for a zero client rectangle,\nso that the resulting window is all non-client.\n<\/p>\n<table CELLPADDING=\"0\" CELLSPACING=\"0\" BORDER=\"0\" TITLE=\"A rectangle with left edge -10, right edge +10, top edge -50, bottom edge +10, with a dot at (0,0)\">\n<tr>\n<td COLSPAN=\"3\" ALIGN=\"right\">&minus;10<\/td>\n<td STYLE=\"width: 1em\"><\/td>\n<td ALIGN=\"center\">0<\/td>\n<td STYLE=\"width: 1em\"><\/td>\n<td ALIGN=\"left\">+10<\/td>\n<\/tr>\n<tr>\n<td ROWSPAN=\"3\" VALIGN=\"center\">&minus;50<\/td>\n<td><\/td>\n<td><\/td>\n<td STYLE=\"border-left: solid black 1px\">&nbsp;<\/td>\n<td ALIGN=\"center\">|<\/td>\n<td STYLE=\"border-right: solid black 1px\">&nbsp;<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td STYLE=\"height: 1ex\"><\/td>\n<\/tr>\n<tr STYLE=\"height: 1.5em\">\n<td STYLE=\"border-top: solid black 1px;width: 1em\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td COLSPAN=\"3\" STYLE=\"border-left: solid black 1px;border-top: solid black 1px;border-right: solid black 1px\">&nbsp;<\/td>\n<td><\/td>\n<\/tr>\n<tr STYLE=\"height: 1.5em\">\n<td><\/td>\n<td><\/td>\n<td><\/td>\n<td COLSPAN=\"3\" STYLE=\"border-left: solid black 1px;border-right: solid black 1px\">&nbsp;<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td COLSPAN=\"2\" ALIGN=\"right\">0 &mdash;<\/td>\n<td><\/td>\n<td ALIGN=\"center\" COLSPAN=\"3\" STYLE=\"border-left: solid black 1px;border-right: solid black 1px\">&bull;<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td ROWSPAN=\"2\" VALIGN=\"bottom\">+10<\/td>\n<td STYLE=\"border-bottom: solid black 1px;width: 1em\">&nbsp;<\/td>\n<td>&nbsp;<\/td>\n<td COLSPAN=\"3\" STYLE=\"border-left: solid black 1px;border-bottom: solid black 1px;border-right: solid black 1px\">&nbsp;<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td STYLE=\"height: 1ex\"><\/td>\n<\/tr>\n<\/table>\n<p>\nWe pass in the empty rectangle represented by the dot in the middle,\nand the\n<code>Adjust&shy;Window&shy;Rect&shy;Ex<\/code> expands the rectangle\nin all dimensions.\nWe see that it added ten pixels to the left, right, and bottom,\nand it added fifty pixels to the top.\n(Numbers are for expository purposes.\nActual numbers will vary.)\n<\/p>\n<p>\nFrom this we can perform the reverse calculation:\nInstead of expanding the rectangle, we shrink it.\n<\/p>\n<pre>\nBOOL UnadjustWindowRectEx(\n    LPRECT prc,\n    DWORD dwStyle,\n    BOOL fMenu,\n    DWORD dwExStyle)\n{\n  RECT rc;\n  SetRectEmpty(&amp;rc);\n  BOOL fRc = AdjustWindowRectEx(&amp;rc, dwStyle, fMenu, dwExStyle);\n  if (fRc) {\n    prc-&gt;left -= rc.left;\n    prc-&gt;top -= rc.top;\n    prc-&gt;right -= rc.right;\n    prc-&gt;bottom -= rc.bottom;\n  }\n  return fRc;\n}\n<\/pre>\n<p>\nNote that the top and left are subtracted,\nso that the two negative signs cancel out.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We saw over a decade ago (my goodness I&#8217;ve been doing this way too long) that the Adjust&shy;Window&shy;Rect and Adjust&shy;Window&shy;Rect&shy;Ex functions do not take menu wrapping into account because they don&#8217;t take a window handle parameter, so they don&#8217;t know what menu to test for wrapping. Still, they are useful functions if you aren&#8217;t worried [&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-2903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We saw over a decade ago (my goodness I&#8217;ve been doing this way too long) that the Adjust&shy;Window&shy;Rect and Adjust&shy;Window&shy;Rect&shy;Ex functions do not take menu wrapping into account because they don&#8217;t take a window handle parameter, so they don&#8217;t know what menu to test for wrapping. Still, they are useful functions if you aren&#8217;t worried [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2903","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=2903"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/2903\/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=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}