我可以从对话框的DoModal函数返回自定义值吗?

Kar*_*udi 2 c++ windows winapi mfc return-value

我想要做的是,在创建一个对话框后,在框中DoModal()按OK退出它,返回一个自定义值.例如,用户将在对话框中输入的几个字符串.

Cod*_*ray 8

你不能改变DoModal()函数的返回值,即使你可以,我也不推荐它.这不是惯用的方法,如果你将其返回值更改为字符串类型,你将失去查看用户何时取消对话框的能力(在这种情况下,返回的字符串值应该完全被忽略).

相反,添加其他功能(或多个)到您的对话框类,像GetUserName()GetUserPassword,然后经过查询这些功能的价值DoModal回报IDOK.

例如,显示对话框和处理用户输入的函数可能如下所示:

void CMainWindow::OnLogin()
{
    // Construct the dialog box passing the ID of the dialog template resource
    CLoginDialog loginDlg(IDD_LOGINDLG);

    // Create and show the dialog box
    INT_PTR nRet = -1;
    nRet = loginDlg.DoModal();

    // Check the return value of DoModal
    if (nRet == IDOK)
    {
        // Process the user's input
        CString userName = loginDlg.GetUserName();
        CString password = loginDlg.GetUserPassword();

        // ...
    }
}
Run Code Online (Sandbox Code Playgroud)