运行环境 VS2005 如果在其他环境可能出现问题, 下篇讲解注意的问题
#include <windows.h> #include <stdio.h> LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ); int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd ) { WNDCLASS wndcls; wndcls.cbClsExtra = 0; wndcls.cbWndExtra = 0; wndcls.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wndcls.hCursor = LoadCursor(NULL, IDC_ARROW); wndcls.hIcon = LoadIcon(NULL, IDI_APPLICATION); wndcls.hInstance = hInstance; wndcls.lpfnWndProc = WinSunProc; wndcls.lpszClassName = "ferry2010"; wndcls.lpszMenuName = NULL; wndcls.style = CS_HREDRAW | CS_VREDRAW; RegisterClass(&wndcls); HWND hwnd; hwnd = CreateWindow("ferry2010", "渡口网络", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, NULL); ShowWindow(hwnd, SW_SHOWNORMAL); UpdateWindow(hwnd); MSG msg; while(GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return 0; } LRESULT CALLBACK WinSunProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { char showMsg[] = "渡口网络"; switch(uMsg) { case WM_CHAR: char szChar[20]; sprintf_s(szChar, "char is %d", wParam); MessageBox(hwnd, szChar, "ferry", MB_OK); break; case WM_LBUTTONDOWN: MessageBox(hwnd, "left mouse click", "ferry", MB_OK); HDC hdc_L; hdc_L = GetDC(hwnd); TextOut(hdc_L, 0, 50, "渡口网络", strlen("渡口网络")); ReleaseDC(hwnd, hdc_L); break; case WM_PAINT: HDC hdc_P; PAINTSTRUCT ps; hdc_P = BeginPaint(hwnd, &ps); TextOut(hdc_P, 0, 0, "渡口网络欢迎您" , strlen("渡口网络欢迎您")); EndPaint(hwnd, &ps); break; case WM_CLOSE: if (IDYES == MessageBox(hwnd, "是否确认关闭窗口", "关闭", MB_YESNO)) { DestroyWindow(hwnd); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; }