win32学习之--------面向对象的窗口类封装代码记录
---创建stdafx.h头文件
成都创新互联公司服务项目包括峨边彝族网站建设、峨边彝族网站制作、峨边彝族网页制作以及峨边彝族网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,峨边彝族网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到峨边彝族省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!
#include#include #include #include "resource.h"
---创建QWnd类==》窗口基类
---QWnd.h文件
#pragma once #include "stdafx.h" class QWnd { public: QWnd(void); ~QWnd(void); virtual BOOL CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam = NULL ); virtual BOOL PreCreateWindow(CREATESTRUCT & cs); public: BOOL ShowWindow(int nCmdShow); BOOL UpdateWindow(); BOOL DestroyWindow(); public: static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); public: virtual LRESULT WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam); virtual LRESULT Default(UINT uMsg, WPARAM wParam, LPARAM lParam); public: //message handle virtual LRESULT onClose(WPARAM wParam, LPARAM lParam); virtual LRESULT onDestroy(WPARAM wParam, LPARAM lParam); virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam); public: BOOL SubClassWindow(HWND hWnd); static QWnd * FromHandle(HWND hWnd); public: HWND m_hWnd; WNDPROC m_lpfnOldWndProc; };
---QWnd.cpp文件
#include "QWnd.h" QWnd::QWnd() { m_hWnd = NULL; m_lpfnOldWndProc = NULL; } QWnd::~QWnd() { } LRESULT CALLBACK QWnd::WndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam) { if (uMsg == WM_CREATE || uMsg == WM_NCCREATE) { LPCREATESTRUCT pCs = (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); return qWnd->WindowProc(uMsg, wParam, lParam); } } } QWnd * qWnd = (QWnd*)GetWindowLong(hWnd, GWL_USERDATA); if (qWnd) { return qWnd->WindowProc(uMsg, wParam, lParam); } return ::DefWindowProc(hWnd,uMsg,wParam,lParam); } LRESULT QWnd::WindowProc( UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CLOSE: { return onClose(wParam, lParam); } break; case WM_DESTROY: { return onDestroy(wParam, lParam); } break; case WM_CREATE: { return onCreate(wParam, lParam); } break; } return Default( uMsg, wParam, lParam); } LRESULT QWnd::Default(UINT uMsg, WPARAM wParam, LPARAM lParam) { if (m_lpfnOldWndProc == QWnd::WndProc) { return ::DefWindowProc(m_hWnd, uMsg, wParam, lParam); } return m_lpfnOldWndProc(m_hWnd, uMsg, wParam, lParam); } //msg handle LRESULT QWnd::onClose(WPARAM wParam, LPARAM lParam) { return Default( WM_CLOSE, wParam, lParam); } LRESULT QWnd::onDestroy(WPARAM wParam, LPARAM lParam) { return Default(WM_DESTROY, wParam, lParam); } LRESULT QWnd::onCreate(WPARAM wParam, LPARAM lParam) { return Default(WM_CREATE, wParam, lParam); } BOOL QWnd::SubClassWindow(HWND hWnd) { assert(hWnd); if (m_hWnd == hWnd) { return true; } 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); return TRUE; } return FALSE; } QWnd * QWnd::FromHandle(HWND hWnd) { assert(hWnd); QWnd * qWnd = (QWnd*)GetWindowLong(hWnd, GWL_USERDATA); return qWnd; } BOOL QWnd::PreCreateWindow(CREATESTRUCT& cs) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); BOOL bRet = ::GetClassInfoEx(cs.hInstance,cs.lpszClass,&wcex ); //检查窗口类是否被注册 if (bRet) { return true; } 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) { return FALSE; } return true; } BOOL QWnd::CreateEx( DWORD dwExStyle, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu, LPVOID lpParam ) { HINSTANCE hInstance = (HINSTANCE)::GetModuleHandle(NULL); assert(hInstance); CREATESTRUCT cs; 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; BOOL bRet = PreCreateWindow(cs); if (!bRet) { MessageBox(NULL, _T("注册窗口类失败"),_T("注册窗口类"), 0); return false; } HWND hWnd = CreateWindowEx(dwExStyle, lpszClassName, lpszWindowName, dwStyle,x,y,nWidth,nHeight, hWndParent, nIDorHMenu, hInstance,this);//这个函数执行完 会发送WM_CREATE消息 this的作用是因为提前传递 if (NULL == hWnd) { return FALSE; } 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消息 } return TRUE; } BOOL QWnd::ShowWindow(int nCmdShow) { assert(m_hWnd); return ::ShowWindow(m_hWnd, nCmdShow); } BOOL QWnd::UpdateWindow() { assert(m_hWnd); return ::UpdateWindow(m_hWnd); } BOOL QWnd::DestroyWindow() { assert(m_hWnd); return ::DestroyWindow(m_hWnd); }
---创建QWinApp基类==》应用程序基类
---QWinApp.h文件
#pragma once #include "QWnd.h" #include "QButton.h" class QMainFrame : public QWnd { public: QMainFrame(); ~QMainFrame(); public: LRESULT onClose(WPARAM wParam, LPARAM lParam); LRESULT onDestroy(WPARAM wParam, LPARAM lParam); virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam); public: QButton *m_wndButton; };
---QWinApp.cpp文件
#include "QWinApp.h" #include "QGlobal.h" QWinApp::QWinApp() { m_pMainWnd = NULL; g_pWinApp = this; } QWinApp::~QWinApp() { } BOOL QWinApp::InitInstance() { return TRUE; } BOOL QWinApp::ExitInstance() { return TRUE; } void QWinApp::run() { MSG msg; while (::GetMessage(&msg, NULL, NULL, NULL)) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } }
---创建应该程序类的全局变量
---创建QGlobal.h头文件
#pragma once #include "QWinApp.h" extern QWinApp* g_pWinApp; extern QWinApp* GlbGetApp();
---创建QGlobal.cpp文件
#include "QGlobal.h" QWinApp* g_pWinApp = NULL; QWinApp* GlbGetApp() { return g_pWinApp; }
---创建QWnd的子类--子控件Button
---创建文件QButton.h
#pragma once #include "QWnd.h" class QButton : public QWnd { public: QButton(); ~QButton(); public: BOOL CreateEx( LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu ); };
---创建文件QButton.cpp
#include "QButton.h" QButton::QButton() { } QButton::~QButton() { } BOOL QButton::CreateEx( LPCTSTR lpszWindowName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hWndParent, HMENU nIDorHMenu ) { return QWnd::CreateEx(0, _T("BUTTON"), lpszWindowName, dwStyle, x, y, nWidth, nHeight, hWndParent, nIDorHMenu); }
---创建QWnd的子类---QMainFrame
---创建QMainFrame.h文件
#pragma once #include "QWnd.h" #include "QButton.h" class QMainFrame : public QWnd { public: QMainFrame(); ~QMainFrame(); public: LRESULT onClose(WPARAM wParam, LPARAM lParam); LRESULT onDestroy(WPARAM wParam, LPARAM lParam); virtual LRESULT onCreate(WPARAM wParam, LPARAM lParam); public: QButton *m_wndButton; };
---创建QMainFrame.cpp文件
#include "QMainFrame.h" #define IDC_BUTTON 10001 QMainFrame::QMainFrame() { m_wndButton = new QButton(); } QMainFrame::~QMainFrame() { } LRESULT QMainFrame::onClose(WPARAM wParam, LPARAM lParam) { return DestroyWindow(); } LRESULT QMainFrame::onDestroy(WPARAM wParam, LPARAM lParam) { ::PostQuitMessage(0); return true; } LRESULT QMainFrame::onCreate(WPARAM wParam, LPARAM lParam) { 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); } return TRUE; }
---创建QWinApp的子类---QDemowinApp
---创建QDemowinApp.h文件
#pragma once #include "QWinApp.h" class QDemowinApp : public QWinApp { public: QDemowinApp(); ~QDemowinApp(); public: virtual BOOL InitInstance(); virtual BOOL ExitInstance(); }; extern QDemowinApp theApp;
---创建QDemowinApp.cpp文件
#include "QDemowinApp.h" #include "QMainFrame.h" QDemowinApp::QDemowinApp() { } QDemowinApp::~QDemowinApp() { } QDemowinApp theApp; BOOL QDemowinApp::InitInstance() { QMainFrame *pMainWnd = new QMainFrame(); assert(pMainWnd); m_pMainWnd = pMainWnd; BOOL bRet = 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("create window failed!"), _T("create window!"), 0); } pMainWnd->ShowWindow(SW_SHOW); pMainWnd->UpdateWindow(); return true; } BOOL QDemowinApp::ExitInstance() { if (m_pMainWnd) { delete m_pMainWnd; m_pMainWnd = NULL; } return TRUE; }
---创建入口程序
#include "QDemowinApp.h" #include "QGlobal.h" int WINAPI _tWinMain( IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPTSTR lpCmdLine, IN int nShowCmd ) { QWinApp* pWinApp = GlbGetApp(); assert(pWinApp); pWinApp->InitInstance(); pWinApp->run(); pWinApp->ExitInstance(); return TRUE; }
网页名称:win32学习之--------面向对象的窗口类封装代码记录
分享路径:http://scyanting.com/article/jojcco.html