1、对话框用 CDialog::OnOk();
Doc/View用 OnClose();
2、 PostQuitMessage(0);//最常用
PostMessage(WM_QUIT,0,0);//最常用
::SendMessage(AfxGetMainWnd()->m_hWnd,WM_CLOSE,0,0);//最常用
::PostMessage(AfxGetMainWnd()->m_hWnd,WM_CLOSE,0,0);//最常用
3、ExitProcess(0);注意使用时先释放分配的内存,以免造成内存泄露
4、exit(0) 正常终止程序,有可能会有内存泄露; exit(非0)非正常终止程序
5、OnClose();
void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default
if (MessageBox("确定要退出吗?","提示",MB_YESNO|MB_DEFBUTTON2)==IDYES)
{
CFrameWnd::OnClose();
}
}
正常情况下通过关闭主窗口close或发送PostQuitMessage(WM_QUIT)等消息能关闭窗口。但如果一个进程中包括多个子线程当子线程没有结束还占用系统资源时通过上面的方法是不能立即结束主进程的。
//强行结束自己
void TerminateApp()
{
HANDLE hself = GetCurrentProcess();
TerminateProcess(hself, 0);
}