当前位置:首页 > Windows程序 > 正文

Windows普通窗口程序

2021-03-24 Windows程序

2015-10-09 12:55:38

KWindow.h

#pragma once #include <windows.h> class KWindow { virtual void OnDraw(HDC hdc) { } virtual void OnKeyDown(WPARAM wParam, LPARAM lParam) { } virtual LRESULT WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); virtual void GetWndClassEx(WNDCLASSEX& wc); public: HWND m_hWnd; KWindow() { m_hWnd = NULL; } virtual ~KWindow() { } virtual bool CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hParent, HMENU hMenu, HINSTANCE hInst); bool RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst); virtual WPARAM MessageLoop(); BOOL ShowWindow(int nCmdShow) const { return ::ShowWindow(m_hWnd, nCmdShow); } BOOL UpdateWindow() const { return ::UpdateWindow(m_hWnd); } void CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point); };

KWindow.cpp

#define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <tchar.h> #include <assert.h> #include "KWindow.h" LRESULT KWindow::WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch(uMsg) { case WM_KEYDOWN: { OnKeyDown(wParam, lParam); return 0; } case WM_PAINT: { PAINTSTRUCT ps; BeginPaint(m_hWnd, &ps); OnDraw(ps.hdc); EndPaint(m_hWnd, &ps); return 0; } case WM_DESTROY: { PostQuitMessage(0); return 0; } } return DefWindowProc(hWnd, uMsg, wParam, lParam); } LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { KWindow* pWindow; DWORD dwErr = 0; if(uMsg == WM_NCCREATE) { assert( ! IsBadReadPtr((void*)lParam, sizeof(CREATESTRUCT))); MDICREATESTRUCT* pMDIC = (MDICREATESTRUCT*)((LPCREATESTRUCT)lParam)->lpCreateParams; pWindow = (KWindow*)(pMDIC->lParam); assert(!IsBadReadPtr(pWindow, sizeof(KWindow))); SetWindowLong(hWnd, GWL_USERDATA, (LONG)pWindow); } else { pWindow = (KWindow*)GetWindowLong(hWnd, GWL_USERDATA); } if(pWindow) { return pWindow->WndProc(hWnd, uMsg, wParam, lParam); } else { return DefWindowProc(hWnd, uMsg, wParam, lParam); } } bool KWindow::RegisterClass(LPCTSTR lpszClass, HINSTANCE hInst) { WNDCLASSEX wc; GetWndClassEx(wc); wc.hInstance = hInst; wc.lpszClassName = lpszClass; if(!RegisterClassEx(&wc)) return false; return true; } bool KWindow::CreateEx(DWORD dwExStyle, LPCTSTR lpszClass, LPCTSTR lpszName, DWORD dwStyle, int x, int y, int nWidth, int nHeight, HWND hParent, HMENU hMenu, HINSTANCE hInst) { if(!RegisterClass(lpszClass,hInst)) return false; MDICREATESTRUCT mdic; memset(&mdic, 0, sizeof(mdic)); mdic.lParam = (LPARAM)this; m_hWnd = CreateWindowEx(dwExStyle, lpszClass, lpszName, dwStyle, x, y, nWidth, nHeight, hParent, hMenu, hInst, &mdic); return m_hWnd != NULL; } void KWindow::GetWndClassEx(WNDCLASSEX& wc) { memset(&wc, 0, sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = 0; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = NULL; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.lpszMenuName = NULL; wc.lpszClassName = NULL; wc.hIconSm = NULL; } WPARAM KWindow::MessageLoop() { MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } void KWindow::CenterText(HDC hdc, int x, int y, LPCTSTR szFace, LPCTSTR szMessage, int point) { HFONT hFont = CreateFont(-point * GetDeviceCaps(hdc, LOGPIXELSY) / 72, 0, 0, 0, FW_BOLD, TRUE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY, VARIABLE_PITCH, szFace); assert(hFont); HGDIOBJ hold = SelectObject(hdc, hFont); SetTextAlign(hdc, TA_CENTER | TA_BASELINE); SetBkMode(hdc, TRANSPARENT); SetTextColor(hdc, RGB(0,0,0xFF)); TextOut(hdc, x, y, szMessage, _tcslen(szMessage)); SelectObject(hdc, hold); DeleteObject(hFont); }

KHelloWindow.cpp

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/66738.html