#include "stdafx.h" #include <windows.h> #include "resource.h" //HINSTANCE hInst; MSG msg; char ClassName[]="window_class"; char *ShowText; LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); void onLButtonDown(HWND hWnd,UINT messgae,WPARAM wParam,LPARAM lParam); void onPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); void onDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); class CFrameWnd { public: HWND m_hWnd; HINSTANCE m_hInstance; public: CFrameWnd(HINSTANCE hInstance); ~CFrameWnd(); int RegisterWindow(); BOOL Create(LPCTSTR WindowName); void ShowWindow(int nCmdShow); void UpdateWindow(); }; CFrameWnd::CFrameWnd(HINSTANCE hInstance): m_hInstance(hInstance) { } CFrameWnd::~CFrameWnd() { } int CFrameWnd::RegisterWindow() { WNDCLASS wc; wc.cbClsExtra=0; wc.cbWndExtra=0; wc.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH); wc.hCursor=LoadCursor(NULL,IDC_ARROW); wc.hIcon=LoadIcon(m_hInstance,IDI_APPLICATION); wc.hInstance=m_hInstance; wc.lpfnWndProc=WndProc; wc.lpszClassName=ClassName; wc.lpszMenuName=NULL; wc.style=0; return RegisterClass(&wc); /* WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = m_hInstance; wcex.hIcon = LoadIcon(m_hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = ClassName; wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL); return RegisterClassEx(&wcex);*/ } BOOL CFrameWnd::Create(LPCTSTR WindowName) { RegisterWindow(); m_hWnd = CreateWindow(ClassName, WindowName, WS_OVERLAPPEDWINDOW, //0,0,500,600, CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, m_hInstance, NULL); if(!m_hWnd) return FALSE; return TRUE; } void CFrameWnd::ShowWindow(int nCmdShow) { ::ShowWindow(m_hWnd,nCmdShow); } void CFrameWnd::UpdateWindow() { ::UpdateWindow(m_hWnd); } class CWinApp { public: CFrameWnd *m_pMainWnd; public: CWinApp(); BOOL InitInstance(HINSTANCE hInstance, int nCmdShow); int Run(); ~CWinApp(); }; CWinApp::CWinApp(): m_pMainWnd(NULL) { } BOOL CWinApp::InitInstance(HINSTANCE hInstance, int nCmdShow) { m_pMainWnd=new CFrameWnd(hInstance); if(m_pMainWnd == NULL) return false; m_pMainWnd->Create("我的窗口"); m_pMainWnd->ShowWindow(nCmdShow); m_pMainWnd->UpdateWindow(); return true; } int CWinApp::Run() { while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } CWinApp::~CWinApp() { delete m_pMainWnd; } CWinApp theApp; int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { int ResultCode; if( !theApp.InitInstance(hInstance, nCmdShow) ) { MessageBox(NULL,"error","error",MB_OK); return -1; } return ResultCode=theApp.Run(); } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch(message) { case WM_LBUTTONDOWN: onLButtonDown(hWnd,message,wParam,lParam); break; case WM_PAINT: onPaint(hWnd,message,wParam,lParam); break; case WM_DESTROY: onDestroy(hWnd,message,wParam,lParam); break; default : return DefWindowProc(hWnd,message,wParam,lParam); } return 0; } void onLButtonDown(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { ShowText="你好!"; InvalidateRect(hWnd,NULL,1); } void onPaint(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; hdc=BeginPaint(hWnd,&ps); TextOut(hdc,50,50,ShowText,6); EndPaint(hWnd,&ps); } void onDestroy(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam) { PostQuitMessage(0); }