{"id":197,"date":"2017-09-20T22:20:16","date_gmt":"2017-09-20T14:20:16","guid":{"rendered":"http:\/\/kogasa.moe\/?p=197"},"modified":"2018-04-29T13:34:18","modified_gmt":"2018-04-29T05:34:18","slug":"duilib%e6%97%a0%e6%b3%95%e8%bf%9e%e7%bb%ad%e5%88%9b%e5%bb%ba%e4%b8%a4%e4%b8%aa%e6%a8%a1%e6%80%81%e5%af%b9%e8%af%9d%e6%a1%86","status":"publish","type":"post","link":"https:\/\/flandre-scarlet.moe\/blog\/197\/","title":{"rendered":"Duilib\u65e0\u6cd5\u8fde\u7eed\u521b\u5efa\u4e24\u4e2a\u6a21\u6001\u5bf9\u8bdd\u6846"},"content":{"rendered":"<p>\u8c03\u8bd5\u4e00\u4e2a Duilib \u754c\u9762\u7a0b\u5e8f\u65f6\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u5728\u4e3b\u51fd\u6570\u91cc\u4f7f\u7528 ShowModal() \u663e\u793a\u7b2c\u4e00\u4e2a\u5bf9\u8bdd\u6846\u5e76\u5173\u95ed\u5bf9\u8bdd\u6846\u540e\uff0c\u518d\u4e00\u6b21\u4f7f\u7528 ShowModal() \u663e\u793a\u7b2c\u4e8c\u4e2a\u5bf9\u8bdd\u6846\u65f6\u7b2c\u4e8c\u4e2a\u5bf9\u8bdd\u6846\u4fbf\u76f4\u63a5\u5173\u95ed\u9000\u51fa\u4e86\u3002<br \/>\n<!--more--><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">    CDemoWindow* pFrame1 = new CDemoWindow();\r\n    pFrame1-&gt;Create(NULL, _T(&quot;&quot;), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);\r\n    pFrame1-&gt;CenterWindow();\r\n    pFrame1-&gt;ShowModal();\r\n\r\n    CDemoWindow* pFrame2 = new CDemoWindow();\r\n    pFrame2-&gt;Create(NULL, _T(&quot;&quot;), UI_WNDSTYLE_FRAME, WS_EX_WINDOWEDGE);\r\n    pFrame2-&gt;CenterWindow();\r\n    pFrame2-&gt;ShowModal();\r\n<\/pre>\n<p>\u8c03\u8bd5\u4e86\u4e00\u4e0b\u53d1\u73b0\u7b2c\u4e8c\u4e2a\u5bf9\u8bdd\u6846\u7684\u6d88\u606f\u5faa\u73af\u5f88\u5feb\u6536\u5230\u4e00\u4e2a WM_QUIT \u6d88\u606f\u5bfc\u81f4\u4e86\u5b83\u7684\u9000\u51fa\u3002\u7ee7\u7eed\u8ddf\u8e2a\u53d1\u73b0\u539f\u6765\u662f\u7b2c\u4e00\u4e2a\u5bf9\u8bdd\u6846\u7684 OnDestory() \u91cc\u8c03\u7528\u4e86 PostQuitMesage() \u5f80\u6d88\u606f\u961f\u5217\u91cc\u53d1\u9001\u4e86 WM_QUIT \u6d88\u606f\uff0c\u800c ShowModal() \u91cc\u7684\u6d88\u606f\u5faa\u73af\u7684\u5faa\u73af\u6761\u4ef6\u5199\u7684\u662f<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">    while(::IsWindow(m_hWnd) &amp;&amp; ::GetMessage(&amp;msg, NULL, 0, 0))<\/pre>\n<p>\u5b83\u6700\u7ec8\u662f\u56e0\u4e3a ::IsWindow(m_hWnd) \u4e3a false \u800c\u9000\u51fa\u6d88\u606f\u5faa\u73af\u7684\uff0c MW_QUIT \u6d88\u606f\u4f9d\u7136\u88ab\u7559\u5728\u4e86\u6d88\u606f\u961f\u5217\u4e2d\uff0c\u968f\u540e\u88ab\u4e0b\u4e00\u4e2a\u5bf9\u8bdd\u6846\u6536\u5230\u4e86\u3002<\/p>\n<p>\u4fee\u6539\u65b9\u6cd5\u5c31\u662f\u53bb\u6389 OnDestroy() \u4e2d\u7684 PostQuitMessage() \u8c03\u7528\u3002\u56e0\u4e3a\u7a97\u53e3\u5df2\u7ecf\u9500\u6bc1\uff0c\u6240\u4ee5\u4e0d\u7528 MW_QUIT \u4e5f\u80fd\u6b63\u5e38\u9000\u51fa\u6d88\u606f\u5faa\u73af\u3002<\/p>\n<p>\u53e6\u5916\u5bf9\u6bd4\u4e86\u4e00\u4e0b MFC\uff0c\u5f53\u5728 MFC \u7684\u5e94\u7528\u7a0b\u5e8f\u5411\u5bfc\u4e2d\u9009\u62e9\u201c\u57fa\u4e8e\u5bf9\u8bdd\u6846\u201d\u4f5c\u4e3a\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u540e\uff0c\u5e76\u5728\u4ee3\u7801\u4e2d\u5c06 App::InitInstance() \u4f5c\u5982\u4e0b\u8865\u5145\uff1a<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">BOOL CMFCApplication2App::InitInstance()\r\n{\r\n    INITCOMMONCONTROLSEX InitCtrls;\r\n    InitCtrls.dwSize = sizeof(InitCtrls);\r\n    InitCtrls.dwICC = ICC_WIN95_CLASSES;\r\n    InitCommonControlsEx(&amp;InitCtrls);\r\n\r\n    CWinApp::InitInstance();\r\n    AfxEnableControlContainer();\r\n    CShellManager *pShellManager = new CShellManager;\r\n    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));\r\n\r\n    SetRegistryKey(_T(&quot;\u5e94\u7528\u7a0b\u5e8f\u5411\u5bfc\u751f\u6210\u7684\u672c\u5730\u5e94\u7528\u7a0b\u5e8f&quot;));\r\n    {\r\n        CMFCApplication2Dlg dlg;\r\n        m_pMainWnd = &amp;dlg;\r\n        INT_PTR nResponse = dlg.DoModal();\r\n    }\r\n    {\r\n        CMFCApplication2Dlg dlg;\r\n        m_pMainWnd = &amp;dlg;\r\n        INT_PTR nResponse = dlg.DoModal();\r\n    }\r\n    if (pShellManager != NULL)\r\n    {\r\n        delete pShellManager;\r\n    }\r\n\r\n    \/\/ \u7531\u4e8e\u5bf9\u8bdd\u6846\u5df2\u5173\u95ed\uff0c\u6240\u4ee5\u5c06\u8fd4\u56de FALSE \u4ee5\u4fbf\u9000\u51fa\u5e94\u7528\u7a0b\u5e8f\uff0c\r\n    \/\/  \u800c\u4e0d\u662f\u542f\u52a8\u5e94\u7528\u7a0b\u5e8f\u7684\u6d88\u606f\u6cf5\u3002\r\n    return FALSE;\r\n}\r\n<\/pre>\n<p>\u5373\u5728\u7b2c\u4e00\u4e2a\u5bf9\u8bdd\u6846\u540e\u7acb\u5373\u8c03\u7528\u751f\u6210\u7b2c\u4e8c\u4e2a\u5bf9\u8bdd\u6846\u3002\u7ed3\u679c\u53d1\u73b0\u7b2c\u4e8c\u4e2a\u5bf9\u8bdd\u6846\u7684 DoModal() \u4e5f\u662f\u7acb\u523b\u9000\u51fa\u4e86\u3002\u8ddf\u8e2a\u4e86\u4e00\u4e0b\u4ee3\u7801\u53d1\u73b0\u5728 RunModalLoop() \u7684\u6d88\u606f\u5faa\u73af\u91cc\u540c\u6837\u6536\u5230\u4e86 WM_QUIT\u3002\u7ee7\u7eed\u8ddf\u8e2a\u4e86\u4e00\u4e0b\uff0c\u53d1\u73b0\u7b2c\u4e00\u4e2a\u5bf9\u8bdd\u6846\u5728\u6536\u5230\u6700\u540e\u4e00\u4e2a\u6d88\u606f WM_NCDESTORY \u540e\u8c03\u7528\u4e86 AfxPostQuitMessage(0)\uff0c\u585e\u4e86\u4e2a MW_QUIT \u5230\u6d88\u606f\u961f\u5217\u91cc\u3002<\/p>\n<p>\u4f46\u662f\u5982\u679c\u5728\u5411\u5bfc\u4e2d\u4e0d\u9009\u62e9\u201c\u57fa\u4e8e\u5bf9\u8bdd\u6846\u201d\u7c7b\u578b\u7684\u8bdd\uff0c\u5728\u4ea7\u751f\u4e3b\u7a97\u53e3\u524d\u5148\u4ea7\u751f\u4e00\u4e2a\u6216\u591a\u4e2a\u6a21\u6001\u5bf9\u8bdd\u6846\u5219\u4e0d\u4f1a\u5bfc\u81f4\u968f\u540e\u7684\u4e3b\u7a97\u53e3\u9000\u51fa\u3002\u8fd9\u4e2a\u539f\u56e0\u51fa\u5728\u7a97\u53e3\u5bf9 WM_NCDESTORY \u7684\u5904\u7406\u4e0a\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void CWnd::OnNcDestroy()\r\n{\r\n    \/\/ cleanup main and active windows\r\n    CWinThread* pThread = AfxGetThread();\r\n    if (pThread != NULL)\r\n    {\r\n        if (pThread-&gt;m_pMainWnd == this)    \/\/ \u5173\u952e\u5728\u8fd9\u91cc\r\n        {\r\n            if (!afxContextIsDLL)\r\n            {\r\n                \/\/ shut down current thread if possible\r\n                if (pThread != AfxGetApp() || AfxOleCanExitApp())\r\n                    AfxPostQuitMessage(0);\r\n            }\r\n            pThread-&gt;m_pMainWnd = NULL;\r\n        }\r\n        if (pThread-&gt;m_pActiveWnd == this)\r\n            pThread-&gt;m_pActiveWnd = NULL;\r\n    }\r\n\r\n    \/\/ \u540e\u9762\u7701\u7565\r\n    \/\/ ...\r\n}\r\n<\/pre>\n<p>\u5176\u4e2d\u6709\u4e00\u4e2a\u5f53\u524d\u7a97\u53e3\u662f\u5426\u662f\u4f5c\u4e3a\u5f53\u524d\u7ebf\u7a0b\u7684\u4e3b\u7a97\u53e3\u7684\u5224\u65ad\uff0c\u5982\u679c\u662f\u7684\u8bdd\u624d\u53d1\u9001 WM_QUIT \u6d88\u606f\u3002\u800c\u57fa\u4e8e\u5bf9\u8bdd\u6846\u7c7b\u578b\u7684\u7a0b\u5e8f\u7684\u8bdd\u5219\u5728\u5bf9\u8bdd\u6846\u58f0\u660e\u4e4b\u540e\u4fbf\u5c06\u5176\u4f5c\u4e3a\u7ebf\u7a0b\u4e3b\u7a97\u53e3\u8bb0\u5f55\u4e0b\u6765\u4e86(\u5bf9\u8bdd\u6846\u90e8\u5206\u4ee3\u7801\u7684 16 \u548c 21 \u884c)\uff0c\u6240\u4ee5\u7a97\u53e3\u9500\u6bc1\u65f6\u4f1a\u6709\u989d\u5916\u7684 WM_QUIT\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8c03\u8bd5\u4e00\u4e2a Duilib \u754c\u9762\u7a0b\u5e8f\u65f6\u9047\u5230\u4e00\u4e2a\u95ee\u9898\uff1a\u5f53\u5728\u4e3b\u51fd\u6570\u91cc\u4f7f\u7528 ShowModal() \u663e\u793a\u7b2c\u4e00\u4e2a\u5bf9\u8bdd\u6846\u5e76\u5173&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/flandre-scarlet.moe\/blog\/197\/\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[11,17],"class_list":["post-197","post","type-post","status-publish","format-standard","hentry","category-debug","tag-duilib","tag-mfc"],"_links":{"self":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/comments?post=197"}],"version-history":[{"count":0,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/posts\/197\/revisions"}],"wp:attachment":[{"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/media?parent=197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/categories?post=197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/flandre-scarlet.moe\/blog\/wp-json\/wp\/v2\/tags?post=197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}