1第四讲Windows窗口程序基础主讲:何军2011-秋季学期2Refreshing•Win32API简介•进程控制API3Outline•Windows窗口基本知识•窗口程序——“HelloWorld”•Windows的消息驱动•实例剖析41.Windows窗口基本知识51.1GUI•什么是GUI?•GraphicalUserInterface(图形化用户界面)6窗口的基本组成72.窗口“HelloWorld”82.1创建Win32工程92.2窗口程序结构WinMain注册窗口类创建主窗口,并进行必要初始化工作开始消息循环WndProc102.3窗口程序中的重要函数•WinMain函数•什么是APIENTRY?11#ifdef_MAC#defineCALLBACKPASCAL#defineWINAPICDECL#defineWINAPIVCDECL#defineAPIENTRYWINAPI#defineAPIPRIVATECDECL#ifdef_68K_#definePASCAL__pascal#else#definePASCAL#endif#elif(_MSC_VER=800)||defined(_STDCALL_SUPPORTED)#defineCALLBACK__stdcall#defineWINAPI__stdcall#defineWINAPIV__cdecl#defineAPIENTRYWINAPI#defineAPIPRIVATE__stdcall#definePASCAL__stdcall#else#defineCALLBACK#defineWINAPI#defineWINAPIV#defineAPIENTRYWINAPI#defineAPIPRIVATE#definePASCALpascal#endif122.3窗口程序中的重要函数•RegisterClassEx函数ATOMMyRegisterClass(HINSTANCEhInstance){WNDCLASSEXwcex;wcex.cbSize=sizeof(WNDCLASSEX);wcex.style=CS_HREDRAW|CS_VREDRAW;wcex.lpfnWndProc=WndProc;wcex.cbClsExtra=0;wcex.cbWndExtra=0;wcex.hInstance=hInstance;wcex.hIcon=LoadIcon(hInstance,MAKEINTRESOURCE(IDI_WIN32HELLO));wcex.hCursor=LoadCursor(NULL,IDC_ARROW);wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW+1);wcex.lpszMenuName=MAKEINTRESOURCE(IDC_WIN32HELLO);wcex.lpszClassName=szWindowClass;wcex.hIconSm=LoadIcon(wcex.hInstance,MAKEINTRESOURCE(IDI_SMALL));returnRegisterClassEx(&wcex);}132.3窗口程序中的重要函数•WndProc函数LRESULTCALLBACKWndProc(HWNDhWnd,UINTmessage,WPARAMwParam,LPARAMlParam){PAINTSTRUCTps;HDChdc;switch(message){caseWM_PAINT:hdc=BeginPaint(hWnd,&ps);//TODO:Addanydrawingcodehere...::TextOut(hdc,10,20,”HelloWorld”);EndPaint(hWnd,&ps);break;caseWM_DESTROY:PostQuitMessage(0);break;default:returnDefWindowProc(hWnd,message,wParam,lParam);}return0;}14•WinMain中的消息循环2.3窗口程序中的重要函数//Mainmessageloop:while(GetMessage(&msg,NULL,0,0)){if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}152.4两个问题?•WinMain函数中的消息循环是死循环吗?•在程序中没有看到WndProc函数被调用,它是如何起作用的?16Callback机制173.Windows消息驱动183.1消息机制•操作系统支持消息机制•消息队列——进程间相互通讯的机制•消息的同步处理•SendMessage•消息的异步处理•PostMessage/**Messagestructure*/typedefstructtagMSG{HWNDhwnd;UINTmessage;WPARAMwParam;LPARAMlParam;DWORDtime;POINTpt;#ifdef_MACDWORDlPrivate;#endif}MSG,*PMSG,NEAR*NPMSG,FAR*LPMSG;193.1消息机制•窗口中各种操作均对应于某种消息•最大化,最小化•窗口激活•窗口移动•也可自定义消息#defineWM_NULL0x0000#defineWM_CREATE0x0001#defineWM_DESTROY0x0002#defineWM_MOVE0x0003#defineWM_SIZE0x0005#defineWM_ACTIVATE0x0006#defineWM_SETFOCUS0x0007#defineWM_KILLFOCUS0x0008#defineWM_ENABLE0x000A#defineWM_SETREDRAW0x000B#defineWM_SETTEXT0x000C#defineWM_GETTEXT0x000D#defineWM_GETTEXTLENGTH0x000E#defineWM_PAINT0x000F#defineWM_CLOSE0x0010#defineWM_QUIT0x0012#defineWM_ERASEBKGND0x0014#defineWM_SYSCOLORCHANGE0x0015#defineWM_SHOWWINDOW0x0018#defineWM_WININICHANGE0x001A#defineWM_DEVMODECHANGE0x001B#defineWM_ACTIVATEAPP0x001C#defineWM_FONTCHANGE0x001D#defineWM_TIMECHANGE0x001E#defineWM_CANCELMODE0x001F#defineWM_SETCURSOR0x0020#defineWM_MOUSEACTIVATE0x0021#defineWM_CHILDACTIVATE0x0022#defineWM_QUEUESYNC0x002320•GetMessage•何时返回FALSE?•WM_QUIT3.2消息机制的几个重要函数//Mainmessageloop:while(GetMessage(&msg,NULL,0,0)){if(!TranslateAccelerator(msg.hwnd,hAccelTable,&msg)){TranslateMessage(&msg);DispatchMessage(&msg);}}BOOLWINAPIGetMessage(__outLPMSGlpMsg,__in_optHWNDhWnd,__inUINTwMsgFilterMin,__inUINTwMsgFilterMax);21•TranslateMessage与DispatchMessage•把键盘输入翻译成为可传递的消息•分发一个消息到对应窗口的窗口函数3.2消息机制的几个重要函数BOOLWINAPITranslateMessage(__inCONSTMSG*lpMsg);LRESULTWINAPIDispatchMessage(__inCONSTMSG*lpMsg);22•SendMessage与PostMessage•消息传递的两个重要函数•同步传递——SendMessage•异步传递——PostMessage3.2消息机制的几个重要函数LRESULTWINAPISendMessage(__inHWNDhWnd,__inUINTMsg,__inWPARAMwParam,__inLPARAMlParam);BOOLWINAPIPostMessage(__in_optHWNDhWnd,__inUINTMsg,__inWPARAMwParam,__inLPARAMlParam);233.3Windows消息循环244.实例剖析2526本节课小结27重要概念•Callback函数(回调函数)•消息驱动(消息循环)28新的Win32API•消息循环相关•GetMessage,TranslateMessage,DispatchMessage,PostMessage,PostQuitMessage•窗口相关•CreateWindow,ShowWindow,UpdateWindow,DestroyWindow•图形界面相关•BeginPaint,EndPaint,TextOut