dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15

Thread: [RESOLVED] All Apps minimized, but the popup menu is not destroied

  1. #1
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    [RESOLVED] All Apps minimized, but the popup menu is not destroied

    In a trivial dialog base application I have a list control and a control variable for it.
    Suppose that we want to handle NM_RCLICK for this list control and we want to create a pop-up menu. That method, looks like this:
    Code:
    void CtestMenuMinimizeDlg::OnNMRclickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
           HMENU   hPopupMenu;   
           POINT point;
           ::GetCursorPos(&point);
           hPopupMenu = CreatePopupMenu();
    
           AppendMenu(hPopupMenu, MF_STRING , 1000, _T("Item 1"));
           AppendMenu(hPopupMenu, MF_SEPARATOR, 0 , _T(""));
           AppendMenu(hPopupMenu, MF_STRING , 1001, _T("Item 2"));
           AppendMenu(hPopupMenu, MF_STRING , 1002, _T("Item 3"));
    
           ::SetForegroundWindow(m_ctrlList.m_hWnd);
           TrackPopupMenuEx(hPopupMenu, TPM_NONOTIFY | TPM_RETURNCMD, point.x,point.y, m_ctrlList.m_hWnd, NULL );
           ::SetForegroundWindow(m_ctrlList.m_hWnd);
    
           DestroyMenu(hPopupMenu);
    
           *pResult = 0;
    }
    If I minimized all the application using the keys combination Windows + D, I didn't succeed to distroy my menu (it remains on the desktop).

    In Dino Esposito's book "Windows Shell Programming" I got that this is a well known bug, that might be fixed by SetForegroundWindow() function, before and after TrackPopupMenu() or TrackPopupMenuEx().

    "This ensures that all the input gets redirected to our window, which can then dismiss the menu. The bug is in the code of TryNotifyWnd window, not in TrackPopupMenu() or our application."

    My station has Windwos XP SP3 OS and the using of SetForegroundWindow() has no result.
    Any ideas?

    Last edit: I saw this bug in many applications, that used pop-up menu and system try... But, I suppose that, there is a solution...
    Last edited by Maximus_X; December 9th, 2008 at 09:59 AM.

  2. #2
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: All Apps minimized, but the popup menu is not destroied

    I think it should work if you send a WM_CANCELMODE if the app is deactivated or loses focus.

    I have this MDI app that can lock itself based on a timer.
    To close any open menus, I do the following:

    Code:
      CWnd *aw=((CMainFrame *)AfxGetMainWnd())->GetActiveFrame();
      aw=aw?((CFrameWnd *)aw)->GetActiveView():aw;
      if(aw)
        SendMessage(aw->GetSafeHwnd(), WM_CANCELMODE,0,0);
    Nobody cares how it works as long as it works

  3. #3
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,592

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by Maximus_X View Post
    ...
    Suppose that we want to handle NM_RCLICK for this list control and we want to create a pop-up menu.
    Why do you handle NM_RCLICK instead of NM_CONTEXTMENU notification?
    Victor Nijegorodov

  4. #4
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by VictorN View Post
    Why do you handle NM_RCLICK instead of NM_CONTEXTMENU notification?
    Sorry, I don't understand what do you mean with NM_CONTEXTMENU.

    I have a list control and I want to create this pop-up menu in the right click handle method. That's all. What is wrong?

  5. #5
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: All Apps minimized, but the popup menu is not destroied

    Like Victor said use ON_WM_CONTEXTMENU()
    Nobody cares how it works as long as it works

  6. #6
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: All Apps minimized, but the popup menu is not destroied

    I handled ON_WM_CONTEXTMENU() but the menu behavior is the same (all the windows are minimized, but the menu is not destroied; it remains on the desktop).
    Code:
    void CtestMenuMinimizeDlg::OnNMRclickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	SendMessage(WM_CONTEXTMENU, (WPARAM) m_hWnd, GetMessagePos());
    	*pResult = 1;
    }
    
    void CtestMenuMinimizeDlg::OnContextMenu(CWnd* pWnd, CPoint ptMousePos) 
    {
    	// if Shift-F10
    	if (ptMousePos.x == -1 && ptMousePos.y == -1)
    		ptMousePos = (CPoint) GetMessagePos();
    
    	HMENU	hPopupMenu;	
    	hPopupMenu = CreatePopupMenu();
    	
    	AppendMenu(hPopupMenu, MF_STRING , 1000, _T("Item 1"));
    	AppendMenu(hPopupMenu, MF_SEPARATOR, 0 , _T(""));
    	AppendMenu(hPopupMenu, MF_STRING , 1001, _T("Item 2"));
    	AppendMenu(hPopupMenu, MF_STRING , 1002, _T("Item 3"));
    
    	//::SetForegroundWindow(this->m_hWnd);
    	TrackPopupMenuEx(hPopupMenu, TPM_NONOTIFY | TPM_RETURNCMD, ptMousePos.x,ptMousePos.y, m_ctrlList.m_hWnd, NULL );
    	//::SetForegroundWindow(this->m_hWnd);
    
    	DestroyMenu(hPopupMenu);
    }

  7. #7
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: All Apps minimized, but the popup menu is not destroied

    I'm not sure if WM_CONTEXTMENU is going to solve your problem, but you should not send the message yourself.

    Code:
    // h file
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
    // cpp file
    ON_WM_CONTEXTMENU()
    so, remove the rclick handler and use contextmenu alone.
    Nobody cares how it works as long as it works

  8. #8
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by zerver View Post
    I'm not sure if WM_CONTEXTMENU is going to solve your problem, but you should not send the message yourself.

    Code:
    // h file
    afx_msg void OnContextMenu(CWnd* pWnd, CPoint point);
    // cpp file
    ON_WM_CONTEXTMENU()
    so, remove the rclick handler and use contextmenu alone.
    I did it and there is no change.
    I'm affraid that as Dino Esposino said "the bug is in the code of TryNotifyWnd window, not in TrackPopupMenu() or our application." and this bug was not solved by Microsoft.
    Last edited by Maximus_X; December 9th, 2008 at 01:01 PM.

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,592

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by Maximus_X View Post
    Sorry, I don't understand what do you mean with NM_CONTEXTMENU...
    I am awfully sorry, it should, of course, have been WM_CONTEXTMENU, not NM_CONTEXTMENU
    Victor Nijegorodov

  10. #10
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by VictorN View Post
    I am awfully sorry, it should, of course, have been WM_CONTEXTMENU, not NM_CONTEXTMENU
    There is no problem. When you said NM_CONTEXTMENU, I searched in MSDN and I got .
    That's why I didn't understand.

  11. #11
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354

    Re: All Apps minimized, but the popup menu is not destroied

    Did you try the WM_CANCELMODE suggestion ?

  12. #12
    Join Date
    Jan 2008
    Posts
    178

    Re: All Apps minimized, but the popup menu is not destroied

    See Google Groups.
    It's a well-known Win32 FAQ for 14 years...
    (nothing to do with WM_CONTEXTMENU )

  13. #13
    Join Date
    Jan 2003
    Location
    Timisoara, Romania
    Posts
    306

    Re: All Apps minimized, but the popup menu is not destroied

    Well, I found a solution...
    I created a class member attribute ( HMENU m_hPopupMenu ), and I handled ON_MOVE message and NM_RCLICK or ON_WM_CONTEXTMENU.
    The methods looks like this:
    Code:
    void CtestMenuMinimizeDlg::OnMove(int x, int y)
    {
    	CDialog::OnMove(x, y);
    
    	if(NULL != this->m_hPopupMenu)
    	{
    		::SetForegroundWindow(this->m_hWnd);
    		::PostMessage(this->m_hWnd, WM_RBUTTONDOWN,NULL,NULL);
    	}
    }
    
    void CtestMenuMinimizeDlg::OnNMRclickList1(NMHDR *pNMHDR, LRESULT *pResult)
    {
    	POINT point;
    	::GetCursorPos(&point);
    
    	m_hPopupMenu = CreatePopupMenu();
    
    	if(this->m_hPopupMenu!=NULL)
    	{
    		AppendMenu(m_hPopupMenu, MF_STRING , 1000, _T("Item 1"));
    		AppendMenu(m_hPopupMenu, MF_SEPARATOR, 0 , _T(""));
    		AppendMenu(m_hPopupMenu, MF_STRING , 1001, _T("Item 2"));
    		AppendMenu(m_hPopupMenu, MF_STRING , 1002, _T("Item 3"));
    
    		::SetForegroundWindow(this->m_hWnd);
    		TrackPopupMenuEx(m_hPopupMenu, TPM_NONOTIFY | TPM_RETURNCMD, point.x,point.y, m_ctrlList.m_hWnd, NULL );
    	}
    
    	DestroyMenu(m_hPopupMenu);
    }
    Thanks a lot, to everyone!
    Cheers!

  14. #14
    Join Date
    Jun 2002
    Location
    Stockholm, Sweden
    Posts
    1,641

    Re: All Apps minimized, but the popup menu is not destroied

    If that works, then posting a WM_CANCELMODE should work as well.
    Nobody cares how it works as long as it works

  15. #15
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: All Apps minimized, but the popup menu is not destroied

    Quote Originally Posted by fred100 View Post
    See Google Groups.
    It's a well-known Win32 FAQ for 14 years...
    (nothing to do with WM_CONTEXTMENU )
    Why don't you post the link?

    This also works:
    Code:
    void CtestMenuMinimizeDlg::OnSize(UINT nType, int cx, int cy)
    {
    	CDialog::OnSize(nType, cx, cy);
    
    	if(nType == SIZE_MINIMIZED)
    	{
    		::PostMessage(this->m_hWnd, WM_RBUTTONDOWN, 0, 0);
    	}
    }
    Call to SetForegroundWindow is not necessary.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)