本文執行個體講述了C++基於對話方塊的程式的架構。分享給大家供大家參考。具體如下:
resource.cpp源檔案如下:
複製代碼 代碼如下:
#include "resource.h"
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
CMainDialog dlg;
m_pMainWnd = &dlg; //給m_pMainWnd 主視窗
dlg.DoModal();
return FALSE; //不進入訊息迴圈
}
BEGIN_MESSAGE_MAP(CMainDialog, CDialog)
ON_BN_CLICKED(IDC_STOP, OnStop)
ON_MESSAGE(WM_CUTTERSTART, OnCutterStart) //自訂訊息
END_MESSAGE_MAP()
//CMainDialog
CMainDialog::CMainDialog(CWnd* pParentWnd):CDialog(IDD_MAIN, pParentWnd)
{
}
BOOL CMainDialog::OnInitDialog( )
{
CDialog::OnInitDialog();
return TRUE;
}
void CMainDialog::OnStop()
{
MessageBox("OnStop");
}
long CMainDialog::OnCutterStart(WPARAM wParam, LPARAM lParam) //處理自訂訊息
{
MessageBox("OnCutterStart");
return 0;
}
resource.h標頭檔如下:
複製代碼 代碼如下:
#include <afxwin.h>
#define WM_CUTTERSTART WM_USER+100
//CMyApp
class CMyApp:public CWinApp
{
public:
BOOL InitInstance();
};
//CMyDialog
class CMainDialog:public CDialog
{
public:
CMainDialog(CWnd* pParentWnd = NULL);
protected:
virtual BOOL OnInitDialog( );
afx_msg void OnStop();
afx_msg long OnCutterStart(WPARAM wParam, LPARAM lParam); //處理自訂訊息的聲明
DECLARE_MESSAGE_MAP()
};
希望本文所述對大家的C++程式設計有所協助。