C++ WPARAM 和 LPARAM 参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6339793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPARAM and LPARAM parameters
提问by Adrian
When passing a value to a function that takes both a WPARAM and a LPARAM parameter, does it matter on which of them I pass it? Someone told me that if I use Windows x64 I should use WPARAM; is this true?
将值传递给同时接受 WPARAM 和 LPARAM 参数的函数时,我传递其中的哪个参数重要吗?有人告诉我,如果我使用 Windows x64,我应该使用 WPARAM;这是真的?
回答by Aaron Klotz
When sending messages, WPARAM
and LPARAM
parameters have specific interpretations depending on the message. You need to pass those parameters in the way that the message that you are sending expects them to be passed. If you are defining your own message (perhaps via an offset from WM_USER
, WM_APP
, or RegisterWindowMessage
), then you obviously have a bit more latitude.
发送信息时,WPARAM
和LPARAM
参数具有取决于消息特定解释。您需要以您发送的消息期望它们被传递的方式传递这些参数。如果您要定义自己的消息(可能通过一个由偏移WM_USER
,WM_APP
或RegisterWindowMessage
),那么你显然有一点更多的自由。
In the days of 16-bit Windows, a WPARAM
was a 16-bit word, while LPARAM
was a 32-bit long. These distinctions went away in Win32; they both became 32-bit values.
在 16 位 Windows 时代, aWPARAM
是 16 位字,而LPARAM
是 32 位长。这些区别在 Win32 中消失了;它们都变成了 32 位值。
According to this, LPARAM
is defined as LONG_PTR
, which in 64-bit Windows is a signed, 64-bit value. WPARAM
is defined as UINT_PTR
, which in 64-bit Windows is an unsigned, 64-bit value. If you are defining your own message, you might want to assign its parameters accordingly.
根据此,LPARAM
被定义为LONG_PTR
,其中在64位被一个签名的Windows的64位值。WPARAM
定义为UINT_PTR
,它在 64 位 Windows 中是一个无符号的 64 位值。如果您正在定义自己的消息,您可能希望相应地分配其参数。
回答by Ian Boyd
| for handles | for pointers |
| and numbers | |
| OS | WPARAM | LPARAM |
|----------------|-----------------|---------------|
| 16-bit Windows | 16-bit unsigned | 32-bit signed |
| 32-bit Windows | 32-bit unsigned | 32-bit signed |
| 64-bit Windows | 64-bit unsigned | 64-bit signed |
The history of its definition has changed over the years.
多年来,其定义的历史已经发生了变化。
WINDOWS.H(Windows 2.03 SDK, c. 1988)
WINDOWS.H(Windows 2.03 SDK,c. 1988)
/* Message structure */
typedef struct tagMSG {
HWND hwnd;
WORD message;
WORD wParam;
LONG lParam;
DWORD time;
POINT pt;
} MSG;
WinDefs.h(c. 1999)
WinDefs.h(c. 1999)
/* Types use for passing & returning polymorphic values */
typedef UINT WPARAM;
typedef LONG LPARAM;
typedef LONG LRESULT;
WinDef.h(c. 2005)
WinDef.h(c. 2005)
/* Types use for passing & returning polymorphic values */
typedef UINT_PTR WPARAM;
typedef LONG_PTR LPARAM;
typedef LONG_PTR LRESULT;
Bonus Reading
奖励阅读
- What do the letters W and L stand for in WPARAM and LPARAM?(
W
is for unsigned 16-bitWORD
, andL
is for signed 32-bitLONG
) - What happens to WPARAM, LPARAM, and LRESULT when they travel between 32-bit and 64-bit windows?(the unsigned is zero-extended, the signed is sign-extended)
- WPARAM 和 LPARAM 中的字母 W 和 L 代表什么?(
W
适用于无符号 16 位WORD
,L
适用于有符号 32 位LONG
) - 当 WPARAM、LPARAM 和 LRESULT 在 32 位和 64 位窗口之间移动时,它们会发生什么变化?(无符号是零扩展,有符号是符号扩展)
回答by lunixbochs
It's message-specific. You can use this list of system-defined message categoriesas reference. Select a group, then a message from the group to see what the message specifies you should pass as WPARAM/LPARAM.
它是特定于消息的。您可以使用此系统定义的消息类别列表作为参考。选择一个组,然后从该组中选择一条消息以查看该消息指定您应该作为 WPARAM/LPARAM 传递的内容。
Raymond Chen explains why we have two params.
Raymond Chen 解释了为什么我们有两个参数。
回答by seand
Yes, the order matters. WPARAM
sent/posted in one side winds up WPARAM
on the other end; likewise for LPARAM
.
是的,顺序很重要。 WPARAM
在一侧发送/张贴在WPARAM
另一端结束;同样对于LPARAM
.
If you have your own custom messages you can use WPARAM
and LPARAM
for anything you want. (There may be some common conventions though.)
如果您有自己的自定义消息,则可以使用WPARAM
并LPARAM
用于您想要的任何内容。(虽然可能有一些共同的约定。)
回答by LeleDumbo
Yes, it does. I once passed them in swapped order and the function I call fails. Nevertheless, you should consult MSDN when in doubt. I never program in Win64 though, so I don't whether there are differences between Win32 and Win64 regarding WPARAM
/LPARAM
behavior.
是的,它确实。我曾经以交换的顺序传递它们,我调用的函数失败了。不过,您应该在有疑问时咨询 MSDN。不过,我从来没有在 Win64 中编程,所以我不知道 Win32 和 Win64 在WPARAM
/LPARAM
行为方面是否存在差异。