MFC modeless dialog close immediately(MFC 无模式对话框立即关闭)
问题描述
我喜欢编写基于无模式对话框的应用程序,但我遇到了问题.当程序启动时,窗口立即关闭.
I like to write a modeless dialog based app, but I have a problem. When the program starts, the window close immediately.
当我创建一个模态对话框时,相同的代码可以正常工作.(DoModal())
The same code works fine when I make a modal dialog. (DoModal())
Csetkliens.h
#pragma once
#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
#include "CsetkliensDlg.h"
class CCsetkliensApp : public CWinApp
{
public:
CCsetkliensApp();
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
private:
CCsetkliensDlg* dlg;
};
extern CCsetkliensApp theApp;
Csetkliens.cpp
#include "stdafx.h"
#include "Csetkliens.h"
#include "CsetkliensDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(CCsetkliensApp, CWinApp)
ON_COMMAND(ID_HELP, &CWinApp::OnHelp)
END_MESSAGE_MAP()
CCsetkliensApp::CCsetkliensApp()
{
dlg = NULL;
}
CCsetkliensApp theApp;
BOOL CCsetkliensApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
CShellManager *pShellManager = new CShellManager;
dlg = new CCsetkliensDlg();
m_pMainWnd = dlg;
dlg->Create(CCsetkliensDlg::IDD);
dlg->ShowWindow(SW_SHOW);
if (pShellManager != NULL)
{
delete pShellManager;
}
return FALSE;
}
CsetkliensDlg.h
#pragma once
#include "ConnectDlg.h"
class CCsetkliensDlg : public CDialogEx
{
public:
CCsetkliensDlg(CWnd* pParent = NULL);
enum { IDD = IDD_CSETKLIENS_DIALOG };
protected:
virtual BOOL OnInitDialog();
DECLARE_MESSAGE_MAP()
};
CsetkliensDlg.cpp
#include "stdafx.h"
#include "Csetkliens.h"
#include "CsetkliensDlg.h"
#include "afxdialogex.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
CCsetkliensDlg::CCsetkliensDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CCsetkliensDlg::IDD, pParent)
{
}
BEGIN_MESSAGE_MAP(CCsetkliensDlg, CDialogEx)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
BOOL CCsetkliensDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
return TRUE;
}
推荐答案
从应用程序类的 FALSE/ae6yx0z0.aspx" rel="nofollow">InitInstance 方法 告诉 MFC 初始化失败,应用程序应该终止.
Returning FALSE from your application class's InitInstance method tells MFC that initialization failed and the application should terminate.
将喜欢的内容更改为 return TRUE; 一切都会正常.
Change that like to return TRUE; and everything should work fine.
BOOL CCsetkliensApp::InitInstance()
{
INITCOMMONCONTROLSEX InitCtrls;
InitCtrls.dwSize = sizeof(InitCtrls);
InitCtrls.dwICC = ICC_WIN95_CLASSES;
InitCommonControlsEx(&InitCtrls);
CWinApp::InitInstance();
if (!AfxSocketInit())
{
AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
return FALSE;
}
CShellManager *pShellManager = new CShellManager;
dlg = new CCsetkliensDlg();
m_pMainWnd = dlg;
dlg->Create(CCsetkliensDlg::IDD);
dlg->ShowWindow(SW_SHOW); // this is not a blocking call!
if (pShellManager != NULL)
{
delete pShellManager;
}
return TRUE; // change this one!
}
它与模态对话框一起工作的原因(通过调用 DoModal 方法显示)是因为模态对话框创建了自己的消息循环,该循环一直运行到您关闭对话框为止.这意味着执行在 DoModal 调用处有效地阻塞",而不会将控制权返回给您的 InitInstance 方法,因此它不会返回 FALSE 和 MFC不退出.至少在您关闭对话框之前不会,在这种情况下,您希望它退出,所以一切看起来都正常.
The reason that it works with a modal dialog (shown by calling the DoModal method) is because a modal dialog creates its own message loop, which runs until you close the dialog. That means that execution effectively "blocks" at the DoModal call without returning control to your InitInstance method, so it doesn't return FALSE and MFC doesn't quit. At least not until you close the dialog, in which case you want it to quit, so everything appears the work.
这篇关于MFC 无模式对话框立即关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MFC 无模式对话框立即关闭
基础教程推荐
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
