windows編程--建立視窗

來源:互聯網
上載者:User

標籤:des   style   blog   color   os   使用   io   strong   for   

第一個win32程式,簡單的建立視窗:

#include <windows.h> LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,                    PSTR szCmdLine, int iCmdShow){     static TCHAR szAppName[] = TEXT ("HelloWin") ;     HWND         hwnd ;     MSG          msg ;     WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;     wndclass.lpfnWndProc   = WndProc ;     wndclass.cbClsExtra    = 0 ;     wndclass.cbWndExtra    = 0 ;     wndclass.hInstance     = hInstance ;     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;     if (!RegisterClass (&wndclass))     {          MessageBox (NULL, TEXT ("This program requires Windows NT!"),                       szAppName, MB_ICONERROR) ;          return 0 ;     }          hwnd = CreateWindow (szAppName,                  // window class name                          TEXT ("The Hello Program"), // window caption                          WS_OVERLAPPEDWINDOW,        // window style                          CW_USEDEFAULT,              // initial x position                          CW_USEDEFAULT,              // initial y position                          CW_USEDEFAULT,              // initial x size                          CW_USEDEFAULT,              // initial y size                          NULL,                       // parent window handle                          NULL,                       // window menu handle                          hInstance,                  // program instance handle                          NULL) ;                     // creation parameters          ShowWindow (hwnd, iCmdShow) ;     UpdateWindow (hwnd) ;          while (GetMessage (&msg, NULL, 0, 0))     {          TranslateMessage (&msg) ;          DispatchMessage (&msg) ;     }     return msg.wParam ;}LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){     HDC         hdc ;     PAINTSTRUCT ps ;     RECT        rect ;          switch (message)     {     case WM_CREATE:return 0 ;               case WM_PAINT:          hdc = BeginPaint (hwnd, &ps) ;                    GetClientRect (hwnd, &rect) ;                    DrawText (hdc, TEXT ("Hello, Windows Program!"), -1, &rect,                    DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;                    EndPaint (hwnd, &ps) ;          return 0 ;               case WM_DESTROY:          PostQuitMessage (0) ;          return 0 ;     }     return DefWindowProc (hwnd, message, wParam, lParam) ;}

視窗是由視窗類別建立的即WNDCLASS,視窗類別確定了處理視窗訊息的視窗過程。

MSDN中是這樣定義的:

typedef struct {  UINT      style;  WNDPROC   lpfnWndProc;  int       cbClsExtra;  int       cbWndExtra;  HINSTANCE hInstance;  HICON     hIcon;  HCURSOR   hCursor;  HBRUSH    hbrBackground;  LPCTSTR   lpszMenuName;  LPCTSTR   lpszClassName;} WNDCLASS, *PWNDCLASS;

在程式中,我們一般這樣定義和初始化

  WNDCLASS     wndclass ;     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;     wndclass.lpfnWndProc   = WndProc ;     wndclass.cbClsExtra    = 0 ;     wndclass.cbWndExtra    = 0 ;     wndclass.hInstance     = hInstance ;     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;     wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;     wndclass.lpszMenuName  = NULL ;     wndclass.lpszClassName = szAppName ;

wndclass.style欄位表示類的風格,具體參加MSDN:

Style Action
CS_BYTEALIGNCLIENT

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

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 Allocates one device context to be shared by all windows in the class. 
CS_DBLCLKS

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

Windows XP: Enables the drop shadow effect on a window.

The effect is turned on and off through SPI_SETDROPSHADOW. 

CS_GLOBALCLASS

Specifies that the window class is an application global class.

For more information, see Application Global Classes.

CS_HREDRAW Redraws the entire window if a movement or size adjustment changes the width of the client area.
CS_NOCLOSE Disables Close on the window menu.
CS_OWNDC Allocates a unique device context for each window in the class.
CS_PARENTDC

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 the CS_PARENTDC style bit receives a regular device context from the system‘s cache of device contexts.

CS_SAVEBITS Saves, as a bitmap, the portion of the screen image obscured by a window of this class. 
CS_VREDRAW Redraws the entire window if a movement or size adjustment changes the height of the client area.

我們這裡選擇的是CS_HREDRAW | CS_VREDRAW,表示無論垂直還是水平尺寸的改變,視窗都將重繪。

wndclass.lpfnWndProc表示視窗過程設定,指向一個函數WndProc。

wndclass.cbClsExtra和cbWndExtra用於維護內部視窗結構中一些預留的空間,可以根據需要來使用這些預留的空間。

wndclass.hInstance表示應用程式的執行個體控制代碼即WinMain第一個參數。

wndclass.hIcon為該視窗類別設定表徵圖。若要使用本地的表徵圖,第一個參數必須為hInstance執行個體控制代碼,第二個參數為表徵圖標示,對於預定義的表徵圖以IDI開頭。

wndclass.hCursor與上面類似,載入滑鼠指標。

wndclass.hbrBackground指定背景色,這裡我們用白色畫刷。

wndclass.lpszMenuName即視窗菜單。

wndclass.lpszClassName為視窗類別指定名字。

這樣視窗類別的初始化完成,使用RegisterClass註冊類,下面就是視窗的建立了,

使用createWindow函數,MSDN定義如下:

HWND CreateWindow(  __in  LPCTSTR lpClassName,   //視窗名稱  __in  LPCTSTR lpWindowName,  //視窗標題  __in  DWORD dwStyle,      //視窗風格  __in  int x,           //x座標  __in  int y,            //y座標  __in  int nWidth,         //長度  __in  int nHeight,       //高度   __in  HWND hWndParent,     //父視窗控制代碼  __in  HMENU hMenu,            //視窗菜單控制代碼  __in  HINSTANCE hInstance,    //程式執行個體控制代碼  __in  LPVOID lpParam          //建立參數);

CreateWindow只是在內部建立視窗,即windows分配了一片記憶體,要想顯示視窗還要使用ShowWindow函數。

第一個參數是CreateWindow建立的視窗控制代碼,第二個是WinMain接受的iCmdShow值,該參數決定視窗初始顯示格式。

UpdateWindow是重繪視窗,這是通過發送一條WM_PAINT訊息完成的。

最後則是訊息的迴圈,即不斷接受訊息,當有訊息時調用WndProc函數進行處理。

 

windows編程--建立視窗

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.