//消息循环,win32编程基础概念,如果不懂,自行百度 //GetMessage (&msg, NULL, 0, 0)函数从消息队列中对消息进行检索 while (GetMessage (&msg, NULL, 0, 0)) { //将msg结构返还给windows以进行某些键盘消息的装换,参见第六章 TranslateMessage (&msg) ; //将msg结构返还给windows,windows会将这条消息发送给合适的窗口过程(WndProc)来处理, //也就是说,windows调用了窗口过程 DispatchMessage (&msg) ; } return msg.wParam ;
}//窗口过程函数,进行窗口消息处理LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam,LPARAM lParam){ static BOOL fFlipFlop = FALSE ; HBRUSH hBrush ; HDC hdc ; PAINTSTRUCT ps ; RECT rc ;
switch (message) { //窗口创建消息 case WM_CREATE: //设置定时器,定时时间到时引出WM_TIMER消息 SetTimer (hwnd, ID_TIMER, 1000, NULL) ; return 0 ; //响应定时器消息 case WM_TIMER : MessageBeep (-1) ; fFlipFlop = !fFlipFlop ; //调用InvalidateRect函数,产生WM_PAINT消息,即下一个case clause进行窗口重绘,实现窗口闪烁 InvalidateRect (hwnd, NULL, FALSE) ; return 0 ; //窗口重绘消息,移动客户群,改变客户区大小时 发出此消息 case WM_PAINT : hdc = BeginPaint (hwnd, &ps) ; GetClientRect (hwnd, &rc) ; hBrush = CreateSolidBrush (fFlipFlop ? RGB(255,0,0) : RGB(0,0,255)) ; FillRect (hdc, &rc, hBrush) ; EndPaint (hwnd, &ps) ; DeleteObject (hBrush) ; return 0 ; //窗口销毁消息 case WM_DESTROY : //程序退出前,kill掉timer KillTimer (hwnd, ID_TIMER) ; PostQuitMessage (0) ; return 0 ; } //其他消息由windows处理 return DefWindowProc (hwnd, message, wParam, lParam) ;
}