{"id":8953,"date":"2011-12-07T07:00:00","date_gmt":"2011-12-07T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2011\/12\/07\/getparent-just-as-confusing-as-enumclaw-but-its-an-actual-function\/"},"modified":"2011-12-07T07:00:00","modified_gmt":"2011-12-07T07:00:00","slug":"getparent-just-as-confusing-as-enumclaw-but-its-an-actual-function","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20111207-00\/?p=8953","title":{"rendered":"GetParent, just as confusing as EnumClaw, but it&#039;s an actual function!"},"content":{"rendered":"<p><a href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/04\/01\/9988477.aspx\"> The function <code>Enum&shy;Claw<\/code><\/a>, documented as returning &#8220;the child or the parent of the window&#8221;, was a joke, but there&#8217;s a function whose behavior is just as confusing as the joke function <code>Enum&shy;Claw<\/code>: <code>Get&shy;Parent<\/code>.\n The <code>Get&shy;Parent<\/code> function returns the parent window, or owner window, or possibly neither. All that&#8217;s left is for it to be a floor wax and it&#8217;ll have everything covered.\n The idea behind <code>Get&shy;Parent<\/code> is that it returns the parent window. Only child windows have parents, so what happens if you pass something that isn&#8217;t a child window? Well, we shouldn&#8217;t let a parameter go to waste, right? So let&#8217;s have it return the <i>owner window<\/i> if you pass in a top-level window. But wait, we&#8217;re not going to return the owner window for all top-level windows, just for top-level windows which have the <code>WS_POPUP<\/code> style.\n This last bit regarding the <code>WS_POPUP<\/code> style is a leftover from Windows&nbsp;1.0, where there was a distinction made between &#8220;tiled windows&#8221; and &#8220;popup windows.&#8221; Popup windows could overlap, whereas tiled windows could not, and the function was only interested in windows that can overlap. Of course, now that all windows can overlap, the rejection of tiled windows is just a compatibility remnant.\n Anyway, the algorithm for <code>Get&shy;Parent<\/code> goes like this:<\/p>\n<ul>\n<li>If the window is a child window, then return the parent window. <\/li>\n<li>Else, the window is a top-level window.     If <code>WS_POPUP<\/code> style is set, and the window has an owner,     then return the owner. <\/li>\n<li>Else, return <code>NULL<\/code>. <\/li>\n<\/ul>\n<p> Here it is in tabular form, since I&#8217;ve discovered that people like tables:<\/p>\n<table border=\"1\" style=\"border-collapse: collapse\" cellpadding=\"3\">\n<tr>\n<th colspan=\"2\" rowspan=\"2\"><code>Get&shy;Parent<\/code> return values<\/th>\n<th colspan=\"2\"><code>WS_CHILD<\/code><\/th>\n<\/tr>\n<tr>\n<th>Set<\/th>\n<th>Clear<\/th>\n<\/tr>\n<tr>\n<th rowspan=\"2\"><code>WS_POPUP<\/code><\/th>\n<th>Set<\/th>\n<td>N\/A<\/td>\n<td>Owner<\/td>\n<\/tr>\n<tr>\n<th>Clear<\/th>\n<td>Parent<\/td>\n<td><code>NULL<\/code><\/td>\n<\/tr>\n<\/table>\n<p> The upper left entry of the table (corresponding to <code>WS_CHILD<\/code> and <code>WS_POPUP<\/code> both set) is left as N\/A because that combination of styles is illegal.<\/p>\n<p> Fortunately, you don&#8217;t have to deal with all the craziness of the <code>Get&shy;Parent<\/code> function. There are ways to get the parent or owner separately and explicitly without having to deal with <code>Get&shy;Parent<\/code>&#8216;s quirks. <\/p>\n<ul>\n<li>To get the parent window, call <code>GetAncestor(hwnd, GA_PARENT)<\/code>. <\/li>\n<li>To get the owner window, call <code>GetWindow(hwnd, GW_OWNER)<\/code>. <\/li>\n<\/ul>\n<p> Compatibility requirements prevent <code>Get&shy;Parent<\/code> from ever going away, but that doesn&#8217;t mean you are forced to continue using it. Use one of the less confusing alternatives. That&#8217;s why they&#8217;re there. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The function Enum&shy;Claw, documented as returning &#8220;the child or the parent of the window&#8221;, was a joke, but there&#8217;s a function whose behavior is just as confusing as the joke function Enum&shy;Claw: Get&shy;Parent. The Get&shy;Parent function returns the parent window, or owner window, or possibly neither. All that&#8217;s left is for it to be a [&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-8953","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>The function Enum&shy;Claw, documented as returning &#8220;the child or the parent of the window&#8221;, was a joke, but there&#8217;s a function whose behavior is just as confusing as the joke function Enum&shy;Claw: Get&shy;Parent. The Get&shy;Parent function returns the parent window, or owner window, or possibly neither. All that&#8217;s left is for it to be a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8953","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=8953"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/8953\/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=8953"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=8953"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=8953"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}