{"id":37683,"date":"2004-10-01T07:01:00","date_gmt":"2004-10-01T07:01:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2004\/10\/01\/how-to-host-an-icontextmenu-part-8-optimizing-for-the-default-command\/"},"modified":"2004-10-01T07:01:00","modified_gmt":"2004-10-01T07:01:00","slug":"how-to-host-an-icontextmenu-part-8-optimizing-for-the-default-command","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20041001-00\/?p=37683","title":{"rendered":"How to host an IContextMenu, part 8 &#8211; Optimizing for the default command"},"content":{"rendered":"<p><P>\nThere is a small improvement that can be made to\n<a href=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2004\/09\/30\/236133.aspx\">\nto the program we wrote last time<\/A>.\nIt involves taking advantage of the last parameter to\n<A HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/shellcc\/platform\/shell\/reference\/ifaces\/icontextmenu\/QueryContextMenu.asp\">\nthe IContextMenu::QueryContextMenu method<\/A>:\n<\/P>\n<BLOCKQUOTE CLASS=\"q\">\n<P STYLE=\"text-indent: -5em;padding-left: 5em\">\nCMF_DEFAULTONLY<BR>\nThis flag is set when the user is activating the default action,\ntypically by double-clicking.\nThis flag provides a hint for the shortcut menu extension\nto add nothing if it does not modify the default item in the menu.\nA shortcut menu extension or drag-and-drop handler\nshould not add any menu items if this value is specified.\nA namespace extension should add only the default item (if any).\n<\/BLOCKQUOTE>\n<P>\nAs the text from MSDN indicates, this flag is a hint to the\nIContextMenu implementation\nthat it should worry only about the default command.\n<\/P>\n<PRE>\nvoid OnContextMenu(HWND hwnd, HWND hwndContext, UINT xPos, UINT yPos)\n{\n  IContextMenu *pcm;\n  if (SUCCEEDED(GetUIObjectOfFile(hwnd, L&#8221;C:\\\\Windows\\\\clock.avi&#8221;,\n                   IID_IContextMenu, (void**)&amp;pcm))) {\n    HMENU hmenu = CreatePopupMenu();\n    if (hmenu) {\n      if (SUCCEEDED(pcm-&gt;QueryContextMenu(hmenu, 0,\n                             SCRATCH_QCM_FIRST, SCRATCH_QCM_LAST,\n                             <FONT COLOR=\"blue\">CMF_DEFAULTONLY<\/FONT>))) {\n        UINT id = GetMenuDefaultItem(hmenu, FALSE, 0);\n        if (id != (UINT)-1) {\n          CMINVOKECOMMANDINFO info = { 0 };\n          info.cbSize = sizeof(info);\n          info.hwnd = hwnd;\n          info.lpVerb = MAKEINTRESOURCEA(id &#8211; SCRATCH_QCM_FIRST);<\/FONT>\n          pcm-&gt;InvokeCommand(&amp;info);\n        }\n      }\n      DestroyMenu(hmenu);\n    }\n    pcm-&gt;Release();\n  }\n}\n<\/PRE>\n<P>\nWith this change on my machine, the time taken by the call to\nIContextMenu::QueryContextMenu dropped from 100ms to 50ms.\nYour mileage may vary.\nIt depends on how many context menu extensions you have and\nhow well they respect the <CODE>CMF_DEFAULTONLY<\/CODE> flag.\n<\/P>\n<P>\n(And this exercise highlights how important it is that people\nwho implement the <CODE>IContextMenu<\/CODE>\ninterface pay attention to the flags.\n<A HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/09\/30\/55100.aspx#55107\">\nIf your context menu handler doesn&#8217;t respect the <CODE>CMF_DEFAULTONLY<\/CODE>\nflag<\/A>, then you&#8217;re part of the problem.)\n<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is a small improvement that can be made to to the program we wrote last time. It involves taking advantage of the last parameter to the IContextMenu::QueryContextMenu method: CMF_DEFAULTONLY This flag is set when the user is activating the default action, typically by double-clicking. This flag provides a hint for the shortcut menu extension [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-37683","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>There is a small improvement that can be made to to the program we wrote last time. It involves taking advantage of the last parameter to the IContextMenu::QueryContextMenu method: CMF_DEFAULTONLY This flag is set when the user is activating the default action, typically by double-clicking. This flag provides a hint for the shortcut menu extension [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37683","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=37683"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/37683\/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=37683"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=37683"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=37683"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}