MFC:在OnInitDialog 里面关闭窗体

解决步骤

  1. 在对应的dialogcpp 文件里面的在OnInitDialog函数里面,找到对应的位置,您需要结束窗体显示的地方。(感觉这是废话)
  2. 经过验证,使用EndDialog(IDCANCEL);//这一句就起到了效果

代码片段如下

BOOL CMy001_CloseDialogOnInitDialogDlg::OnInitDialog()
{
	int nMERet = IDCANCEL;

	CDialogEx::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	/*
	在这儿进行一些验证性的工作,通过则显示窗体,失败则退出	
	*/
	
	nMERet = MessageBox(_T("验证窗体,点击OK显示窗体,点击Cancel关闭窗体"),
		_T("验证窗体"),
		MB_OKCANCEL);
	if ( nMERet != IDOK )
	{
		EndDialog(IDCANCEL);//这一句就起到了效果
		return FALSE;//同时,我喜欢加上这一句
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

MFC题外话

其实不想记录这个问题的,因为没有什么实质性的价值。在网上找找就可以找到的。但不知为什么,这个问题确出现在了我的记录里面,也许是因为现在已经知道了。我记得当初我是废了一些时间的。无语,这么纠结。希望能帮助有用的人吧。不过MFC这东西,说真的,不太喜欢。就是不喜欢,没有为什么,不喜欢,不喜欢,就是不喜欢。

参考文献

  1. C bbs论坛

原文地址:https://www.cnblogs.com/zi-xing/p/5332939.html