1. 程式人生 > >WPARAM和LPARAM的含義

WPARAM和LPARAM的含義

lParam 和 wParam 是巨集定義,一般在訊息函式中帶這兩個型別的引數,通常用來儲存視窗訊息的引數。
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
wParam 通常用來儲存小段資訊,如,標誌
lParam 通常用於儲存訊息所需的物件
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
typedef unsigned int UINT;
typedef long LONG;
typedef UINT WPARAM;
typedef LONG LPARAM;
lParam 和 wParam 是 Win16 系統遺留下來的產物,在 Win16 API 中 WndProc 有兩個引數,一個 WORD 型別的 16 位整型變數,另一個是 LONG 型別的 32 位整型變數。根據匈牙利命名法(Hungarian notation),16 位的變數就被命名為 wParam,而 32 位的變數就被命名為 lParam。
到了 Win32 API 中,原來的 16 位變數也被擴充套件為 32 位,因此 lParam 和 wParam 的大小完全相同。在 Win32 API 的早期,為了保證和 Win16 API 的程式碼相容,MS 定義了 MPARAM 和 LPARAM 兩個巨集。保留 w 字首的原因一方面是由於 WPARAM 巨集也以 W 開頭,另一方面是希望提醒程式設計師注意到可移植性。到了現在,Win16 早已退出歷史舞臺,但是這個字首仍然約定俗成的沿用了下來。
the history of WPARAM, LPARAM
Once upon a time, Windows was 16-bit. Each message could carry with it two pieces of data, called WPARAM and LPARAM. The first one was a 16-bit value (“word”), so it was called W. The second one was a 32-bit value (“long”), so it was called L.
You used the W parameter to pass things like handles and integers. You used the L parameter to pass pointers.
When Windows was converted to 32-bit, the WPARAM parameter grew to a 32-bit value as well. So even though the “W” stands for “word”, it isn’t a word any more. (And in 64-bit Windows, both parameters are 64-bit values!)
It is helpful to understand the origin of the terms. If you look at the design of window messages, you will see that if the message takes a pointer, the pointer is usually passed in the LPARAM, whereas if the message takes a handle or an integer, then it is passed in the WPARAM. (And if a message takes both, the integer goes in the WPARAM and the pointer goes in the LPARAM.)
“在Win 3.x中,WPARAM是16位的,而LPARAM是32位的,兩者有明顯的區別。因為地址通常是32位的,所以LPARAM 被用來傳遞地址,這個習慣在Win32 API中仍然能夠看到。在Win32 API中,WPARAM和LPARAM都是32位,所以沒有什麼本質的區 別。Windows的訊息必須參考幫助檔案才能知道具體的含義。如果是你定義的訊息,願意怎麼使這兩個引數都行。但是習慣上,我們願意使用LPARAM傳 遞地址,而WPARAM傳遞其他引數。”
在 MSDN 網站中關於 Windows Data Types 中有如下定義:
LPARAM: A message parameter. This type is declared in WinDef.h as follows: typedef LONG_PTR LPARAM;
WPARAM: A message parameter. This type is declared in WinDef.h as follows: typedef UINT_PTR WPARAM;
LPARAM is a typedef for LONG_PTR which is a long (signed 32-bit) on win32 and __int64 (signed 64-bit) on x86_64.
WPARAM is a typedef for UINT_PTR which is an unsigned int (unsigned 32-bit) on win32 and unsigned __int64 (unsigned 64-bit) on x86_64
(x86_64 is how Microsoft now refer to amd64)
In c#, you should use IntPtr for LPARAM and UIntPtr for WPARAM.
在 C# 與 C++ 的互操作中,可以使用 IntPtr 來宣告 LPARAM 型別變數,使用 UIntPtr 來宣告 WPARAM 型別的變數。
當 WPARAM, LPARAM 和 LRESULT 在 32 位和 64 位 Windows 系統中傳遞的時候會發生什麼?
如果是從 64 位 Windows 系統到 32 位系統,那麼只有一個選擇:截斷 truncation。
如果是從 32 位到 64 位,那麼對 WPARAM 採用補零擴充套件(zero-extended),對 LPARAM 和 LRESULT 採用符號擴充套件 (sign-extended)。
擴充套件方式不同的原因主要是因為 WPARAM 被定義為 “字 (WORD)” 也就是 “UINT_PTR”,而 LPARAM 和 LRESULT 被定義為 “LONG”,也就是 “LONG_PTR”。
What happens to WPARAM, LPARAM, and LRESULT when the travel between 32-bit and 64-bit windows?
The integral types WPARAM, LPARAM, and LRESULT are 32 bits wide on 32-bit systems and 64 bits on 64-bit systems. What happens when a 32-bit process sends a message to a 64-bit window or vice versa ?
There’s really only one choice when converting a 64-bit value to a 32-bit value: Truncation. When a 64-bit process sends a message to a 32-bit window, the 64-bit WPARAM and LPARAM values are truncated to 32 bits. Similarly, when a 64-bit window returns an LRESULT back to a 32-bit sender, the value is truncate.
But converting a 32-bit value to a 64-bit value includes a choice: Do you zero-extend or sign-extend?
The answer is obvious if you remember the history of WPARAM, LPARAM, and LRESULT or if you just look at the header file.
The WPARAM is zero-extend, while LPARAM and LRESULT are sign-extended.
If you remember that WPARAM used to be a WORD and LPARAM and LRESULT used to be LONG, then this follows from the fact that WORD is an unsigned type (therefore zero-extended) and LONG is a signed type (therefore sign-extend).
Even if you didn’t know that, you could look it up in the header file.
typedef UINT_PTR WPARAM;
typedef LONG_PTR LPARAM;
typedef LONG_PTR LRESULT;
UINT_PTR is an unsigned type (therefore zero-extended) and LONG_PTR is a signed type (therefore sign-extended).

具體的訊息表示

  1. WM_PAINT訊息,LOWORD(lParam)是客戶區的寬,HIWORD(lParam)是客戶區的高

  2. 滾動條WM_VSCROLL或WM_HSCROLL訊息,LOWORD(wParam)指出了滑鼠對滾動條的操作。比如上、下、左、右、翻頁、移動等。

  3. 擊鍵訊息,有WM_SYSKEYDOWN、WM_SYSKEYUP、WM_KEYUP、WM_KEYDOWN,其中wParam是虛擬鍵程式碼,lParam是包含屬於擊鍵的其他資訊。lParam訊息引數分為6個域,有重複計數、環境程式碼、鍵的先前狀態等。4. 字元訊息WM_CHAR、WM_DEADCHAR、WM_SYSCHAR、WM_SYSDEADCHAR,lParam訊息引數跟擊鍵訊息的lParam 訊息引數內容相同,wParam引數是ANSI或Unicode字元程式碼

  4. 客戶區滑鼠訊息WM_LBUTTONDOWN、WM_LBUTTONUP、WM_RBUTTONDOWN、WM_RBUTTONUP、 WM_MBUTTONDOWN、WM_MBUTTONUP,lParam引數的低位是滑鼠的客戶區x座標,高位是客戶區y座標。wParam引數是指示滑鼠鍵及Shift和Ctrl鍵的狀態。wParam&MK_SHIFT或MK_CTRL,如果返回TRUE就意味著有按下Shift或Ctrl 鍵。

  5. 非客戶區訊息,wParam引數指明移動或者單擊滑鼠鍵的非客戶區位置,以HT開頭,lParam引數低位指出了滑鼠所在螢幕座標的x座標,高位指出了滑鼠所在螢幕座標的y座標。

  6. 滑鼠輪滾動訊息,WM_MOUSEWHEEL訊息,lParam將獲得滑鼠的螢幕位置(座標),wParam引數的低位表明滑鼠鍵和Shift與Ctrl 鍵的狀態。wParam高位有一個“delta”值,該值可正可負,指出了滾輪導致螢幕滾動幾行,120表示向上3行。

  7. 計時器訊息WM_TIMER,wParam引數等於計時器的ID值,lParam為0

  8. 按鈕子視窗的WM_COMMAND訊息,wParam引數的低位是子視窗ID,高位是通知碼, lParam引數是接收訊息的子視窗的控制代碼。

  9. 焦點訊息,對於正在失去焦點的視窗,會收到WM_KILLFOCUS訊息,其wParam引數是即將接收輸入焦點的視窗的控制代碼。對於即將獲取焦點的視窗,會收到WM_SETFOCUS訊息,其wParam引數是正在失去焦點的視窗的控制代碼。11. 編輯控制的WM_COMMAND訊息,wParam引數的低位是子視窗ID,高位是通知碼, lParam引數是子視窗控制代碼。12. 列表框的WM_COMMAND訊息,wParam引數的低位是子視窗ID,高位是通知碼, lParam引數是子視窗控制代碼。13. 選單訊息1,WM_INITMENU,wParam是主選單控制代碼,lParam是0.

  10. 選單訊息2,WM_MENUSELECT,選單跟蹤訊息,指標移到選單的某一些,就會發送這個訊息給視窗過程,其wParam引數的低位是選中項選單的 ID或者彈出式選單的控制代碼,高位是選擇標識,lParam引數是包含選中項的選單控制代碼。

  11. 選單訊息3,WM_INITMENUPOPUP,準備顯示一個彈出式選單時產生的訊息,wParam引數是彈出式選單的控制代碼,lParam的低位是彈出式選單的索引,如果該選單是系統選單,那麼高位是1,否則為0.

  12. 選單訊息4,WM_COMMAND,選中選單後產生,wParam低位是擊中選單的ID,高位是0,lParam引數也是0

  13. 選單訊息5,WM_SYSCOMMAND,表示使用者從系統選單中選擇一個啟用的選單項,其wParam引數是選單的ID, lParam為0.如果該訊息是由按滑鼠產生的,那麼lParam引數是滑鼠的螢幕座標。

  14. 加速鍵訊息,WM_COMMAND訊息,wParam低位是加速鍵ID,高位是1, lParam是0.

19.控制項著色訊息,WM_CTLCOLORBTN訊息,wParam是按鈕的裝置描述表控制代碼,lParam是按鈕的視窗控制代碼。