(VC)win32 窗体开发主要流程

窗体设计

设计流图:

代码示例:

  1. //设计窗口  
  2. WNDCLASS wndclass;  
  3.   
  4. wndclass.cbClsExtra=0;  
  5. wndclass.cbWndExtra=0;  
  6. wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);  
  7. wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);  
  8. wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);  
  9. wndclass.hInstance=hInstance;  
  10. wndclass.lpfnWndProc=textprom;  
  11. wndclass.lpszClassName="text";  
  12. wndclass.lpszMenuName=NULL;  
  13. wndclass.style=CS_HREDRAW | CS_VREDRAW;  
  14.   
  15.   
  16. //注册窗口类  
  17. if(!RegisterClass(&wndclass))  
  18. {  
  19.     MessageBox(NULL,"create windows error!","error",MB_OK | MB_ICONSTOP);  
  20. }  
  21.   
  22.   
  23. //创建窗口  
  24. HWND hwnd=CreateWindow("text","hellow world",WS_DLGFRAME | WS_MINIMIZEBOX | WS_SYSMENU,CW_USEDEFAULT,CW_USEDEFAULT,  
  25.         CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);  
  26.   
  27.   
  28. //显示更新窗口  
  29. ShowWindow(hwnd,nCmdShow);  
  30. UpdateWindow(hwnd);  
  31.   
  32.   
  33. //消息循环  
  34. MSG msg;  
  35. while(GetMessage(&msg,NULL,0,0))  
  36. {  
  37.     TranslateMessage(&msg);  
  38.     DispatchMessage(&msg);  
  39. }  


回调函数设计

设计大致模型:

  1. LRESULT CALLBACK WindowProc(  
  2.   HWND hwnd,     // handle to window  
  3.   UINT uMsg,     // message identifier  
  4.   WPARAM wParam, // first message parameter  
  5.   LPARAM lParam   // second message parameter  
  6. )  
  7. {  
  8.     ……  
  9.     switch(uMsg)  
  10.     {  
  11.     case ‥ : ……;  
  12.           break;  
  13.     ……  
  14.     case WM_DESTROY:  PostQuitMessage(0);//在消息队列尾部插入一个WM_QUIT消息  
  15.              break;  
  16.     defaultreturn DefWindowProc(hwnd,uMsg,wParam,lParam);  
  17.     }  
  18.     ……  
  19.     return 0;  
  20. }  

注意:

  1. 必须把所有不处理的消息交给 DefWindowPro 函数处理,也要把它的返回值返回给 windows 否则 windows 就失去了与应用程序通信的途径也就是说不能在控制窗口的行为
  2. WM_DESTROY 是窗口函数必须处理的消息,因为在窗体销毁的时候并不会主动向程序发送一个 WM_QUIT 这个消息,所以我们的窗体即使销毁了程序依旧还在消息循环中,为了达到在销毁窗体时候并且退出消息循环我们应用处理这个WM_DESTROY 这个消息,在程序接收到这个消息时候向消息队列发一个 WM_QUIT 消息来退出消息循环
  3. WM_CLOSE 默认由 DefWindowPro 函数处理,它会调用 DestroyWindow 函数销毁窗口
  4. WM_CREATE 为 WndProc 第一处理的信息
  5. 在视窗大小改变时,会发 WM_SIZE 这个消息且在 lParam 中 LOWORD(lParam) 中为窗口横坐标 HIWORD(lParam) 中为窗口纵坐标
  6. 回调函数的参数与 MSG 结构的前四位成员相同

 

程序样例

  1. #include<windows.h>  
  2. #include"resource.h"  
  3. #include<string.h>  
  4.   
  5. LRESULT CALLBACK textprom(  
  6.   HWND hwnd,      // handle to window  
  7.   UINT uMsg,      // message identifier  
  8.   WPARAM wParam,  // first message parameter  
  9.   LPARAM lParam   // second message parameter  
  10. );  
  11.   
  12.   
  13. int WINAPI WinMain(  HINSTANCE hInstance,  // handle to current instance  
  14.   HINSTANCE hPrevInstance,  // handle to previous instance  
  15.   LPSTR lpCmdLine,      // pointer to command line  
  16.   int nCmdShow          // show state of window  
  17.   )  
  18. {  
  19.     WNDCLASS wndclass;  
  20.   
  21.     wndclass.cbClsExtra=0;  
  22.     wndclass.cbWndExtra=0;  
  23.     wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);  
  24.     wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);  
  25.     wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);  
  26.     wndclass.hInstance=hInstance;  
  27.     wndclass.lpfnWndProc=textprom;  
  28.     wndclass.lpszClassName="text";  
  29.     wndclass.lpszMenuName=NULL;  
  30.     wndclass.style=CS_HREDRAW | CS_VREDRAW;  
  31.   
  32.   
  33.     if(!RegisterClass(&wndclass))  
  34.     {  
  35.         MessageBox(NULL,"create windows error!","error",MB_OK | MB_ICONSTOP);  
  36.     }  
  37.   
  38.   
  39.     HWND hwnd=CreateWindow("text","hellow world",WS_DLGFRAME | WS_MINIMIZEBOX | WS_SYSMENU,CW_USEDEFAULT,CW_USEDEFAULT,  
  40.         CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);  
  41.   
  42.   
  43.   
  44.     ShowWindow(hwnd,nCmdShow);  
  45.     UpdateWindow(hwnd);  
  46.   
  47.   
  48.     MSG msg;  
  49.     while(GetMessage(&msg,NULL,0,0))  
  50.     {  
  51.         TranslateMessage(&msg);  
  52.         DispatchMessage(&msg);  
  53.     }  
  54.   
  55.     return msg.wParam;  
  56. }  
  57.   
  58.   
  59.   
  60. LRESULT CALLBACK textprom(  
  61.   HWND hwnd,      // handle to window  
  62.   UINT uMsg,      // message identifier  
  63.   WPARAM wParam,  // first message parameter  
  64.   LPARAM lParam   // second message parameter  
  65. )  
  66. {  
  67.     HDC hdc;  
  68.     PAINTSTRUCT ps;  
  69.     RECT rect;  
  70.   
  71.     switch(uMsg)  
  72.     {  
  73.     case WM_PAINT:  
  74.         hdc=BeginPaint(hwnd,&ps);  
  75.         GetClientRect(hwnd,&rect);  
  76.         DrawText(hdc,"hellow my first windows program",strlen("hellow my first windows program"),&rect,  
  77.             DT_SINGLELINE | DT_CENTER | DT_VCENTER);  
  78.         EndPaint(hwnd,&ps);  
  79.         break;  
  80.     case WM_RBUTTONDOWN:  
  81.         hdc=GetDC(hwnd);  
  82.         TextOut(hdc,0,0,"success",strlen("success"));  
  83.         ReleaseDC(hwnd,hdc);      
  84.         break;  
  85.     case WM_RBUTTONUP:  
  86.         GetClientRect(hwnd,&rect);  
  87.         InvalidateRect(hwnd,&rect,true);  
  88.         break;  
  89.     case WM_DESTROY:  
  90.         PostQuitMessage(0);  
  91.         break;  
  92.     default:  
  93.         ;  
  94.     }  
  95.     return DefWindowProc(hwnd,uMsg,wParam,lParam);  
  96. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值