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); ; } case WM_PAINT: { PAINTSTRUCT ps; BeginPaint(m_hWnd, &ps); OnDraw(ps.hdc); EndPaint(m_hWnd, &ps); ; } case WM_DESTROY: { PostQuitMessage(); ; } } return DefWindowProc(hWnd, uMsg, wParam, lParam); } LRESULT CALLBACK KWindow::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { KWindow* pWindow; DWORD dwErr = ; 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, , 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, , sizeof(wc)); wc.cbSize = sizeof(WNDCLASSEX); wc.style = ; wc.lpfnWndProc = WindowProc; wc.cbClsExtra = ; wc.cbWndExtra = ; 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; , )) { 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) / , , , , 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(,,0xFF)); TextOut(hdc, x, y, szMessage, _tcslen(szMessage)); SelectObject(hdc, hold); DeleteObject(hFont); }
KHelloWindow.cpp
#define STRICT #define WIN32_LEAN_AND_MEAN #include <windows.h> #include <tchar.h> #include <assert.h> #include <conio.h> #include <iostream> #include "KWindow.h" #pragma warning( disable:4996 ) const TCHAR szMessage[] = _T("Hello,World!"); const TCHAR szFace[] = _T("Times New Roman"); const TCHAR szHint[] = _T("Press ESC to quit."); const TCHAR szProgram[] = _T("HelloWorld3"); class KHelloWindow : public KWindow { void OnKeyDown(WPARAM wParam, LPARAM lParam) { if(wParam == VK_ESCAPE) PostMessage(m_hWnd, WM_CLOSE, , ); } void OnDraw(HDC hdc) { TextOut(hdc, , , szHint, lstrlen(szHint)); CenterText(hdc, GetDeviceCaps(hdc, HORZRES) / , GetDeviceCaps(hdc, VERTRES) / , szFace, szMessage, ); } }; //* int WINAPI WinMain( _In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd ) { KHelloWindow win; , szProgram, szProgram, WS_POPUP, , , GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hInstance); win.ShowWindow(nShowCmd); win.UpdateWindow(); return win.MessageLoop(); } /*/