《windows程序设计》学习_2.1:初识消息
1 #include <windows.h> 2 3 //#define WM_MYMSG (WM_USER +100) 4 5 LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM); 6 7 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 8 PSTR szCmdLine, int iCmdShow) 9 { 10 static TCHAR szAppName[] = TEXT ("HelloWin"); 11 HWND hwnd; 12 MSG msg; 13 WNDCLASS wndclass; 14 15 wndclass.style = CS_HREDRAW | CS_VREDRAW ; 16 wndclass.lpfnWndProc = WndProc ; 17 wndclass.cbClsExtra = 0 ; 18 wndclass.cbWndExtra = 0 ; 19 wndclass.hInstance = hInstance ; 20 wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION) ; 21 wndclass.hCursor = LoadCursor(NULL,IDC_ARROW) ; 22 wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH) ; 23 wndclass.lpszMenuName = NULL ; 24 wndclass.lpszClassName = szAppName ; 25 26 if(!RegisterClass(&wndclass)) 27 { 28 MessageBox(NULL, TEXT("This program requires Windows NT!"), 29 szAppName, MB_ICONERROR); 30 31 return 0; 32 } 33 34 hwnd = CreateWindow(szAppName, 35 TEXT("The Hello Program"), 36 WS_OVERLAPPEDWINDOW, 37 CW_USEDEFAULT, 38 CW_USEDEFAULT, 39 CW_USEDEFAULT, 40 CW_USEDEFAULT, 41 NULL, 42 NULL, 43 hInstance, 44 NULL); 45 46 ShowWindow(hwnd , iCmdShow); 47 UpdateWindow(hwnd); 48 49 while(GetMessage (&msg, NULL, 0, 0)) 50 { 51 TranslateMessage(&msg); 52 DispatchMessage(&msg); 53 } 54 55 return msg.wParam; 56 } 57 58 LRESULT CALLBACK WndProc(HWND hwnd , UINT message , WPARAM wParam , LPARAM lParam) 59 { 60 HDC hdc; 61 PAINTSTRUCT ps; 62 RECT rect; 63 64 switch(message) 65 { 66 case WM_CREATE: 67 // PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC); 68 return 0; 69 70 case WM_PAINT: 71 hdc = BeginPaint(hwnd , &ps); 72 73 GetClientRect(hwnd, &rect); 74 75 DrawText(hdc , TEXT("Hello Windows 98!") , -1 ,&rect , 76 DT_SINGLELINE | DT_CENTER | DT_VCENTER); 77 78 EndPaint(hwnd,&ps); 79 80 // PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC); 81 return 0; 82 /* 83 case WM_MYMSG: 84 PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC); 85 return 0; //自定义消息 86 */ 87 case WM_LBUTTONDOWN: 88 // PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC); 89 // InvalidateRect(hwnd, NULL , FALSE); 90 // SendMessage(hwnd , WM_MYMSG , wParam , lParam); 91 return 0; 92 93 case WM_DESTROY: 94 // PlaySound (TEXT ("123.wav"), NULL , SND_FILENAME |SND_ASYNC); 95 PostQuitMessage(0); 96 return 0; 97 } 98 return DefWindowProc(hwnd,message,wParam,lParam); 99 }