{"id":19603,"date":"2009-01-05T10:00:00","date_gmt":"2009-01-05T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2009\/01\/05\/even-if-you-have-code-to-handle-a-message-youre-allowed-to-call-defwindowproc-because-you-were-doing-that-anyway-after-all\/"},"modified":"2009-01-05T10:00:00","modified_gmt":"2009-01-05T10:00:00","slug":"even-if-you-have-code-to-handle-a-message-youre-allowed-to-call-defwindowproc-because-you-were-doing-that-anyway-after-all","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20090105-00\/?p=19603","title":{"rendered":"Even if you have code to handle a message, you&#8217;re allowed to call DefWindowProc, because you were doing that anyway after all"},"content":{"rendered":"<p><P>\nJust because you write <CODE>case WM_SOMETHING:<\/CODE> doesn&#8217;t mean\nthat you have to handle all possible parameters for the\n<CODE>WM_SOMETHING<\/CODE> message.\nYou&#8217;re still allowed to call the <CODE>DefWindowProc<\/CODE> function.\nAfter all, that&#8217;s what you did when you didn&#8217;t have a\n<CODE>case WM_SOMETHING:<\/CODE> statement in the first place.\n<\/P>\n<PRE>\nswitch (uMsg) {\ncase WM_CHAR:\n    OnChar(&#8230;);\n    return 0;<\/p>\n<p>default:\n    return DefWindowProc(&#8230;);\n}\n<\/PRE>\n<P>\nThe above code fragment doesn&#8217;t handle the <CODE>WM_SOMETHING<\/CODE>\nmessage at all.\nSuppose the <CODE>WM_SOMETHING<\/CODE> message uses the <CODE>wParam<\/CODE>\nparameter to specify what type of something occurred, and you\nonly want to override the default processing\nin the case where <CODE>wParam<\/CODE> has the value of 4.\nWhat do you do with the other values?\n<\/P>\n<PRE>\nswitch (uMsg) {\ncase WM_CHAR:\n    OnChar(&#8230;);\n    return 0;<\/p>\n<p><FONT COLOR=\"blue\">case WM_SOMETHING:\n    if (wParam == 4) { DoSomething4(&#8230;); }\n    else &#8230; ????? &#8230;\n    return 0;<\/FONT><\/p>\n<p>default:\n    return DefWindowProc(&#8230;);\n}\n<\/PRE>\n<P>\nIf the value is 4, then you do your special &#8220;something 4&#8221; processing,\nbut what about all the other values? How do you handle them?\n<\/P>\n<P>\nWell, think about it: How did you handle them before?\nThe original code, before you added a <CODE>WM_SOMETHING<\/CODE>\nhandler, was equivalent to this:\n<\/P>\n<PRE>\nswitch (uMsg) {\ncase WM_CHAR:\n    OnChar(&#8230;);\n    return 0;<\/p>\n<p><FONT COLOR=\"blue\">case WM_SOMETHING:\n    return DefWindowProc(&#8230;);<\/FONT><\/p>\n<p>default:\n    return DefWindowProc(&#8230;);\n}\n<\/PRE>\n<P>\nIn the original code, since there was no explicit handler for\nthe <CODE>WM_SOMETHING<\/CODE> message, control is transferred to\nthe <CODE>default<\/CODE> case handler, which just calls the\n<CODE>DefWindowProc<\/CODE> function.\nIf you really want to, you can expand the case out a bit more:\n<\/P>\n<PRE>\nswitch (uMsg) {\ncase WM_CHAR:\n    OnChar(&#8230;);\n    return 0;<\/p>\n<p>case WM_SOMETHING:\n    <FONT COLOR=\"blue\">if (wParam == 4) return DefWindowProc(&#8230;);\n    else return DefWindowProc(&#8230;);<\/FONT><\/p>\n<p>default:\n    return DefWindowProc(&#8230;);\n}\n<\/PRE>\n<P>\nBecause if the <CODE>wParam<\/CODE> is 4, the original code just\ncalled <CODE>DefWindowProc<\/CODE>.\nAnd if the <CODE>wParam<\/CODE> was something other than 4,\nthe original code still just\ncalled <CODE>DefWindowProc<\/CODE>.\n<\/P>\n<P>\nOf course, I expanded the block in precisely this way so it matches\nup with the case we started writing when we decided to handle the\n<CODE>WM_SOMETHING<\/CODE> method.\nWritten out this way, it becomes obvious\nwhat to write for the question marks.\n<\/P>\n<PRE>\nswitch (uMsg) {\ncase WM_CHAR:\n    OnChar(&#8230;);\n    return 0;<\/p>\n<p>case WM_SOMETHING:\n    if (wParam == 4) { DoSomething4(&#8230;); }\n    else <FONT COLOR=\"blue\">return DefWindowProc(&#8230;);<\/FONT>\n    return 0;<\/p>\n<p>default:\n    return DefWindowProc(&#8230;);\n}\n<\/PRE>\n<P>\nJust because you have a <CODE>case WM_SOMETHING<\/CODE> statement\ndoesn&#8217;t mean you have to handle all the cases;\nyou can still call <CODE>DefWindowProc<\/CODE> for the cases\nyou don&#8217;t want to handle.\n<\/P>\n<P>\nArmed with this information, you can help\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/articles\/407234.aspx#533895\">\ncommenter Norman Diamond<\/A>\nhandle the <CODE>VK_F10<\/CODE> key in his <CODE>WM_SYSKEYDOWN<\/CODE>\nmessage handler without having to\n&#8220;start handling a bunch of keys that really are system keys,\nthat I didn&#8217;t want to bother with.&#8221;\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Just because you write case WM_SOMETHING: doesn&#8217;t mean that you have to handle all possible parameters for the WM_SOMETHING message. You&#8217;re still allowed to call the DefWindowProc function. After all, that&#8217;s what you did when you didn&#8217;t have a case WM_SOMETHING: statement in the first place. switch (uMsg) { case WM_CHAR: OnChar(&#8230;); return 0; default: [&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-19603","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>Just because you write case WM_SOMETHING: doesn&#8217;t mean that you have to handle all possible parameters for the WM_SOMETHING message. You&#8217;re still allowed to call the DefWindowProc function. After all, that&#8217;s what you did when you didn&#8217;t have a case WM_SOMETHING: statement in the first place. switch (uMsg) { case WM_CHAR: OnChar(&#8230;); return 0; default: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19603","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=19603"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/19603\/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=19603"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=19603"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=19603"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}