2015-10-09 12:55:38
KWindow.h
#pragmaOnce#include<windows.h>classkwindow{Virtual voidOnDraw (hdc hdc) {}Virtual voidOnKeyDown (WPARAM WPARAM, LPARAM LPARAM) {}VirtualLRESULT WndProc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM); StaticLRESULT CALLBACK WindowProc (HWND hwnd, UINT umsg, WPARAM WPARAM, LPARAM LPARAM); Virtual voidGetwndclassex (wndclassex&WC); Public: HWND m_hwnd; Kwindow () {m_hwnd=NULL; } Virtual~Kwindow () {}Virtual BOOLCreateEx (DWORD dwExStyle, LPCTSTR Lpszclass, LPCTSTR LpszName, DWORD dwstyle,intXintYintNwidth,intnheight, HWND hparent, HMENU HMENU, hinstance hInst); BOOLregisterclass (LPCTSTR lpszclass, hinstance hInst); VirtualWPARAM Messageloop (); BOOL ShowWindow (intnCmdShow)Const { return:: ShowWindow (M_hwnd, ncmdshow); } BOOL UpdateWindow ()Const { return:: UpdateWindow (M_hwnd); } voidCentertext (HDC hdc,intXintY, LPCTSTR szface, LPCTSTR szmessage,intPoint );};
KWindow.cpp
#defineSTRICT#defineWin32_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) { Casewm_keydown: {OnKeyDown (WParam, LParam); return 0; } Casewm_paint: {paintstruct ps; BeginPaint (M_hwnd,&PS); OnDraw (PS.HDC); EndPaint (M_hwnd,&PS); return 0; } CaseWm_destroy: {postquitmessage (0); return 0; } } returnDefWindowProc (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) {returnPwindow->WndProc (hWnd, umsg, WParam, LParam); } Else { returnDefWindowProc (hWnd, umsg, WParam, LParam); }}BOOLKwindow::registerclass (LPCTSTR lpszclass, hinstance hInst) {wndclassex WC; Getwndclassex (WC); Wc.hinstance=HInst; Wc.lpszclassname=Lpszclass; if(! RegisterClassEx (&WC)) return false; return true;}BOOLKwindow::createex (DWORD dwExStyle, LPCTSTR Lpszclass, LPCTSTR LpszName, DWORD dwstyle,intXintYintNwidth,intnheight, 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); returnM_hwnd! =NULL;}voidKwindow::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); } returnMsg.wparam;}voidKwindow::centertext (HDC hdc,intXintY, LPCTSTR szface, LPCTSTR szmessage,intPoint ) {Hfont Hfont= CreateFont (-point * GETDEVICECAPS (HDC, Logpixelsy)/ the, 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
#defineSTRICT#defineWin32_lean_and_mean#include<windows.h>#include<tchar.h>#include<assert.h>#include<conio.h>#include<iostream>#include"KWindow.h"#pragmaWarning (disable:4996)ConstTCHAR szmessage[] = _t ("hello,world!");ConstTCHAR szface[] = _t ("Times New Roman");ConstTCHAR szhint[] = _t ("Press ESC to quit.");ConstTCHAR szprogram[] = _t ("HelloWorld3");classKhellowindow: Publickwindow{voidOnKeyDown (WPARAM WPARAM, LPARAM LPARAM) {if(WParam = =vk_escape) PostMessage (M_hwnd, Wm_close,0,0); } voidOnDraw (hdc hdc) {TextOut (hdc,0,0, Szhint, Lstrlen (Szhint)); Centertext (HDC, GetDeviceCaps (HDC, horzres)/2, GetDeviceCaps (hdc, vertres)/2, Szface, Szmessage, the); }};//*intWINAPI WinMain (_in_ hinstance hinstance, _in_opt_ hinstance hprevinstance, _in_ LPSTR lpCmdLine, _in_intnShowCmd) {Khellowindow win; BOOLresult = win. CreateEx (0, Szprogram, Szprogram, Ws_popup,0,0, GetSystemMetrics (Sm_cxscreen), GetSystemMetrics (Sm_cyscreen), NULL, NULL, HINSTANCE); Win. ShowWindow (nShowCmd); Win. UpdateWindow (); returnwin. Messageloop ();}/*/
Windows Normal Window Program