{"id":36263,"date":"2005-03-07T07:00:00","date_gmt":"2005-03-07T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/03\/07\/a-timed-context-menu\/"},"modified":"2005-03-07T07:00:00","modified_gmt":"2005-03-07T07:00:00","slug":"a-timed-context-menu","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050307-00\/?p=36263","title":{"rendered":"A timed context menu"},"content":{"rendered":"<p>\nThis is sort of in the same spirit as our previous\nexercise in writing a timed message box, but this is much\neasier.\nHere, we use the handy-dandy\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/WinUI\/WindowsUserInterface\/Windowing\/Windows\/WindowReference\/WindowMessages\/WM_CANCELMODE.asp\">\n<code>WM_CANCELMODE<\/code> message<\/a>\nto get us out of menu mode.\n<\/p>\n<pre>\nvoid CALLBACK\nMenuTooLateProc(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime)\n{\n  SendMessage(hwnd, WM_CANCELMODE, 0, 0);\n}\nBOOL\nTimedTrackPopupMenuEx(HMENU hMenu, UINT uFlags, int x, int y,\n    HWND hwnd, LPTPMPARAMS pTpm, DWORD dwTimeout)\n{\n    UINT idTimer = SetTimer(NULL, IDT_TOOLATE, dwTimeout, MenuTooLateProc);\n    BOOL fResult = TrackPopupMenuEx(hMenu, uFlags, x, y, hwnd, pTpm);\n    if (idTimer) KillTimer(NULL, idTimer);\n    return fResult;\n}\n<\/pre>\n<p>\nBefore displaying the menu, we set a timer.  (And we use a thread\ntimer because we don&#8217;t own the <code>hwnd<\/code> window and therefore\ndon&#8217;t know what timer IDs are safe to use.)\nIf the timer fires,\nwe send ourselves a <code>WM_CANCELMODE<\/code> message to cancel menu mode.\nThis causes the system to act as if the user had dismissed the\nmenu without selecting anything, either by hitting <tt>ESC<\/tt> or\nclicking outside the menu.  The call to\n<a HREF=\"http:\/\/msdn.microsoft.com\/library\/en-us\/winui\/winui\/windowsuserinterface\/resources\/menus\/menureference\/menufunctions\/trackpopupmenuex.asp\">\nthe <code>TrackPopupMenuEx<\/code> function<\/a>\nreturns once the user has selected something\n(or the timeout has elapsed), at which point we clean up by\ndestroying our timer before returning.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is sort of in the same spirit as our previous exercise in writing a timed message box, but this is much easier. Here, we use the handy-dandy WM_CANCELMODE message to get us out of menu mode. void CALLBACK MenuTooLateProc(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime) { SendMessage(hwnd, WM_CANCELMODE, 0, 0); } BOOL TimedTrackPopupMenuEx(HMENU [&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-36263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>This is sort of in the same spirit as our previous exercise in writing a timed message box, but this is much easier. Here, we use the handy-dandy WM_CANCELMODE message to get us out of menu mode. void CALLBACK MenuTooLateProc(HWND hwnd, UINT uiMsg, UINT idEvent, DWORD dwTime) { SendMessage(hwnd, WM_CANCELMODE, 0, 0); } BOOL TimedTrackPopupMenuEx(HMENU [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36263","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=36263"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36263\/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=36263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}