看书上的一个例子,大概思想是在主界面出现之前有一个欢迎界面(CWelcomeDlg)
这个CWelcomeDlg执行一段时间到接受到消息,后跳转到主界面
在CWelcomeDlg中
nwait 是全局变量
UINT ThreadProc(LPVOID param);
UINT ThreadProc(LPVOID param)
{
nwait=0;
while(nwait<500)
{
::InterlockedDecrement((long *)&nwait);
::Sleep(10);
}
that->SendMessage(WM_CLOSE,0,0);
return 0;
}BOOL CWelcomeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
that=this;
HWND hwnd=this->GetSafeHwnd();
AfxMessageBox("启动线程在运行中");
AfxBeginThread(ThreadProc,hwnd,THREAD_PRIORITY_NORMAL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}void CWelcomeDlg::OnClose()
{
AfxMessageBox("启动线程已经完成,主程序尚在运行中");
OnOK();
CDialog::OnClose();
}
在BOOL CThreadApp::InitInstance()函数中
{
AfxEnableControlContainer();
Enable3dControls();
Enable3dControlsStatic();
//下面是自己添加的
CWelcomeDlg dlgwelcome;
dlgwelcome.DoModal();
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo)) return FALSE;
//到这为止。
//不知道怎么跳转到下面的
CThreadDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.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
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
我自己跟书上的例子有点不一样,当 CWelcomeDlg 条用OnClose()函数后 我怎么感觉跟顺序执行没什么区别啊
而且不知道怎么往下执行,因为我的代码就无法进入主对话框。
感觉不会问问题,没说清楚,请各位指教了
这个CWelcomeDlg执行一段时间到接受到消息,后跳转到主界面
在CWelcomeDlg中
nwait 是全局变量
UINT ThreadProc(LPVOID param);
UINT ThreadProc(LPVOID param)
{
nwait=0;
while(nwait<500)
{
::InterlockedDecrement((long *)&nwait);
::Sleep(10);
}
that->SendMessage(WM_CLOSE,0,0);
return 0;
}BOOL CWelcomeDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
that=this;
HWND hwnd=this->GetSafeHwnd();
AfxMessageBox("启动线程在运行中");
AfxBeginThread(ThreadProc,hwnd,THREAD_PRIORITY_NORMAL);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}void CWelcomeDlg::OnClose()
{
AfxMessageBox("启动线程已经完成,主程序尚在运行中");
OnOK();
CDialog::OnClose();
}
在BOOL CThreadApp::InitInstance()函数中
{
AfxEnableControlContainer();
Enable3dControls();
Enable3dControlsStatic();
//下面是自己添加的
CWelcomeDlg dlgwelcome;
dlgwelcome.DoModal();
CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo)) return FALSE;
//到这为止。
//不知道怎么跳转到下面的
CThreadDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.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
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
我自己跟书上的例子有点不一样,当 CWelcomeDlg 条用OnClose()函数后 我怎么感觉跟顺序执行没什么区别啊
而且不知道怎么往下执行,因为我的代码就无法进入主对话框。
感觉不会问问题,没说清楚,请各位指教了
解决方案 »
- 怎样用wingdows SDK7.0编程
- 请问怎么做一个透明的HTML页面?
- C++基础问题
- 谁有飞鸽传书能在vc2005下正常编译的代码?
- 为什么调用CreateDispatch总是不成功呢?
- 请问:怎样获取硬盘的物理序列号和CPU的序列号?
- ++++立即结帖:VC调试环境、BoundsChecker基础应用之基础问题++++
- mfc断点,在mfc中打断点,本来是在上一行打的断点,在运行按f5的时候却跑到了下一行或者下几行去了,请教这是神马原因?
- 关于vc2008在unicode项目中boost正则表达式使用问题?
- 热烈讨论:你用VC开发一般软件吗?你认为VC用来于什么?
- 怎么关闭时间控件
- CListBox::FindString( int nStartAfter, LPCTSTR lpszItem
书上的例子实现了什么?你的疑惑又是什么?上面的代码是不是要实现:在主窗口之前先显示一个对话框,并在这个对话框的初始化函数里创建一个线程,
这个线程若干时间(你的意思是5秒?)后向对话框发送关闭消息使对话框结束,这时主窗口再显示。
你说进不到主窗口?用户把前面那个对话框关掉也进不到吗?
InterlockedDecrement是不是应该为:InterlockedIncrement呀?
1:CCommandLineInfo cmdInfo;
ParseCommandLine(cmdInfo);
if(!ProcessShellCommand(cmdInfo)) return FALSE;
这几句是SDI里的吧,你说的主界面是这个单文档程序的主界面的话,上面那3句话执行完
主界面就出来了
2:UINT ThreadProc(LPVOID param)
{
nwait=0;
while(nwait <500)
{
::InterlockedDecrement((long *)&nwait); //这里为何是自减?
::Sleep(10);
}
that->SendMessage(WM_CLOSE,0,0); //这里你的创建线程时传了个窗口句柄,你这里的that是怎么得到的
return 0;
}
3:CThreadDlg dlg;这里CThreaddlg 是那个,和你说的那个welcomdlg是一个么
m_pMainWnd = &dlg;
1.InterlockedDecrement是错了 应该改为InterlockedIncrement
2.程序跟2楼说的意思差不多。我现在改了可以实现主程序对话框。
3.书上的例子是单文档,我的是对话框,所以把那一段添上去了。寒。。都不知道
4.that是CWelcome的对话框指针 taht=this谢谢上面的提示,可以进主程序窗口,现在有个问题,是为什么我的
void CWelcomeDlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
nwait=600;
CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}
按键下去后nwait=600无法强制进入主程序窗口,对线程理解也不深,是思路问题还是?请指教
void ThreadFunc(LPVOID pParam){
CTime time;
CString strTime;
HWND hWnd = (HWND)pParam;
int count = 5,/*设定5s延时*/
i = 0; m_bRun=TRUE;
while(m_bRun)
{
strTime.Format(_T("%d"),count - i);
::SetDlgItemText(hWnd,IDC_TIME,strTime);
Sleep(1000);
i++;
if( i == count )//5秒自动关闭
{
m_bRun = FALSE;
PostMessage(hWnd,WM_CLOSE,0,0);
}
}
}
也就是说你在对话框类里面响应KeyUp或者nChar消息都是没反应的,要想这么
做要在PreTranslateMessage里面自己提取出来