本文实例讲述了CWnd窗口框架,分享给大家供大家参考。具体分析如下:
.h头文件代码如下:
class CMyApp:public CWinApp
{
public:
virtual BOOL InitInstance();
};
//CMainWindow
class CMainWindow:public CWnd
{
public:
CMainWindow();
~CMainWindow();
protected:
afx_msg void OnNcDestroy( );
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnDestroy( );
DECLARE_MESSAGE_MAP()
};
.cpp源文件代码如下:
#include "resource.h"
CMyApp theApp;
BOOL CMyApp::InitInstance()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow(m_nCmdShow);
return TRUE; //必须返回TRUE,否则不会进入消息循环,界面会直接退出
}
//CMainWindow
BEGIN_MESSAGE_MAP(CMainWindow, CWnd)
ON_WM_CREATE()
END_MESSAGE_MAP()
//构造函数
CMainWindow::CMainWindow()
{
LPCTSTR lpszClassName = ::AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW, ::LoadCursorA(NULL, IDC_ARROW), (HBRUSH)(COLOR_3DFACE+1), theApp.LoadIcon(IDI_MAIN));
CreateEx(WS_EX_CLIENTEDGE, lpszClassName, "xxx", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL);
}
//析构函数
CMainWindow::~CMainWindow()
{
}
//消息映射函数
int CMainWindow::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
//OutputDebugString("oncreate");
//设置菜单
HMENU hMenu = ::LoadMenuA(theApp.m_hInstance, (LPCSTR)IDR_MENU);
::SetMenu(m_hWnd, hMenu);
return 0;
}
void CMainWindow::OnNcDestroy( )
{
delete this;
}
void CMainWindow::OnDestroy()
{
}
希望本文所述对大家的C++程序设计有所帮助。