2014-10-22 2 views
1

MFC 대화 상자 기반 창 응용 프로그램이 있습니다. 기본 대화 상자 양식 작성은 아래 코드와 같습니다. 별도의 스레드에서 실행되는 일부 코드가 있으며 때로는 대화 상자 창에 메시지를 보내야합니다. 이를 위해 창 핸들러가 필요합니다.대화 상자 창 핸들러 가져 오기

줄 MyAppDlg.GetSafeHwnd()는 0을 반환합니다. 왜? 대화 상자 창 핸들러를 얻는 방법? 작동하지 않습니다 - 대화가 DoModal로 생성되기 전에

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;  
} 

답변

1

DoModal이 호출 될 때까지 대화 상자 개체가 만들어졌지만 대화 상자 창 (및 해당 HWND)이 만들어지지 않았습니다. 대화 상자의 OnInitDialog 함수에이 HWND에 액세스 할 수있는 첫 번째 위치가 있습니다.

1

당신은 개체의 HWND를 얻기 위해 시도했습니다. DoModal은 대화 상자가 소멸 될 때까지 돌아 오지 않으므로 이후에 작업을 수행 할 수 없습니다. 핸들을 캡처 할 수있는 다른 지점을 찾아야합니다.

P. 다른 스레드에서 SendMessage으로 전화하지 마십시오. 당신은 문제를 요구하고 있습니다. 대신 PostMessage을 사용하십시오.