窗体设计
设计流图:
代码示例:
- //设计窗口
- WNDCLASS wndclass;
- wndclass.cbClsExtra=0;
- wndclass.cbWndExtra=0;
- wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
- wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
- wndclass.hInstance=hInstance;
- wndclass.lpfnWndProc=textprom;
- wndclass.lpszClassName="text";
- wndclass.lpszMenuName=NULL;
- wndclass.style=CS_HREDRAW | CS_VREDRAW;
- //注册窗口类
- if(!RegisterClass(&wndclass))
- {
- MessageBox(NULL,"create windows error!","error",MB_OK | MB_ICONSTOP);
- }
- //创建窗口
- HWND hwnd=CreateWindow("text","hellow world",WS_DLGFRAME | WS_MINIMIZEBOX | WS_SYSMENU,CW_USEDEFAULT,CW_USEDEFAULT,
- CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
- //显示更新窗口
- ShowWindow(hwnd,nCmdShow);
- UpdateWindow(hwnd);
- //消息循环
- MSG msg;
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
回调函数设计
设计大致模型:
- LRESULT CALLBACK WindowProc(
- HWND hwnd, // handle to window
- UINT uMsg, // message identifier
- WPARAM wParam, // first message parameter
- LPARAM lParam // second message parameter
- )
- {
- ……
- switch(uMsg)
- {
- case ‥ : ……;
- break;
- ……
- case WM_DESTROY: PostQuitMessage(0);//在消息队列尾部插入一个WM_QUIT消息
- break;
- default: return DefWindowProc(hwnd,uMsg,wParam,lParam);
- }
- ……
- return 0;
- }
注意:
- 必须把所有不处理的消息交给 DefWindowPro 函数处理,也要把它的返回值返回给 windows 否则 windows 就失去了与应用程序通信的途径也就是说不能在控制窗口的行为
- WM_DESTROY 是窗口函数必须处理的消息,因为在窗体销毁的时候并不会主动向程序发送一个 WM_QUIT 这个消息,所以我们的窗体即使销毁了程序依旧还在消息循环中,为了达到在销毁窗体时候并且退出消息循环我们应用处理这个WM_DESTROY 这个消息,在程序接收到这个消息时候向消息队列发一个 WM_QUIT 消息来退出消息循环
- WM_CLOSE 默认由 DefWindowPro 函数处理,它会调用 DestroyWindow 函数销毁窗口
- WM_CREATE 为 WndProc 第一处理的信息
- 在视窗大小改变时,会发 WM_SIZE 这个消息且在 lParam 中 LOWORD(lParam) 中为窗口横坐标 HIWORD(lParam) 中为窗口纵坐标
- 回调函数的参数与 MSG 结构的前四位成员相同
程序样例
- #include<windows.h>
- #include"resource.h"
- #include<string.h>
- LRESULT CALLBACK textprom(
- HWND hwnd, // handle to window
- UINT uMsg, // message identifier
- WPARAM wParam, // first message parameter
- LPARAM lParam // second message parameter
- );
- int WINAPI WinMain( HINSTANCE hInstance, // handle to current instance
- HINSTANCE hPrevInstance, // handle to previous instance
- LPSTR lpCmdLine, // pointer to command line
- int nCmdShow // show state of window
- )
- {
- WNDCLASS wndclass;
- wndclass.cbClsExtra=0;
- wndclass.cbWndExtra=0;
- wndclass.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
- wndclass.hCursor=LoadCursor(NULL,IDC_ARROW);
- wndclass.hIcon=LoadIcon(NULL,IDI_ERROR);
- wndclass.hInstance=hInstance;
- wndclass.lpfnWndProc=textprom;
- wndclass.lpszClassName="text";
- wndclass.lpszMenuName=NULL;
- wndclass.style=CS_HREDRAW | CS_VREDRAW;
- if(!RegisterClass(&wndclass))
- {
- MessageBox(NULL,"create windows error!","error",MB_OK | MB_ICONSTOP);
- }
- HWND hwnd=CreateWindow("text","hellow world",WS_DLGFRAME | WS_MINIMIZEBOX | WS_SYSMENU,CW_USEDEFAULT,CW_USEDEFAULT,
- CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);
- ShowWindow(hwnd,nCmdShow);
- UpdateWindow(hwnd);
- MSG msg;
- while(GetMessage(&msg,NULL,0,0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- LRESULT CALLBACK textprom(
- HWND hwnd, // handle to window
- UINT uMsg, // message identifier
- WPARAM wParam, // first message parameter
- LPARAM lParam // second message parameter
- )
- {
- HDC hdc;
- PAINTSTRUCT ps;
- RECT rect;
- switch(uMsg)
- {
- case WM_PAINT:
- hdc=BeginPaint(hwnd,&ps);
- GetClientRect(hwnd,&rect);
- DrawText(hdc,"hellow my first windows program",strlen("hellow my first windows program"),&rect,
- DT_SINGLELINE | DT_CENTER | DT_VCENTER);
- EndPaint(hwnd,&ps);
- break;
- case WM_RBUTTONDOWN:
- hdc=GetDC(hwnd);
- TextOut(hdc,0,0,"success",strlen("success"));
- ReleaseDC(hwnd,hdc);
- break;
- case WM_RBUTTONUP:
- GetClientRect(hwnd,&rect);
- InvalidateRect(hwnd,&rect,true);
- break;
- case WM_DESTROY:
- PostQuitMessage(0);
- break;
- default:
- ;
- }
- return DefWindowProc(hwnd,uMsg,wParam,lParam);
- }