GetDlgCtrlID for top-level window with menu bar - return value

百般思念 提交于 2019-12-26 15:53:24

问题


MSDN -> "GetDlgCtrlID function" -> Remarks: "... Although GetDlgCtrlID may return a value if hwndCtl is a handle to a top-level window, top-level windows cannot have identifiers and such a return value is never valid."

It seems it is wrong information - "never valid".

At least for Win2k...Win8 this return value is just kernel pointer to hmenu(bar).

And my question is (primarily to MS insiders): why MSDN so inaccurate here?

(Screenshot: http://files.rsdn.ru/42164/gwl(-1)_tagwnd.png)


Upd (tagWND):

Also demo: http://files.rsdn.ru/42164/gwl(-1)_tagwnd.zip


回答1:


It is not inaccurate. You create a top-level window with CreateWindowEx(). Which looks like this:

HWND WINAPI CreateWindowEx(
  _In_      DWORD dwExStyle,
  _In_opt_  LPCTSTR lpClassName,
  _In_opt_  LPCTSTR lpWindowName,
  _In_      DWORD dwStyle,
  _In_      int x,
  _In_      int y,
  _In_      int nWidth,
  _In_      int nHeight,
  _In_opt_  HWND hWndParent,
  _In_opt_  HMENU hMenu,
  _In_opt_  HINSTANCE hInstance,
  _In_opt_  LPVOID lpParam
);

Note how you don't specify the ID anywhere. But the fine print is in the description for the hMenu argument:

A 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.

So you can have a menu OR a child ID. Overloaded, pretty common in the winapi, a child control can't have a menu and a toplevel window can't have a child ID. If you forge ahead and ignore this and call GetDlgCtrlID() on a toplevel window anyway then you will get back the value of the hMenu argument you specified in the create call. Well, today, always follow the api or you might get a rude surprise some day, you'd of course use GetMenu() instead.



来源:https://stackoverflow.com/questions/25641251/getdlgctrlid-for-top-level-window-with-menu-bar-return-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!