扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
---创建stdafx.h头文件
创新互联建站是一家专注于成都网站建设、网站制作与策划设计,陆丰网站建设哪家好?创新互联建站做网站,专注于网站建设10多年,网设计领域的专业建站公司;建站业务涵盖:陆丰等地区。陆丰做网站价格咨询:028-86922220#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; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流