---创建stdafx.h头文件

#include<Windows.h>#include<tchar.h>#include<assert.h>#include"resource.h"

---创建QWnd类==》窗口基类

---QWnd.h文件

#pragmaonce#include"stdafx.h"classQWnd{public:QWnd(void);~QWnd(void);virtualBOOLCreateEx(DWORDdwExStyle,LPCTSTRlpszClassName,LPCTSTRlpszWindowName,DWORDdwStyle,intx,inty,intnWidth,intnHeight,HWNDhWndParent,HMENUnIDorHMenu,LPVOIDlpParam=NULL);virtualBOOLPreCreateWindow(CREATESTRUCT&cs);public:BOOLShowWindow(intnCmdShow);BOOLUpdateWindow();BOOLDestroyWindow();public:staticLRESULTCALLBACKWndProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam);public:virtualLRESULTWindowProc(UINTuMsg,WPARAMwParam,LPARAMlParam);virtualLRESULTDefault(UINTuMsg,WPARAMwParam,LPARAMlParam);public://messagehandlevirtualLRESULTonClose(WPARAMwParam,LPARAMlParam);virtualLRESULTonDestroy(WPARAMwParam,LPARAMlParam);virtualLRESULTonCreate(WPARAMwParam,LPARAMlParam);public:BOOLSubClassWindow(HWNDhWnd);staticQWnd*FromHandle(HWNDhWnd);public:HWNDm_hWnd;WNDPROCm_lpfnOldWndProc;};

---QWnd.cpp文件

#include"QWnd.h"QWnd::QWnd(){m_hWnd=NULL;m_lpfnOldWndProc=NULL;}QWnd::~QWnd(){}LRESULTCALLBACKQWnd::WndProc(HWNDhWnd,UINTuMsg,WPARAMwParam,LPARAMlParam){if(uMsg==WM_CREATE||uMsg==WM_NCCREATE){LPCREATESTRUCTpCs=(LPCREATESTRUCT)lParam;if(pCs){QWnd*qWnd=(QWnd*)pCs->lpCreateParams;//这个就是之前CreateWindowEx传过来的this这里的处理原因是因为CreateWindowEx在SetWindowLong之前所以用这种方式if(qWnd){qWnd->m_hWnd=hWnd;qWnd->m_lpfnOldWndProc=(WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);returnqWnd->WindowProc(uMsg,wParam,lParam);}}}QWnd*qWnd=(QWnd*)GetWindowLong(hWnd,GWL_USERDATA);if(qWnd){returnqWnd->WindowProc(uMsg,wParam,lParam);}return::DefWindowProc(hWnd,uMsg,wParam,lParam);}LRESULTQWnd::WindowProc(UINTuMsg,WPARAMwParam,LPARAMlParam){switch(uMsg){caseWM_CLOSE:{returnonClose(wParam,lParam);}break;caseWM_DESTROY:{returnonDestroy(wParam,lParam);}break;caseWM_CREATE:{returnonCreate(wParam,lParam);}break;}returnDefault(uMsg,wParam,lParam);}LRESULTQWnd::Default(UINTuMsg,WPARAMwParam,LPARAMlParam){if(m_lpfnOldWndProc==QWnd::WndProc){return::DefWindowProc(m_hWnd,uMsg,wParam,lParam);}returnm_lpfnOldWndProc(m_hWnd,uMsg,wParam,lParam);}//msghandleLRESULTQWnd::onClose(WPARAMwParam,LPARAMlParam){returnDefault(WM_CLOSE,wParam,lParam);}LRESULTQWnd::onDestroy(WPARAMwParam,LPARAMlParam){returnDefault(WM_DESTROY,wParam,lParam);}LRESULTQWnd::onCreate(WPARAMwParam,LPARAMlParam){returnDefault(WM_CREATE,wParam,lParam);}BOOLQWnd::SubClassWindow(HWNDhWnd){assert(hWnd);if(m_hWnd==hWnd){returntrue;}m_lpfnOldWndProc=(WNDPROC)GetWindowLong(hWnd,GWL_WNDPROC);if(m_lpfnOldWndProc!=QWnd::WndProc){m_hWnd=hWnd;SetWindowLong(m_hWnd,GWL_USERDATA,(LONG)this);SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)QWnd::WndProc);returnTRUE;}returnFALSE;}QWnd*QWnd::FromHandle(HWNDhWnd){assert(hWnd);QWnd*qWnd=(QWnd*)GetWindowLong(hWnd,GWL_USERDATA);returnqWnd;}BOOLQWnd::PreCreateWindow(CREATESTRUCT&cs){WNDCLASSEXwcex;wcex.cbSize=sizeof(WNDCLASSEX);BOOLbRet=::GetClassInfoEx(cs.hInstance,cs.lpszClass,&wcex);//检查窗口类是否被注册if(bRet){returntrue;}wcex.style=CS_HREDRAW|CS_VREDRAW;wcex.lpfnWndProc=WndProc;wcex.cbClsExtra=0;wcex.cbWndExtra=0;wcex.hInstance=cs.hInstance;wcex.hIcon=(HICON)::LoadIcon(NULL,(LPCTSTR)IDI_ICON1);wcex.hIconSm=(HICON)::LoadIcon(NULL,(LPCTSTR)IDI_ICON1);wcex.hbrBackground=(HBRUSH)::GetStockObject(WHITE_BRUSH);wcex.hCursor=(HCURSOR)::LoadCursor(NULL,IDC_ARROW);wcex.lpszMenuName=NULL;wcex.lpszClassName=cs.lpszClass;bRet=::RegisterClassEx(&wcex);if(!bRet){returnFALSE;}returntrue;}BOOLQWnd::CreateEx(DWORDdwExStyle,LPCTSTRlpszClassName,LPCTSTRlpszWindowName,DWORDdwStyle,intx,inty,intnWidth,intnHeight,HWNDhWndParent,HMENUnIDorHMenu,LPVOIDlpParam){HINSTANCEhInstance=(HINSTANCE)::GetModuleHandle(NULL);assert(hInstance);CREATESTRUCTcs;cs.cx=nWidth;cs.cy=nHeight;cs.dwExStyle=dwExStyle;cs.hInstance=hInstance;cs.hMenu=nIDorHMenu;cs.hwndParent=hWndParent;cs.lpCreateParams=lpParam;cs.lpszClass=lpszClassName;cs.lpszName=lpszWindowName;cs.style=dwExStyle;cs.x=x;cs.y=y;BOOLbRet=PreCreateWindow(cs);if(!bRet){MessageBox(NULL,_T("注册窗口类失败"),_T("注册窗口类"),0);returnfalse;}HWNDhWnd=CreateWindowEx(dwExStyle,lpszClassName,lpszWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,nIDorHMenu,hInstance,this);//这个函数执行完会发送WM_CREATE消息this的作用是因为提前传递if(NULL==hWnd){returnFALSE;}m_hWnd=hWnd;SetWindowLong(m_hWnd,GWL_USERDATA,(LONG)this);m_lpfnOldWndProc=(WNDPROC)GetWindowLong(m_hWnd,GWL_WNDPROC);//原类的处理程序if(m_lpfnOldWndProc!=QWnd::WndProc){//子类化SetWindowLong(m_hWnd,GWL_WNDPROC,(LONG)QWnd::WndProc);WindowProc(WM_CREATE,0,0);//这里重新发送WM_CREATE消息WindowProc(WM_NCCREATE,0,0);//这里重新发送WM_CREATE消息}returnTRUE;}BOOLQWnd::ShowWindow(intnCmdShow){assert(m_hWnd);return::ShowWindow(m_hWnd,nCmdShow);}BOOLQWnd::UpdateWindow(){assert(m_hWnd);return::UpdateWindow(m_hWnd);}BOOLQWnd::DestroyWindow(){assert(m_hWnd);return::DestroyWindow(m_hWnd);}



---创建QWinApp基类==》应用程序基类

---QWinApp.h文件

#pragmaonce#include"QWnd.h"#include"QButton.h"classQMainFrame:publicQWnd{public:QMainFrame();~QMainFrame();public:LRESULTonClose(WPARAMwParam,LPARAMlParam);LRESULTonDestroy(WPARAMwParam,LPARAMlParam);virtualLRESULTonCreate(WPARAMwParam,LPARAMlParam);public:QButton*m_wndButton;};

---QWinApp.cpp文件

#include"QWinApp.h"#include"QGlobal.h"QWinApp::QWinApp(){m_pMainWnd=NULL;g_pWinApp=this;}QWinApp::~QWinApp(){}BOOLQWinApp::InitInstance(){returnTRUE;}BOOLQWinApp::ExitInstance(){returnTRUE;}voidQWinApp::run(){MSGmsg;while(::GetMessage(&msg,NULL,NULL,NULL)){::TranslateMessage(&msg);::DispatchMessage(&msg);}}



---创建应该程序类的全局变量

---创建QGlobal.h头文件

#pragmaonce#include"QWinApp.h"externQWinApp*g_pWinApp;externQWinApp*GlbGetApp();

---创建QGlobal.cpp文件

#include"QGlobal.h"QWinApp*g_pWinApp=NULL;QWinApp*GlbGetApp(){returng_pWinApp;}


---创建QWnd的子类--子控件Button

---创建文件QButton.h

#pragmaonce#include"QWnd.h"classQButton:publicQWnd{public:QButton();~QButton();public:BOOLCreateEx(LPCTSTRlpszWindowName,DWORDdwStyle,intx,inty,intnWidth,intnHeight,HWNDhWndParent,HMENUnIDorHMenu);};


---创建文件QButton.cpp

#include"QButton.h"QButton::QButton(){}QButton::~QButton(){}BOOLQButton::CreateEx(LPCTSTRlpszWindowName,DWORDdwStyle,intx,inty,intnWidth,intnHeight,HWNDhWndParent,HMENUnIDorHMenu){returnQWnd::CreateEx(0,_T("BUTTON"),lpszWindowName,dwStyle,x,y,nWidth,nHeight,hWndParent,nIDorHMenu);}



---创建QWnd的子类---QMainFrame

---创建QMainFrame.h文件

#pragmaonce#include"QWnd.h"#include"QButton.h"classQMainFrame:publicQWnd{public:QMainFrame();~QMainFrame();public:LRESULTonClose(WPARAMwParam,LPARAMlParam);LRESULTonDestroy(WPARAMwParam,LPARAMlParam);virtualLRESULTonCreate(WPARAMwParam,LPARAMlParam);public:QButton*m_wndButton;};

---创建QMainFrame.cpp文件

#include"QMainFrame.h"#defineIDC_BUTTON10001QMainFrame::QMainFrame(){m_wndButton=newQButton();}QMainFrame::~QMainFrame(){}LRESULTQMainFrame::onClose(WPARAMwParam,LPARAMlParam){returnDestroyWindow();}LRESULTQMainFrame::onDestroy(WPARAMwParam,LPARAMlParam){::PostQuitMessage(0);returntrue;}LRESULTQMainFrame::onCreate(WPARAMwParam,LPARAMlParam){if(NULL==m_wndButton->m_hWnd){m_wndButton->CreateEx(_T("www.itxuba.org"),WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,0,0,200,120,m_hWnd,(HMENU)IDC_BUTTON);}returnTRUE;}



---创建QWinApp的子类---QDemowinApp

---创建QDemowinApp.h文件

#pragmaonce#include"QWinApp.h"classQDemowinApp:publicQWinApp{public:QDemowinApp();~QDemowinApp();public:virtualBOOLInitInstance();virtualBOOLExitInstance();};externQDemowinApptheApp;


---创建QDemowinApp.cpp文件

#include"QDemowinApp.h"#include"QMainFrame.h"QDemowinApp::QDemowinApp(){}QDemowinApp::~QDemowinApp(){}QDemowinApptheApp;BOOLQDemowinApp::InitInstance(){QMainFrame*pMainWnd=newQMainFrame();assert(pMainWnd);m_pMainWnd=pMainWnd;BOOLbRet=pMainWnd->CreateEx(0,_T("www.itxueba.org"),_T("www.itxueba.org"),WS_VISIBLE|WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL);if(!bRet){MessageBox(NULL,_T("createwindowfailed!"),_T("createwindow!"),0);}pMainWnd->ShowWindow(SW_SHOW);pMainWnd->UpdateWindow();returntrue;}BOOLQDemowinApp::ExitInstance(){if(m_pMainWnd){deletem_pMainWnd;m_pMainWnd=NULL;}returnTRUE;}








---创建入口程序

#include"QDemowinApp.h"#include"QGlobal.h"intWINAPI_tWinMain(INHINSTANCEhInstance,INHINSTANCEhPrevInstance,INLPTSTRlpCmdLine,INintnShowCmd){QWinApp*pWinApp=GlbGetApp();assert(pWinApp);pWinApp->InitInstance();pWinApp->run();pWinApp->ExitInstance();returnTRUE;}