查看文章 |
WNDCLASSEX结构
2007-11-05 16:17
![]() UINT cbSize; UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINSTANCE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; HICON hIconSm; } WNDCLASSEX, *PWNDCLASSEX; cbSize Specifies the size, in bytes, of this structure. Set this member to sizeof(WNDCLASSEX). Be sure to set this member before calling the GetClassInfoEx function. 通常情况下,只要将此参数设置为sizeof(WNDCLASSEX)即可,如果程序中用到了GetClassInfoEx函数,那么在使用前请一定先设置cbSize的值, style Specifies the class style(s). This member can be any combination of the Class Styles. 指定一个样式,它的值可以是窗口样式的任意组合,在这里我们将其设置为CS_HREDRAW | CS_VREDRAW,当然它还有其它取值如 CS_VREDRAW CS_HREDRAW CS_DBLCLKS CS_OWNDC CS_CLASSDC CS_PARENTDC CS_NOCLOSE CS_SAVEBITS CS_BYTEALIGNCLIENT CS_BYTEALIGNWINDOW CS_GLOBALCLASS lpfnWndProc Pointer to the window procedure. You must use the CallWindowProc function to call the window procedure. For more information, see WindowProc. 此参数为指定一个回调函数,我们先定义一个回调函数如下 //回调函数 LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) { switch(Msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,Msg,wParam,lParam); } 然后将此参数设置为回调函数名,WinProc cbClsExtra Specifies the number of extra bytes to allocate following the window-class structure. The system initializes the bytes to zero. 指定一个数值,记录窗口类额外的信息,默认为0 cbWndExtra Specifies the number of extra bytes to allocate following the window instance. The system initializes the bytes to zero. If an application uses WNDCLASSEX to register a dialog box created by using the CLASS directive in the resource file, it must set this member to DLGWINDOWEXTRA. 指定一个数值,记录窗口实例的额外信息,默认为0;如果程序使用WNDCLASSEX注册一个从资源文件里创建的对话框,则此参数必须设置为DLGWINDOWEXTRA,这里我也将其设置为0 hIcon Handle to the class icon. This member must be a handle to an icon resource. If this member is NULL, the system provides a default icon. 窗口类的图标,此参数ICON资源句柄,如果设置为NULL,操作系统将提供一个默认的图标,这里我指定一个ICON,LoadIcon(NULL,MAKEINTRESOURCE(IDI_QUESTION)); hCursor Handle to the class cursor. This member must be a handle to a cursor resource. If this member is NULL, an application must explicitly set the cursor shape whenever the mouse moves into the application's window. 窗口类的鼠标样式,同样,该参数为指向CURSOR资源的句柄,如果为NULL,则当任何时候鼠标进入窗口区域时必须明确指定鼠标形状,此处将其设置为LoadCursor(NULL,MAKEINTRESOURCE(IDC_HAND)); hbrBackground Handle to the class background brush. This member can be a handle to the physical brush to be used for painting the background, or it can be a color value. A color value must be one of the following standard system colors (the value 1 must be added to the chosen color). If a color value is given, you must convert it to one of the following HBRUSH types: 背景刷的句柄,即可以指向物理笔刷又可以为一个颜色值,该值必须为标准系统颜色,如果颜色值已经给出,则必须转化为以下给出的HBRUSH类型; COLOR_ACTIVEBORDER COLOR_ACTIVECAPTION COLOR_APPWORKSPACE COLOR_BACKGROUND COLOR_BTNFACE COLOR_BTNSHADOW COLOR_BTNTEXT COLOR_CAPTIONTEXT COLOR_GRAYTEXT COLOR_HIGHLIGHT COLOR_HIGHLIGHTTEXT COLOR_INACTIVEBORDER COLOR_INACTIVECAPTION COLOR_MENU COLOR_MENUTEXT COLOR_SCROLLBAR COLOR_WINDOW COLOR_WINDOWFRAME COLOR_WINDOWTEXT The system automatically deletes class background brushes when the class is unregistered by using UnregisterClass. An application should not delete these brushes. 当使用UnregisterClass函数注销一个窗口类的时候,系统会自动删除该类的笔刷,程序运行中不可删除 When this member is NULL, an application must paint its own background whenever it is requested to paint in its client area. To determine whether the background must be painted, an application can either process the WM_ERASEBKGND message or test the fErase member of the PAINTSTRUCT structure filled by the BeginPaint function. 如果此成员值为NULL,那么在必要的时候客户端必须自己执行渲染.确认什么时候渲染背景,你可通过BeginPaint函数提取WM_ERASEBKGND 消息或测试PAINTSTRUCT结构中fErase的值; 这里,我们将其值设置为(HBRUSH)GetStockObject(WHITE_BRUSH); lpszMenuName Pointer to a null-terminated character string that specifies the resource name of the class menu, as the name appears in the resource file. If you use an integer to identify the menu, use the MAKEINTRESOURCE macro. If this member is NULL, windows belonging to this class have no default menu. 指向一个以NULL结尾的字符串,同资源中的名字一样,作为窗口菜单的名字.如果要使用整型ID标识菜单,可以使用MAKEINTRESOURCE定义一个宏.如果它的值为NULL,那么该类创建的窗口将都没有默认的菜单 为了简单,我们将其设置为NULL,即没有菜单 lpszClassName Pointer to a null-terminated string or is an atom. If this parameter is an atom, it must be a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpszClassName; the high-order word must be zero. If lpszClassName is a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names. The maximum length for lpszClassName is 256. 窗口类的名字,我们可以随便取,值类型为一个字符串 hIconSm Handle to a small icon that is associated with the window class. If this member is NULL, the system searches the icon resource specified by the hIcon member for an icon of the appropriate size to use as the small icon.在任务栏时的图标,值设置同hIcon,这里我们设置为LoadIcon(NULL,IDI_APPLICATION); 至此我们的窗口类已经设计完毕,完整代码如下 #include <windows.h> //回调函数 LRESULT WINAPI WinProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam) { switch(Msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,Msg,wParam,lParam); } //主函数 int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd) { char *cName = "cName"; WNDCLASSEX wc; HWND hWnd; MSG Msg; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.cbSize = sizeof(WNDCLASSEX); wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //GetStockObject wc.hCursor = LoadCursor(NULL,MAKEINTRESOURCE(IDC_HAND)); //MAKEINTRESOURCE wc.hIcon = LoadIcon(NULL,MAKEINTRESOURCE(IDI_QUESTION)); //IDI_QUESTION wc.hIconSm = LoadIcon(NULL,IDI_APPLICATION); //IDI_APPLICATION wc.hInstance = hInstance; wc.lpfnWndProc = WinProc; wc.lpszClassName =(LPSTR)cName; wc.lpszMenuName = NULL; wc.style = CS_HREDRAW | CS_VREDRAW; //cs_classdc,cs_parentdc,cs_hredraw,cs_vredraw,cs_owndc,cs_dblclks RegisterClassEx(&wc); hWnd = CreateWindowEx(WS_EX_CLIENTEDGE,cName,"cName Window",WS_OVERLAPPEDWINDOW, 0,0,600,400,NULL,NULL,hInstance,NULL); if(hWnd == NULL) { MessageBox(NULL,"There's an Error","Error Title",MB_ICONEXCLAMATION|MB_OK); return 0; } ShowWindow(hWnd,nShowCmd); UpdateWindow(hWnd); while(GetMessage(&Msg,NULL,0,0)) { TranslateMessage(&Msg); DispatchMessage(&Msg); } return Msg.message; } ![]() 运行后可以看到效果如下图,一个白色背景窗口, ![]() |


