您的位置:首页 > 编程语言 > C语言/C++

HIWORD和LOWORD

2016-02-07 11:08 931 查看

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 和 [b]HIWORD [b]返回值是WORD型,即unsigned short,因此不能表示负数,在使用的时候可能就要注意了。[/b][/b]

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

 

#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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++