孫鑫VC++講座筆記-(1)Windows程序內部運行機制

1,windows程序設計是種事件驅動方式的程序設計,主要基於消息的。當用戶需要完成某種功能時,需要調用OS某種支持,然後OS將用戶的需要包裝成消息,並投入到消息隊列中,最後應用程序從消息隊列中取走消息並進行響應。
2,消息結構:
typedef struct tagMSG {     // msg
    HWND   hwnd;     //接收消息的窗口句柄。和哪個窗口相關聯。
    UINT   message;  //消息標識。消息本身是什麼。
    WPARAM wParam;   //消息的附加信息。具體取決於消息本身。  
    LPARAM lParam;
    DWORD  time;     //消息投遞時間。 
    POINT  pt;       //消息投遞時,光標在屏幕上的位置。 
} MSG;

3,消息隊列:
每個應用程序OS都爲它建立一個消息隊列,消息隊列是個先進先出的緩衝區,其中每個元素都是一個消息,OS將生成的每個消息按先後順序放進消息隊列中,應用程序總是取走當前消息隊列中的第一條消息,應用程序取走消息後便知道用戶的操作和程序的狀態,然後對其處理即消息響應,消息響應通過編碼實現。

4,使用VC編程除了良好的C基礎外還需要掌握兩方面:
一,消息本身。不同消息所代表的用戶操作和應用程序的狀態。
二,對於某個特定的消息來說,要讓OS執行某個特定的功能去響應消息。

5,Window程序入口:
int WINAPI WinMain(
  HINSTANCE hInstance,  // 當前事例句柄。
  HINSTANCE hPrevInstance,  // 先前事例句柄。
  LPSTR lpCmdLine,      // 命令行指針
  int nCmdShow          // (窗口)顯示的狀態
);
說明:WinMain函數是Windows程序入口點函數,由OS調用,當OS啓動應用程序的時候,winmain函數的參數由OS傳遞的。

6,創建一個完整的窗口需要經過下面四個操作步驟:
一,設計一個窗口類;如:WNDCLASS wndcls;
二,註冊窗口類;    如:RegisterClass(&wndcls);
三,創建窗口;      如:CreateWindow(),CreateWindowEX();
四,顯示及更新窗口。如:ShowWindow(),UpdateWindow();

說明:創建窗口的時候一定要基於已經註冊的窗口類.

7,Windows提供的窗口類:
typedef struct _WNDCLASS {
    UINT    style;        //窗口的類型
    WNDPROC lpfnWndProc;  //窗口過程函數指針(回調函數)
    int     cbClsExtra; //窗口類附加字節,爲該類窗口所共享。通常0。
    int     cbWndExtra; //窗口附加字節。通常設爲0。
    HANDLE  hInstance;  //當前應用程序事例句柄。
    HICON   hIcon;      //圖標句柄 LoadIcon();
    HCURSOR hCursor;    //光標句柄 LoadCursor();
    HBRUSH  hbrBackground; //畫刷句柄 (HBRUSH)GetStockObject();
    LPCTSTR lpszMenuName;  //菜單名字
    LPCTSTR lpszClassName; //類的名字
} WNDCLASS;

8,窗口類註冊:
ATOM RegisterClass(
  CONST WNDCLASS *lpWndClass   // address of structure with class
                               // data
);

9,創建窗口:
HWND CreateWindow(
  LPCTSTR lpClassName,  // pointer to registered class name
  LPCTSTR lpWindowName, // pointer to window name
  DWORD dwStyle,        // window style
  int x,                // horizontal position of window
  int y,                // vertical position of window
  int nWidth,           // window width
  int nHeight,          // window height
  HWND hWndParent,      // handle to parent or owner window
  HMENU hMenu,          // handle to menu or child-window identifier
  HANDLE hInstance,     // handle to application instance
  LPVOID lpParam        // pointer to window-creation data
);

10,顯示和更新窗口窗口:
BOOL ShowWindow(
  HWND hWnd,     // handle to window
  int nCmdShow   // show state of window
);
BOOL UpdateWindow(
  HWND hWnd   // handle of window
);

11,消息循環:
MSG msg;
while(GetMessage(&msg,...))    //從消息隊列中取出一條消息
{
 TranslateMessage(&msg); //進行消息(如鍵盤消息)轉換
 DispatchMessage(&msg); //分派消息到窗口的回調函數處理,(OS調用窗口回調函數進行處理)。
}

其中:
//**The GetMessage function retrieves a message from the calling thread's message queue and places it in the specified structure.
//**If the function retrieves a message other than WM_QUIT, the return value is nonzero.If the function retrieves the WM_QUIT message, the return value is zero. If there is an error, the return value is -1.

BOOL GetMessage(
  LPMSG lpMsg,         // address of structure with message
  HWND hWnd,           // handle of window
  UINT wMsgFilterMin,  // first message
  UINT wMsgFilterMax   // last message
);


//The TranslateMessage function translates virtual-key messages into character messages. The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.
BOOL TranslateMessage(
  CONST MSG *lpMsg   // address of structure with message
);

//The DispatchMessage function dispatches a message to a window procedure.
LONG DispatchMessage(
  CONST MSG *lpmsg   // pointer to structure with message
);


12,窗口過程函數(回調函數)原型:
The WindowProc function is an application-defined function that processes messages sent to a window. The WNDPROC type defines a pointer to this callback function. WindowProc is a placeholder(佔位符) for the application-defined function name.

LRESULT CALLBACK WindowProc(  //這裏WindowProc是個代號名字。
  HWND hwnd,      // handle to window
  UINT uMsg,      // message identifier
  WPARAM wParam,  // first message parameter
  LPARAM lParam   // second message parameter
);

說明:兩種函數調用約定(__stdcall 和 __cdecl):
#define CALLBACK    __stdcall
//__stdcall 標準調用預定,是PASCAL 調用約定,象DELPHI使用的就是標準調用約定
#define WINAPIV     __cdecl 
// __cdecl 是C 語言形式的調用約定。


主要區別:函數參數傳遞順序 和 對堆棧的清除上。
問題:除了那些可變參數的函數調用外,其餘的一般都是__stdcall約定。但 C/C++編譯默然的是__cdecl約定。所以如果在VC等環境中調用__stdcall約定的函數,必須要在函數聲明的時加上 __stdcall 修飾符,以便對這個函數的調用是使用__stdcall約定(如使用DELPHI編寫的DLL時候)。
(VC中可通過這途徑修改:project|settings..|c/c++|...)


在窗口過程函數中通過一組switch語句來對消息進行處理:
如:
LRESULT CALLBACK WindowProc( 
  HWND hwnd,
  UINT uMsg,
  WPARAM wParam,
  LPARAM lParam  
)
{
    switch(uMsg)
    {
 case WM_PAINT:
  ...
  break;
 case ...
  break;
 case WM_CLOSE:
  //DestroyWindow(hwnd);
   //銷燬窗口,併發送WM_DESTROY消息。
  break;
 case WM_DESTROY:
  //PostQuitMessage(0);
  //發送WM_QUIT消息到消息隊列中,請求終止。
         //GetMessage()取到WM_QUIT消息後,返回0,退出消息循                //   環,從而終止應用程序。
  break;
 default:
  return DefWindowProc(hwnd,uMsg,wParam,lParam);
 //用缺省的窗口過程處理我們不感興趣的消息(其它消息)。
 //這是必須的。
    }//switch
 return 0;
}//WindowProc

13,DestroyWindow()函數和PostQuitMessage()函數原型:
//**The DestroyWindow function destroys the specified window. The function sends WM_DESTROY and WM_NCDESTROY messages。

BOOL DestroyWindow(
  HWND hWnd   // handle to window to destroy
);

//**The PostQuitMessage function indicates to the system that a thread has made a request to terminate (quit). It is typically used in response to a WM_DESTROY message.
//**The PostQuitMessage function posts a WM_QUIT message to the thread's message queue and returns immediately; the function simply indicates(預示,通知) to the system that the thread is requesting to quit at some time in the future.

When the thread retrieves the WM_QUIT message from its message queue, it should exit its message loop and return control to the system.

VOID PostQuitMessage(
  int nExitCode   // exit code
);

14,關於DC句柄獲取:
a)使用BeginPaint(),EndPaint()對。注意只能在響應WM_PAINT消息時使用。
b)使用GetDc(),ReleaseDC()對。注意他們不能在響應WM_PAINT中使用。

發佈了30 篇原創文章 · 獲贊 4 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章