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++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性