获取对话窗口处理程序

时间:2014-10-22 16:30:29

标签: visual-c++ mfc

我有基于MFC对话框的窗口应用程序。主对话框表单创建显示在下面的代码中。我有一些代码在单独的线程上运行,有时我需要将消息发送到对话框窗口。为此,我需要窗口处理程序。

行MyAppDlg.GetSafeHwnd()返回0.为什么?如何获得对话窗口处理程序?

BOOL CMyApp::InitInstance()
{
    CWinApp::InitInstance();

    // Activate "Windows Native" visual manager for enabling themes in MFC controls
    CMFCVisualManager::SetDefaultManager(RUNTIME_CLASS(CMFCVisualManagerWindows));

    startAll(NULL);             

    CMyAppDlg MyAppDlg;         

    m_pMainWnd = &MyAppDlg;
    m_pActiveWnd = &MyAppDlg;

    AuthMsgHWND = MyAppDlg.GetSafeHwnd();       

    INT_PTR nResponse = MyAppDlg.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
    }
    else if (nResponse == -1)
    {
        TRACE(traceAppMsg, 0, "Warning: dialog creation failed, so application is terminating unexpectedly.\n");
        TRACE(traceAppMsg, 0, "Warning: if you are using MFC controls on the dialog, you cannot #define _AFX_NO_MFC_CONTROLS_IN_DIALOGS.\n");
    }

    // Since the dialog has been closed, return FALSE so that we exit the
    //  application, rather than start the application's message pump.
    return FALSE;       
}

2 个答案:

答案 0 :(得分:1)

在使用HWND创建对话框之前,您已尝试获取对象的DoModal - 这不会起作用。由于DoModal在对话框被销毁之前不会返回,因此您无法执行此操作。您必须找到另一个可以捕获该句柄的点。

P.S。不要从另一个帖子中调用SendMessage。你在寻找麻烦。请改用PostMessage

答案 1 :(得分:1)

已创建对话框对象,但在调用DoModal之前,不会创建对话框窗口(及其HWND)。您可以访问此HWND的第一个位置是对话框的OnInitDialog函数。