C++之CWnd窗口框架实例
投稿:shichen2014
这篇文章主要介绍了C++之CWnd窗口框架,实例分析了CWnd窗口框架的结构,如构造函数、析构函数、消息映射函数等,需要的朋友可以参考下
本文实例讲述了CWnd窗口框架,分享给大家供大家参考。具体分析如下:
.h头文件代码如下:
复制代码 代码如下:
#include <afxwin.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()
};
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 "ReadBMP.h"
#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()
{
}
#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++程序设计有所帮助。