{"id":34803,"date":"2005-07-26T07:00:00","date_gmt":"2005-07-26T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/07\/26\/what-is-the-difference-between-wm_destroy-and-wm_ncdestroy\/"},"modified":"2005-07-26T07:00:00","modified_gmt":"2005-07-26T07:00:00","slug":"what-is-the-difference-between-wm_destroy-and-wm_ncdestroy","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050726-00\/?p=34803","title":{"rendered":"What is the difference between WM_DESTROY and WM_NCDESTROY?"},"content":{"rendered":"<p><P>\nThere are two window messages closely-associated with window\ndestruction,\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windows\/windowreference\/windowmessages\/wm_destroy.asp\">\nthe <CODE>WM_DESTROY<\/CODE> message<\/A>\nand\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/windowing\/windows\/windowreference\/windowmessages\/wm_ncdestroy.asp\">\nthe <CODE>WM_NCDESTROY<\/CODE> message<\/A>.\nWhat&#8217;s the difference?\n<\/P>\n<P>\nThe difference is that\nthe <CODE>WM_DESTROY<\/CODE> message\nis sent at the start of the window destruction sequence,\nwhereas the <CODE>WM_NCDESTROY<\/CODE> message is sent at the end.\nThis is an important distinction when you have child windows.\nIf you have a parent window with a child window,\nthen the message traffic (in the absence of weirdness)\nwill go like this:\n<\/P>\n<PRE>\nhwnd = parent, uMsg = WM_DESTROY\nhwnd = child, uMsg = WM_DESTROY\nhwnd = child, uMsg = WM_NCDESTROY\nhwnd = parent, uMsg = WM_NCDESTROY\n<\/PRE>\n<P>\nNotice that the parent receives the <CODE>WM_DESTROY<\/CODE>\nbefore the child windows are destroyed,\nand it receives the <CODE>WM_NCDESTROY<\/CODE> message after they have\nbeen destroyed.\n<\/P>\n<P>\nHaving two destruction messages, one sent top-down and the other\nbottom-up, means that you can perform clean-up appropriate to\na particular model when handling the corresponding message.\nIf there is something that must be cleaned up top-down, then you can\nuse the <CODE>WM_DESTROY<\/CODE> message, for example.\n<\/P>\n<P>\nThe <CODE>WM_NCDESTROY<\/CODE> is the last message your window will\nreceive (in the absence of weirdness), and it is therefore the best\nplace to do &#8220;final cleanup&#8221;.\nThis is why\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/04\/22\/410773.aspx\">\nour new scratch program<\/a>\nwaits until <CODE>WM_NCDESTROY<\/CODE> to destroy its instance\nvariables.\n<\/P>\n<P>\nThese two destruction messages are paired with the analogous\n<CODE>WM_CREATE<\/CODE> and <CODE>WM_NCCREATE<\/CODE> messages.\nJust as <CODE>WM_NCDESTROY<\/CODE> is the last message your window receives,\nthe <CODE>WM_NCCREATE<\/CODE> message is the first message,\nso that&#8217;s a good place to create your instance variables.\nNote also that\nif you cause the <CODE>WM_NCCREATE<\/CODE> message to return failure,\nthen all you will get is <CODE>WM_NCDESTROY<\/CODE>;\nthere will be no <CODE>WM_DESTROY<\/CODE> since you never got the\ncorresponding <CODE>WM_CREATE<\/CODE>.\n<\/P>\n<P>\nWhat&#8217;s this &#8220;absence of weirdness&#8221; I keep alluding to?\nWe&#8217;ll look at that\n<A HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2005\/07\/27\/443824.aspx\">\nnext time<\/A>.\n<\/P>\n<P>\n[Typos corrected, 9:30am]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are two window messages closely-associated with window destruction, the WM_DESTROY message and the WM_NCDESTROY message. What&#8217;s the difference? The difference is that the WM_DESTROY message is sent at the start of the window destruction sequence, whereas the WM_NCDESTROY message is sent at the end. This is an important distinction when you have child windows. [&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-34803","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There are two window messages closely-associated with window destruction, the WM_DESTROY message and the WM_NCDESTROY message. What&#8217;s the difference? The difference is that the WM_DESTROY message is sent at the start of the window destruction sequence, whereas the WM_NCDESTROY message is sent at the end. This is an important distinction when you have child windows. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34803","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=34803"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/34803\/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=34803"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=34803"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=34803"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}