HIWORD和LOWORD

HIWORD和LOWORD

说说 LoWord 和 HiWord 的用途:

譬如在一个鼠标消息的消息参数 lParam 中存放着鼠标位置. lParam 是 4 字节的, 它的低两位存放 x、高两位存放 y ...


在使用win32的一些特殊宏时,有必要注意一下宏的定义,例如:

#define MAKEWORD(a, b)      ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) | ((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8))

#define MAKELONG(a, b)      ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) | ((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16))

#define LOWORD(l)           ((WORD)((DWORD_PTR)(l) & 0xffff))

#define HIWORD(l)           ((WORD)((DWORD_PTR)(l) >> 16))

#define LOBYTE(w)           ((BYTE)((DWORD_PTR)(w) & 0xff))

#define HIBYTE(w)           ((BYTE)((DWORD_PTR)(w) >> 8))

使用:

xPos = LOWORD(lParam);

yPos = HIWORD(lParam);

/********************************************************************/

从上面加粗部分可以看出 LOWORD 和 HIWORD 返回值是WORD型,即unsigned short,因此不能表示负数,在使用的时候可能就要注意了。

对于一般情况,用它们是没有问题的,但有一个特例,就是利用它们取当前鼠标的坐标时,因为鼠标有时候会移出窗口,因此我们得到的值本应该是负数,但是利用这两个宏得出的值始终是正值。我们可以用以下的宏来代替。

 

#define GET_X_LPARAM(lp)                        ((int)(short)LOWORD(lp))

#define GET_Y_LPARAM(lp)                        ((int)(short)HIWORD(lp))

 

其头文件是:#include <windowsx.h>

 

使用:

xPos = GET_X_LPARAM(lParam);

yPos = GET_Y_LPARAM(lParam);


http://blog.csdn.net/woyaowenzi/article/details/4608148



HIBYTE
The HIBYTE macro retrieves the high-order byte from the given 16-bit value. 

HIWORD
The HIWORD macro retrieves the high-order word from the given 32-bit value. 

LOBYTE
The LOBYTE macro retrieves the low-order byte from the specified value. 

LOWORD
The LOWORD macro retrieves the low-order word from the specified value. 

MAKELONG
The MAKELONG macro creates a LONG value by concatenating the specified values. 

MAKELPARAM
The MAKELPARAM macro creates a value for use as an lParam parameter in a message. The macro concatenates the specified values. 

MAKELRESULT
The MAKELRESULT macro creates a value for use as a return value from a window procedure. The macro concatenates the specified values. 

MAKEWORD
The MAKEWORD macro creates a WORD value by concatenating the specified values. 

MAKEWPARAM
The MAKEWPARAM macro creates a value for use as a wParam parameter in a message. The macro concatenates the specified values.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值