wc(視窗類)

在Windows中運行的程式,大多數都有一個或幾個可以看得見的視窗,而在這些視窗被創建起來之前,作業系統怎么知道該怎樣創建該視窗,以及用戶操作該視窗的各種訊息交給誰處理呢?所以VC在調用Windows的API(CreateWindow或者CreateWindowEx)創建視窗之前,要求程式設計師必須定義一個視窗類(不是傳統C++意義上的類)來規定所創建該視窗所需要的各種信息,主要包括:視窗的訊息處理函式、視窗的風格、圖示、 滑鼠、選單等。

視窗類結構體WNDCLASSEX介紹:
一、結構體原型:
typedef struct{
UINTcbSize;
UINTstyle;
WNDPROClpfnWndProc;
intcbClsExtra;
intcbWndExtra;
HINSTANCEhInstance;
HICONhIcon;
HCURSORhCursor;
HBRUSHhbrBackground;
LPCTSTRlpszMenuName;
LPCTSTRlpszClassName;
HICONhIconSm;
} WNDCLASSEX, *PWNDCLASSEX;
二、成員介紹
1、cbSizeWNDCLASSEX的大小。在調用GetClassInfoEx前必須要先設定它值。我們可以用sizeof(WNDCLASSEX)來獲得準確的值。
2、style視窗類的樣式,它的值可以是視窗樣式值的任意組合。可以有以下的值:
Constant/value
Description
CS_BYTEALIGNCLIENT
0x1000
Aligns the window's client area on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
CS_BYTEALIGNWINDOW
0x2000
Aligns the window on a byte boundary (in the x direction). This style affects the width of the window and its horizontal placement on the display.
CS_CLASSDC
0x0040
Allocates one device context to be shared by all windows in the class. Because window classes are process specific, it is possible for multiple threads of an application to create a window of the same class. It is also possible for the threads to attempt to use the device context simultaneously. When this happens, the system allows only one thread to successfully finish its drawing operation.
CS_DBLCLKS
0x0008
Sends a double-click message to the window procedure when the user double-clicks the mouse while the cursor is within a window belonging to the class.
CS_DROPSHADOW
0x00020000
Enables the drop shadow effect on a window. The effect is turned on and off throughSPI_SETDROPSHADOW. Typically, this is enabled for small, short-lived windows such as menus to emphasize their Z order relationship to other windows.
CS_GLOBALCLASS
0x4000
Indicates that the window class is an application global class. For more information, see the "Application Global Classes" section ofAbout Window Classes.
CS_HREDRAW
0x0002
Redraws the entire window if a movement or size adjustment changes the width of the client area.
CS_NOCLOSE
0x0200
DisablesCloseon the window menu.
CS_OWNDC
0x0020
Allocates a unique device context for each window in the class.
CS_PARENTDC
0x0080
Sets the clipping rectangle of the child window to that of the parent window so that the child can draw on the parent. A window with theCS_PARENTDCstyle bit receives a regular device context from the system's cache of device contexts. It does not give the child the parent's device context or device context settings. SpecifyingCS_PARENTDCenhances an application's performance.
CS_SAVEBITS
0x0800
Saves, as a bitmap, the portion of the screen image obscured by a window of this class. When the window is removed, the system uses the saved bitmap to restore the screen image, including other windows that were obscured. Therefore, the system does not sendWM_PAINTmessages to windows that were obscured if the memory used by the bitmap has not been discarded and if other screen actions have not invalidated the stored image.
This style is useful for small windows (for example, menus or dialog boxes) that are displayed briefly and then removed before other screen activity takes place. This style increases the time required to display the window, because the system must first allocate memory to store the bitmap.
CS_VREDRAW
0x0001
Redraws the entire window if a movement or size adjustment changes the height of the client area.
3、lpfnWndProc指向視窗處理函式(回調函式)。處理視窗事件,像單擊滑鼠會怎樣,右擊滑鼠會怎樣,都是由此函式控制的。
4、cbClsExtra為視窗類的額外信息做記錄,初始化為0。
5、cbWndExtra記錄視窗實例的額外信息,系統初始為0.如果程式使用WNDCLASSEX註冊一個從資源檔案里創建的對話框,則此參數必須設定為DLGWINDOWEXTRA
6、hInstance:本模組的事例句柄。
7、hIcon視窗類的圖示,為資源句柄,如果設定為NULL,系統將為視窗提供一個默認的圖示。
8、hCursor視窗類的滑鼠樣式,為滑鼠樣式資源的句柄,如果設定為NULL,系統提供一個默認的滑鼠樣式。
9、hbrBackground視窗類的背景刷,為背景刷句柄,也可以為系統顏色值,如果顏色值已給出,則必須轉化為以下的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
10.lpszMenuName指向一個以NULL結尾的字元串,同目錄資源的名字一樣。如果使用整型id表示選單,可以用MAKEINTRESOURCE定義一個宏。如果它的值為NULL,那么該類創建的視窗將都沒有默認的選單。
11.lpszClassName指向視窗類的指針,LPSTR類型。
12.hIconSm小圖示的句柄,在系統列顯示的圖示,可以和上面的那個一樣。
三、實例
HWND hWND; //視窗句柄變數
MSG msg;//訊息結構體變數
WNDCLASSEX wndclass;//視窗類結構體變數
//以下是對wndclass各成員賦值
wndclass.cbSize = sizeof(WNDCLASSEX);
wndclass.cbClsExtra=0;
wndclass.cbWndExtra =0;
wndclass.lpfnWndProc = WinProc;
wndclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
wndclass.hInstance = hinstance;
wndclass.hCursor =LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon =LoadIcon(NULL,IDI_APPLICATION);
wndclass.hIconSm = LoadIcon(NULL,IDI_APPLICATION) ;
wndclass.lpszMenuName = NULL;
wndclass.hbrBackground =(HBRUSH) GetStockObject(BLACK_BRUSH);
wndclass.lpszClassName =WND_CLS_NAME;
//下面是利用wndcalss結構體的信息註冊一個視窗類
if (!RegisterClassEx(&wndclass))
return 0;
//……
return 0;

相關詞條

熱門詞條

聯絡我們