Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get CMenu popup menu to disappearing?

I have a popup menu for my application in the system tray. The issue I'm have is I can not get the menu to disappear without selecting an item.

How could I get the popup menu to disappear when I click elsewhere in windows?

Here is the code for making the popup menu appear:

LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    if( message == WM_MYMESSAGE && lParam == WM_RBUTTONDOWN)
    {
        m_menu.CreatePopupMenu();        
        POINT point;

        GetCursorPos(&point);
        m_menu.AppendMenu(MF_POPUP, WM_MAXIMIZE, _T("Maximize"));
        m_menu.AppendMenu(MF_POPUP, WM_EXIT, _T("Exit"));
        m_menu.TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON, point.x, point.y, this);

    }
    return CDialog::WindowProc(message, wParam, lParam);
}

I've tried using CMenu::DestroyMenu with no luck.

like image 261
andre Avatar asked Oct 23 '25 19:10

andre


1 Answers

The solution that worked for me was invoking the SetForegroundWindow(); function before calling TrackPopupMenu.

I found the solution in Remarks of the TrackPopupMenu function description.

like image 125
andre Avatar answered Oct 26 '25 09:10

andre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!