我想从上下文弹出菜单中获取一个矩形

问题描述 投票:0回答:1

我几天前问了问题(How to get Rect of Popup menu in mfc(with C++)?)。然后,我成功完成了“我的按钮”和上下文菜单的处理。过程是这样的。enter image description here

1.当鼠标指针悬停在按钮A上时,显示弹出菜单(1)和(2)。2.当鼠标指针从按钮A离开时,关闭所有弹出菜单。└这很麻烦,因为当鼠标指针悬停在我的弹出菜单(1)或(2)上时,关闭所有弹出菜单。我要除弹出菜单区域外。

因此,我尝试获取我的弹出菜单并从流程2中添加例外。如何获取它?

※P.S 1:我使用了TrackPopupMenu函数。我认为,直到TrackPopupMenu函数(如选定菜单或关闭弹出菜单)结束之前,才能获得弹出菜单。我对吗?※P.S 2:我尝试使用WM_MENUSELECT消息。但是,当鼠标指针离开Button A时,它仍关闭弹出菜单。因此,我将尝试使用GetMenuItemRect函数。如何使用它?

c++ mfc contextmenu popupmenu
1个回答
0
投票

您可以显式创建并显示一个新的TrackPopupMenu,而不是使用CMFCPopupMenu函数(该函数不会返回创建的菜单的句柄。)>

[不知道实现的全部细节,您将执行以下操作:

void MyClass:OnShowPopup(int Xpos, int Ypos) { // Xpos and Ypos are top-left corner
    CMDIFrameWndEx* frame = dynamic_cast<CMDIFrameWndEx *>(AfxGetMainWnd()); // Main frame
    CMenu menu;  menu.LoadMenu(IDM_MYPOPUP_ID); // Load from resources - or something
    CMFCPopupMenu *pPop = new CMFCPopupMenu; // New popup menu
    pPop->Create(this, Xpos, Ypos, menu.m_hMenu, FALSE, TRUE);
    CRect rc; pPop->GetWindowRect(&rc); // Here you have the popup menu's rectangle!!
    frame->OnShowPopupMenu(pPop); // This will activate the menu.
}

让我知道是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.