zoukankan      html  css  js  c++  java
  • lParam 和 wParam

    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. 弱智问题:请问什么是 lParam 和 wParam ?
    2. Windows Data Types
    3. what are the definitions for LPARAM and WPARAM?
    4. Message.LParam Property (System.Windows.Forms)
    5. WPARAM 与 LPARAM 的区别
    6. What do the letters W and L stand for in WPARAM and LPARAM ?
    7. What happens to WPARAM, LPARAM, and LRESULT when they travel between 32-bit and 64-bit windows ?
  • 相关阅读:
    codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) C. Constellation
    codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) B Guess the Permutation
    codeforces Wunder Fund Round 2016 (Div. 1 + Div. 2 combined) A Slime Combining(栈)
    在服务器IIS中添加新网站后打开网站需要用户名和密码时怎么办?
    简易通讯录
    先列出所有数据库,再删除其中一个数据库。
    数据库创建mysql_query($sql,$link)
    显示所有数据库 mysql_fetch_object()
    读取数据库数据
    PHP警告错误处理办法
  • 原文地址:https://www.cnblogs.com/zhaorui/p/2364867.html
Copyright © 2011-2022 走看看