dialog.cpp

来自「MFC中对话框的使用」· C++ 代码 · 共 89 行

CPP
89
字号
#include <afxwin.h>
#include "resource.h"

class CApp : public CWinApp
{
public:
	virtual InitInstance();
};

class CWindow : public CFrameWnd
{
	CMenu *menu;
public:
	CWindow();
	afx_msg void OnPrompt();
	afx_msg void OnExit();
	DECLARE_MESSAGE_MAP()
};

class CPromptDialog : public CDialog
{
private:
	CString inputString;
public:   
    CPromptDialog(CString initString=NULL,CWnd *pParentWnd=NULL) 
		: CDialog(IDC_DIALOG1 , pParentWnd)
	{	
	   inputString=initString; 
	}

	virtual void OnOk();
	virtual BOOL OnInitDialog();
	CString & GetInputString()
	{ return inputString; }
};

void CPromptDialog ::OnOk()
{
	GetDlgItemText(IDC_EDIT1,
		inputString.GetBuffer(100),100);
	inputString.ReleaseBuffer();
	EndDialog(IDOK);
}

BOOL CPromptDialog ::OnInitDialog()
{
	SetDlgItemText(IDC_EDIT1,inputString);
	return TRUE;
}

void CWindow :: OnPrompt()
{
	CPromptDialog promptDialog("initial string",
		this);
	if (promptDialog.DoModal() == IDOK)
	{
		MessageBox(promptDialog.GetInputString(),
			"String Entered", MB_ICONINFORMATION);
	}
}

void CWindow ::OnExit()
{
	DestroyWindow();
}

CApp App;

CWindow ::CWindow()
{
	Create(NULL,"Simple Custom Dialog",
		WS_OVERLAPPEDWINDOW,
		rectDefault,NULL,
		MAKEINTRESOURCE(IDR_MENU1));
}

BOOL CApp ::InitInstance()
{
	m_pMainWnd = new CWindow();
    m_pMainWnd->ShowWindow(m_nCmdShow);
	m_pMainWnd->UpdateWindow();
	return TRUE;
}


BEGIN_MESSAGE_MAP(CWindow,CFrameWnd)
ON_COMMAND(IDM_FILE_PROMPT,OnPrompt)
ON_COMMAND(IDM_FILE_EXIT,OnExit)
END_MESSAGE_MAP()

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?