{"id":1451,"date":"2021-09-09T17:11:21","date_gmt":"2021-09-09T09:11:21","guid":{"rendered":"https:\/\/gnonstop.linkpc.net\/wp\/?p=1451"},"modified":"2025-08-22T22:36:11","modified_gmt":"2025-08-22T14:36:11","slug":"keyboard-messages-accelerators-handling-in-mfc-dialog-based-applications","status":"publish","type":"post","link":"https:\/\/guerilla.ddns.net\/wp\/?p=1451","title":{"rendered":"MFC \u5c0d\u8a71\u76d2\u7a0b\u5f0f\uff0c\u5982\u4f55\u8655\u7406\u9375\u76e4\u8a0a\u606f\/\u5feb\u901f\u9375"},"content":{"rendered":"<p>\u4e00\u3001\u5efa\u7acb\u4e00\u500b MFC \u5c0d\u8a71\u76d2\u7a0b\u5f0f (\u53ef\u76f4\u63a5\u4e0b\u8f09\u9019\u908a\u7684\u7bc4\u4f8b <a href=\"\/wp\/wp-content\/uploads\/DlgKeystroke-0.001.zip\">DlgKeystroke-0.001.zip<\/a>)<br \/>\n&nbsp;<br \/>\n\u4e8c\u3001\u8655\u7406 dialog \u7684 virtual BOOL <strong>PreTranslateMessage<\/strong>(MSG* pMsg)<br \/>\n&nbsp;<br \/>\n\u7bc4\u4f8b\uff11\uff1a<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">    if (pMsg-&gt;message == WM_KEYDOWN)\n    {\n        if (pMsg-&gt;wParam == VK_RETURN)\n            pMsg-&gt;wParam = VK_TAB;\n    }\n    else\n    {\n        return CDialog::PreTranslateMessage(pMsg);\n    }<\/code><\/pre>\n<p>&nbsp;<br \/>\n\u9019\u6a23\u8655\u7406\u7684\u8a71\uff0c\u5728\u5c0d\u8a71\u76d2\u6309\u4e0b <kbd>Enter<\/kbd> \u6703\u8b8a\u6210 <kbd>Tab<\/kbd><br \/>\n&nbsp;<\/p>\n<p>\u7bc4\u4f8b\uff12\uff1a<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">    int iChanged = 0;\n    \/\/ If this is a system message, is the Alt bit of the message on?\n    if (pMsg-&gt;wParam == VK_MENU)\n    {\n        bool AltBit = false;\n        if ((pMsg-&gt;message &amp; WM_SYSKEYDOWN) || (pMsg-&gt;message &amp; WM_SYSKEYUP))\n        {\n            AltBit = (pMsg-&gt;lParam &amp; (1 &lt;&lt; 29)) != 0;\n            if (bAlt != AltBit)\n                bAlt = AltBit;\n        }\n        iChanged++;\n    }\n#if 0\n    if (pMsg-&gt;wParam == VK_CONTROL)\n    {\n        if (pMsg-&gt;message == WM_KEYDOWN || pMsg-&gt;message == WM_SYSKEYDOWN)\n        {\n            if (bCtl != true)\n                bCtl = true;\n        }\n        else if (pMsg-&gt;message == WM_KEYUP || pMsg-&gt;message == WM_SYSKEYUP)\n        {\n            if (bCtl != false)\n                bCtl = false;\n        }\n    }\n#else\n    if (::GetKeyState(VK_CONTROL) &lt; 0)\n    {\n        if (bCtl != true)\n            bCtl = true;\n    }\n    else\n    {\n        if (bCtl != false)\n            bCtl = false;\n    }\n#endif\n    \/\/ This is an accelerator example.\n    \/\/ If the user press Alt+F5, this dialog closes.\n    \/\/\n    if (pMsg-&gt;wParam == VK_F5 &amp;&amp; pMsg-&gt;message == WM_SYSKEYUP)\n    {\n        iChanged++;\n        if (bAlt)\n            PostMessage(WM_CLOSE);\n    }\n    return (iChanged != 0) ? TRUE : CDialog::PreTranslateMessage(pMsg);<\/code><\/pre>\n<p>&nbsp;<br \/>\n\u5176\u4e2d\u7684 bAlt \u548c bCtl \u70ba\u6574\u9ad4\u8b8a\u6578\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cpp\" data-line=\"\">bool bAlt = false;\nbool bCtl = false;<\/code><\/pre>\n<p>&nbsp;<br \/>\n<kbd>Alt<\/kbd> \u9375\u5728 VC \u88e1\u88ab\u5b9a\u7fa9\u70ba VK_MENU\uff0c\u800c\u4e14\u6309\u4e0b\u4e86 <kbd>Alt<\/kbd> \u7684\u8a71\uff0cpMsg-&gt;message \u5f97\u5230\u7684\u662f WM_SYSKEYDOWN\uff0c\u4e26\u975e\u5e38\u898b\u7684 WM_KEYDOWN<br \/>\n&nbsp;<br \/>\n<kbd>Ctrl<\/kbd> \u5c31\u8ddf\u6b63\u5e38\u7684\u9375\u4e00\u6a23 pMsg-&gt;message \u662f WM_KEYDOWN\u3001WM_KEYUP\uff1bpMsg-&gt;wParam \u70ba VK_CONTROL<br \/>\n&nbsp;<br \/>\n\u6216\u8005\u4e5f\u53ef\u4ee5\u4f7f\u7528 <i>if (::GetKeyState(VK_CONTROL) &lt; 0)<\/i> \u4f86\u6aa2\u67e5 <kbd>Ctrl<\/kbd> \u9375\u662f\u5426\u6309\u4e0b\u53bb<br \/>\n&nbsp;<br \/>\n\u6700\u5f8c\u6709\u500b\u7c21\u55ae\u7684\u7bc4\u4f8b\uff0c\u8868\u73fe\u51fa\u6309\u4e0b <kbd>Alt<\/kbd>+<kbd>F5<\/kbd> \u7684\u8a71\uff0c\u5c0d\u8a71\u76d2\u5c31\u6703\u7d50\u675f\u2026\u2026\u800c\u7cfb\u7d71\u5167\u5efa\u7684 <kbd>Alt<\/kbd>+<kbd>F4<\/kbd> \u7d50\u675f\u7576\u7136\u4e5f\u9084\u5728<br \/>\n&nbsp;<br \/>\n(\u4e5f\u53ef\u76f4\u63a5\u4e0b\u8f09\u7bc4\u4f8b <a href=\"\/wp\/wp-content\/uploads\/DlgKeystroke-0.002.zip\">DlgKeystroke-0.002.zip<\/a>)<br \/>\n&nbsp;<\/p>\n<p>&nbsp;<br \/>\nref:<br \/>\n\u2022 <a href=\"https:\/\/www.codeproject.com\/Articles\/1619\/Keyboard-messages-accelerators-handling-in-MFC-dia\" target=\"_blank\" rel=\"noopener\"><i>Keyboard messages\/accelerators handling in MFC dialog based applications<\/i><\/a><br \/>\n\u2022 <a href=\"https:\/\/stackoverflow.com\/questions\/48259255\/handling-accelerator-in-a-dialog\" target=\"_blank\" rel=\"noopener\"><i>Handling accelerator in a dialog<\/i><\/a><br \/>\n&nbsp;<\/p>\n<p>#mfc #c++ #programming<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u5efa\u7acb\u4e00\u500b MFC \u5c0d\u8a71\u76d2\u7a0b\u5f0f (\u53ef\u76f4\u63a5\u4e0b\u8f09\u9019\u908a\u7684\u7bc4\u4f8b DlgKeystroke-0.001.zip) &#038;n [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[212],"tags":[27,28,36],"class_list":["post-1451","post","type-post","status-publish","format-standard","hentry","category-coding","tag-coding","tag-mfc","tag-windows"],"_links":{"self":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1451","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1451"}],"version-history":[{"count":26,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1451\/revisions"}],"predecessor-version":[{"id":3396,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1451\/revisions\/3396"}],"wp:attachment":[{"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1451"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1451"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guerilla.ddns.net\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1451"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}