我能想到两种方法来做到这一点:
无模式对话框
https://www.codeproject.com/Articles/1651/Tutorial-Modeless-Dialogs-with-MFC
用户线程(UI线程)。
使用CWinThread创建主UI线程(CWinApp)的一个兄弟线程。最重要的是将CWinThread::m_pMainWnd成员指定为指向对话框的指针。如果对话框是模态的,请在调用DoModal后返回FALSE,如果对话框是非模态的,则返回TRUE。
class CMainFrame : public CFrameWnd {
// pointer to thread
CWinThread* m_pUserThread;
}
开始线程。
m_pUserThread = AfxBeginThread(RUNTIME_CLASS(CUserThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED );
m_pUserThread->m_bAutoDelete = TRUE;
m_pUserThread->ResumeThread();
"头文件"
class CUserThread : public CWinThread
{
DECLARE_DYNCREATE(CUserThread)
public:
CUserThread();
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CUserThread)
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL
protected:
virtual ~CUserThread();
// Generated message map functions
//{{AFX_MSG(CUserThread)
// NOTE - the ClassWizard will add and remove member functions here.
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CUserMsg* m_pDlg;
}
源文件
#define DO_MODAL
BOOL CUserThread::InitInstance()
{
#ifdef DO_MODAL
CUserMsg dlg;
m_pMainWnd = &m_pDlg;
dlg.DoModal();
return FALSE;
#endif
#ifdnef DO_MODAL
m_pDlg = new CUserMsg();
m_pDlg->Create( IDD_USER_DLG, AfxGetMainWnd() );
m_pMainWnd = m_pDlg;
return TRUE;
#endif
}
int CUserThread::ExitInstance()
{
m_pDlg->SendMessage( WM_CLOSE ) ;
delete m_pDlg;
return CWinThread::ExitInstance();
}
终止线程
// post quit message to thread
m_pUserThread->PostThreadMessage(WM_QUIT,0,0);
// wait until thread termineates
::WaitForSingleObject(m_pUserThread->m_hThread,INFINITE);
我强烈建议两种方式都将对话框设置为最顶层窗口:
BOOL CLicenseGenDlg::OnInitDialog() {
CDialog::OnInitDialog();
// TODO: Add extra initialization here
SetWindowPos( &wndTopMost, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | WS_EX_TOPMOST );
return TRUE;
}
delete inProcess;
这段代码可以编译吗?无论如何,都不要使用它 - 它没有意义。 - Igor TandetnikWM_USER
属于窗口类实现。在这种特殊情况下,是一些对话框窗口类。如果你想发送应用程序私有消息,需要使用WM_APP + x
消息范围。 - IInspectable