MSDN文档介绍GetMenuBarInfo是用来检索有关指定菜单栏的信息。
假如有个需求是要找到菜单下拉菜单的矩形大小,该怎么做呢?
最简单的方法就是获取菜单栏的句柄,然后将句柄作为参数传给GetMenuBarInfo函数。
HWND myhWnd = FindWindowW(L"#32768", nullptr);
#32768是菜单栏的类,见:About Window Classes
GetMenuBarInfo中有我们需要的PMENUBARINFO结构体参数,结构体中有下拉菜单的矩形大小
typedef struct tagMENUBARINFO { DWORD cbSize; RECT rcBar; HMENU hMenu; HWND hwndMenu; BOOL fBarFocused : 1; BOOL fFocused : 1; BOOL fUnused : 30; } MENUBARINFO, *PMENUBARINFO, *LPMENUBARINFO;
rcBar:The coordinates of the menu bar, popup menu, or menu item.
部分代码:
case WM_MENUSELECT: { HWND myhWnd = FindWindowW(L"#32768", nullptr); int err = GetLastError(); GetMenuBarInfo(myhWnd, OBJID_CLIENT, 0, &minfo); int w = minfo.rcBar.right - minfo.rcBar.left; int h = minfo.rcBar.bottom - minfo.rcBar.top; wchar_t szBuff[64]; wchar_t W[10]; wchar_t H[10]; wsprintf(szBuff, L"%p ", myhWnd); wsprintf(W, L"%d ", w); wsprintf(H, L"%d ", h); OutputDebugStringW(szBuff); OutputDebugStringW(W); OutputDebugStringW(H); } break;
结果: