不知道大家用过该函数没有:
TrackPopupMenu( UINT nFlags, int x, int y, CWnd* pWnd, LPCRECT lpRect = NULL );在MSDN中对该函数中的第5个参数是这样是说明的:lpRectPoints to a RECT structure or CRect object that contains the screen coordinates of a rectangle within which the user can click without dismissing the pop-up menu. If this parameter is NULL, the pop-up menu is dismissed if the user clicks outside the pop-up menu. This must be NULL for Windows 3.0.我在程序中是这样写的:
switch(uMouseMsg)
{case WM_RBUTTONDOWN:
  CMenu menu,*pMenu;
  POINT  point;   if(menu.LoadMenu(IDR_MENU1) && GetCursorPos(&point) )
  {
     pMenu=menu.GetSubMenu(0);
     if(pMenu)
     {        pMenu->EnableMenuItem(IDR_MENU1,MF_GRAYED);
        pMenu->TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,this);
      }
   }
break;
case WM_LBUTTONDOWN:
  ...
break;} 
程序可以运行,点击鼠标右键时能弹出菜单,可是在菜单之外点击时,菜单不消失,这就是我的问题,为什么在菜单之外点击时菜单不消失呢?

解决方案 »

  1.   

    把那个参数设置为  NULL
      

  2.   

    我写的那句:
    pMenu->TrackPopupMenu(TPM_CENTERALIGN,point.x,point.y,this);
    第五个参数我没有写,可是默认参数不就是空吗?
      

  3.   

    TrackPopupMenu 之前用一下 SetForegroundWindow();
      

  4.   

    谢谢你,rokia(大力水手)。
    加上SetForegroundWindow()之后,问题搞定。
      

  5.   

    在MYMENU.TrackPopupMenu(ID_MENU1);
    之前加一句:   SetForegroundWindow();
    这个函数的功能为: puts the thread that created the specified window into the foreground and activates the window. Keyboard input is directed to the window, and various visual cues are changed for the user.前几天我也是遇到這個問題。 這是別人給我的解釋。我想這是系統的bug. 需要你做一下 setforegroundwindow(),激活你的窗口,這時你再在其它地方點鼠標,windows才會認為那個popmenu失去了焦點,然后把它destroy掉。
      

  6.   

    遭了,这个帖子已经结了,好象加不了分了。sorry!