您好,登錄后才能下訂單哦!
---創建stdafx.h頭文件
#include<Windows.h> #include<tchar.h> #include<assert.h> #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; }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。