主窗口作为无模式对话框的 MFC 应用程序

发布于 2024-11-17 17:42:20 字数 831 浏览 2 评论 0原文

我正在开发一个MFC应用程序并将其导出到dll中。该应用程序只有一个窗口,我希望该窗口是无模式的。在 InitInstance() 内部,如果我希望它是模态的,我只需要这样做:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}
return false;

它作为模态工作得很好。这是无模式代码:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg * dlg;
dlg=new CUIWelcomeDlg();
m_pMainWnd=dlg;
if(dlg!=NULL) {
    dlg->Create(IDD_UIWELCOME_DIALOG,NULL);
    dlg->ShowWindow(SW_SHOW);
} 
return true;

我尝试调试它。直到达到return true就可以了;之后,对话框窗口冻结并且没有响应。有谁知道如何解决这个问题?

I am developing an MFC application and exporting it into dll. The application has only one window, and I want that window modeless. Inside InitInstance(), if I want it to be modal, I only need to do this:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg dlg;
m_pMainWnd = &dlg;
INT_PTR nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with OK
}
else if (nResponse == IDCANCEL)
{
    // TODO: Place code here to handle when the dialog is
    //  dismissed with Cancel
}
return false;

It works just fine as a modal. This is the code for the modeless one:

AFX_MANAGE_STATE(AfxGetStaticModuleState());
CUIWelcomeDlg * dlg;
dlg=new CUIWelcomeDlg();
m_pMainWnd=dlg;
if(dlg!=NULL) {
    dlg->Create(IDD_UIWELCOME_DIALOG,NULL);
    dlg->ShowWindow(SW_SHOW);
} 
return true;

I tried to debug it. It is fine until it reaches return true; After that, the dialog window freezes and is not responding. Does anyone know how to fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恏ㄋ傷疤忘ㄋ疼 2024-11-24 17:42:20

尝试删除以下行:
m_pMainWnd = dlg;

(如果这里 dlg 是一个指针,则应该将其称为 pdlg)。

Try to remove the following line:
m_pMainWnd = dlg;

(if dlg is a pointer here, you should call it pdlg).

一指流沙 2024-11-24 17:42:20

您需要实现自己的无限循环。当然,您不想停止 UI 线程进行响应,因此您需要在此循环内捕获和分派消息。尝试在 ShowWindow 之后添加以下内容:

MSG msg;

// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
    if(!IsDialogMessage(&msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);  
    }
}

You need to implement your own endless loop. Of course you don't want to stop the UI thread to be responsive so you need to capture and dispatch message inside this loop. Try adding this after ShowWindow:

MSG msg;

// Handle dialog messages
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
    if(!IsDialogMessage(&msg))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);  
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文