gpt4 book ai didi

winapi - CDialog - 来自无模式对话框的 EndDialog?

转载 作者:行者123 更新时间:2023-12-02 00:09:19 25 4
gpt4 key购买 nike

MS documentation (和 others )“明确”指出:

... Because the normal OnOk and OnCancel member functions of a CDialog object would call EndDialog, make sure your modeless dialog box does not call those functions and instead overrides

由于 CDialog::OnOk 有效地调用了 CDialog::EndDialog,该方法如下所示:

void CDialog::EndDialog(int nResult)
{
ASSERT(::IsWindow(m_hWnd));

if (m_nFlags & (WF_MODALLOOP|WF_CONTINUEMODAL))
EndModalLoop(nResult);

::EndDialog(m_hWnd, nResult);
}

我们还可以检查the docs for ::EndDialog再次“明确”声明:

Dialog boxes created by the DialogBox, DialogBoxParam, DialogBoxIndirect, and DialogBoxIndirectParam functions must be destroyed using the EndDialog function. An application calls EndDialog from within the dialog box procedure; the function must not be used for any other purpose.

然而,我有一个 CDialog 派生类,它具有默认行为 wrt。 OnOK并且似乎当我使用非模态/无模态时一切正常。

即:* 当我关闭(无模式)对话框时,它被关闭/从 View 中删除。* 应用程序没有显示任何内存泄漏。 (MFC 调试构建)

那又怎样?我是否需要自己阻止 EndDialog 并调用 DestroyWindow 或不需要


注意:我知道文档和“网络”是怎么说的。只是我还没有找到为什么我需要做不同的事情,这个类应该可用于无模式和模态模式,所以不必做任何不同的事情可能会很方便。 p>

最佳答案

MSDN Docs对于 CDialog::OnOK 明确指出

If you implement the OK button in a modeless dialog box, you must override the OnOK method and call DestroyWindow inside it. Do not call the base-class method, because it calls EndDialog which makes the dialog box invisible but does not destroy it

因此您需要覆盖 CDialog::OnOK 并在内部调用 DestroyWindow() -- 这是来自 MSDN 的修改示例:

class CDlg : public CDialog
{
...
BOOL m_bModal;
...
}

CDlg::CDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDlg::IDD, pParent)
{
...
m_bModal = FALSE;
...
}

INT_PTR CDlg::DoModal()
{ m_bModal = TRUE;
const INT_PTR rval = CDialog::DoModal();
m_bModal = FALSE;
return rval;
}

void CDlg::OnOK()
{
if (!UpdateData(TRUE))
{
TRACE(_T("UpdateData failed during dialog termination\n"));
// The UpdateData routine will set focus to correct item
return;
}
if (m_bModal)
EndDialog(IDOK);
else
DestroyWindow();
}

void CDlg::OnCancel()
{
if (m_bModal)
EndDialog(IDCANCEL);
else
DestroyWindow();
}

关于winapi - CDialog - 来自无模式对话框的 EndDialog?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16359005/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com