VC++编程 请高人解释一个语句

来源:百度知道 编辑:UC知道 时间:2024/06/23 05:11:50
switch (message)
{
case WM_CREATE :
for (x = 0 ; x < DIVISIONS ; x++)
for (y = 0 ; y < DIVISIONS ; y++)
hwndChild[x][y] = CreateWindow (szChildClass, NULL,
WS_CHILDWINDOW | WS_VISIBLE,
0, 0, 0, 0,
hwnd, (HMENU) (y << 8 | x),
(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE),
NULL) ;

尤其是
(HMENU) (y << 8 | x),实在不明白这个

感激不尽~~~~

(y << 8 | x)的运算含义前面两位已经说的很清楚了,将y的值向左移动8位然后与x的值或操作。

那么它的动机是什么呢,就将x和y这两个8位整数,用一个16位的整数来保存,将来可用下面的代码解码出x,y的值(假设z为那个16位值)。
x = z & 8;
y = (z>>8) & 8;
也就是说,x,y代表的子窗口的索引,需要把它存起来。

至于CreateWindow函数的(HMENU) (y << 8 | x)参数,当创建子窗口时,它其实表示的并不是窗口句柄,而是窗口id。(这是win32API接口定义的那个年代,为了节约资源,滥用糟糕的重复定义接口参数的编程技巧,极大的降低代码可读性,所以说微软也产垃圾代码的时候)

hMenu
[in] Handle to a menu, or specifies a child-window identifier, depending on the window style. For an overlapped or pop-up window, hMenu identifies the menu to be used with the window; it can be NULL if the class menu is to be used. For a child window, hMenu specifies the child-window identifier, an integer value used by a dialog box control to notify its parent about events. The application determines the child-window identifier; it must be unique for all child windows with the same parent window.

这个id可以在子窗口发往父窗口的消息中获得,也可以通过子窗口句柄得到这个id,于是此时,就可以得到标识窗口的x,y值了。
其实,这样的方式,同样一种很不好的编程风格,