Shin
Shin

Reputation: 37

I want to get a rect from my contextual popup menu

I asked the question a few days ago.

Then I succeeded the process of My button and contextual menu.

The process is like this.

enter image description here

  1. When the mouse point is hovering on Button A, Show the popup menu (1) and (2).

  2. When the mouse point is leaving from Button A, Close the all popup menu.

This is trouble because when the mouse point is hovering on my popup menu (1) or (2), close the all popup menu. I want to except the area of popup menu.

So I've tried to get rect of my popup menu and add exception from the Process 2. How can I get it?

※ P.S 1 : I used TrackPopupMenu function. In my opinion, It's impossible to get rect of popup menu until the end of TrackPopupMenu function(like selected menu or close popup menu). Am I right?

※ P.S 2 : I've tried to use WM_MENUSELECT Message. However it's still close the popup menu when the mouse point is leaved from Button A. So I'll try to use GetMenuItemRect function. How to use it?

Upvotes: 2

Views: 416

Answers (1)

Adrian Mole
Adrian Mole

Reputation: 51884

Rather than using the TrackPopupMenu function (which doesn't return a handle to the created menu), you can explicitly create and show a new CMFCPopupMenu.

Without knowing the full details of your implementation, you would do something like this:

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

Let me know if this helps.

Upvotes: 2

Related Questions