为什么 CMenu::DestroyMenu之前一定要CMenu::Detach

时间:2021-04-04 05:19:17
MSDN:
Destroys the menu and any Windows resources that were used. The menu is detached from the CMenu object before it is destroyed. 

既然detach分离了菜单对象和菜单资源(即句柄=null)
DestroyMenu 又要销毁菜单资源,既然要销毁肯定要知道句柄,可是句柄都没了,它怎么知道要销毁的资源在哪???

9 个解决方案

#1



_AFXWIN_INLINE CMenu::~CMenu()
DestroyMenu(); }

BOOL CMenu::DestroyMenu()
{
if (m_hMenu == NULL)
return FALSE;
return ::DestroyMenu(Detach());
}

#2


_AFXWIN_INLINE CMenu::~CMenu()
    { DestroyMenu(); }

BOOL CMenu::DestroyMenu()
{
    if (m_hMenu == NULL)
        return FALSE;
    return ::DestroyMenu(Detach());
}

#3


否则不能delete掉

#4


下面这个网页说的很清楚:
http://hi.baidu.com/kofeffect/blog/item/6feb0b52badf570a0df3e37a.html

int CMyWnd::OnCreate( LPCREATESTRUCT lpCreateStruct ) 

  CMenu MyMenu; 
  MyMenu.CreateMenu(); 
  MyMenu.AppendMenu(MF_STRING,IDM_MENU0,"文件"); 
  MyMenu.InsertMenu(IDM_MENU2,MF_BYCOMMAND,IDM_ITEM0,"有关"); 
  this->SetMenu(&MyMenu); 
  MyMenu.Detach(); 
  return 0; 
}
用Detach()使菜单和MyMenu对象脱离关系,因为MyMenu对象是一个局部变量,马上就要超出作用域了,这一步是必须的。 
而SetMenu  是CMyWnd的成员函数,

是建立看得见的菜单同CMyWnd::m_cmenu的关系

DestroyMenu则根据m_cmenu 销毁看得见的菜单。




#5


既然detach分离了菜单对象和菜单资源(即句柄=null)
============
分离并不是将句柄设为NULL。

#6


Detach 是不是要先SetMenu才可以用能上面
而这个DestroyMenu应该不是销毁这个m_hMenu所对应的资源,应该是其他的资源吧
我在msdn上看到这个:

The call to Detach detaches the HMENU from the CMenu object, so that when the local CMenu variable passes out of scope,  the CMenu object destructor does not attempt to destroy a menu it no longer owns. The menu itself is automatically destroyed when the window is destroyed.

既然不再关联那销毁的应该不是m_hMenu关联的资源,detach的作用不就是为了供以后再使用吗??

#7


该回复于2011-04-25 11:09:55被版主删除

#8


这个到底怎么用呀

#9


该回复于2011-05-23 08:43:11被版主删除

#1



_AFXWIN_INLINE CMenu::~CMenu()
DestroyMenu(); }

BOOL CMenu::DestroyMenu()
{
if (m_hMenu == NULL)
return FALSE;
return ::DestroyMenu(Detach());
}

#2


_AFXWIN_INLINE CMenu::~CMenu()
    { DestroyMenu(); }

BOOL CMenu::DestroyMenu()
{
    if (m_hMenu == NULL)
        return FALSE;
    return ::DestroyMenu(Detach());
}

#3


否则不能delete掉

#4


下面这个网页说的很清楚:
http://hi.baidu.com/kofeffect/blog/item/6feb0b52badf570a0df3e37a.html

int CMyWnd::OnCreate( LPCREATESTRUCT lpCreateStruct ) 

  CMenu MyMenu; 
  MyMenu.CreateMenu(); 
  MyMenu.AppendMenu(MF_STRING,IDM_MENU0,"文件"); 
  MyMenu.InsertMenu(IDM_MENU2,MF_BYCOMMAND,IDM_ITEM0,"有关"); 
  this->SetMenu(&MyMenu); 
  MyMenu.Detach(); 
  return 0; 
}
用Detach()使菜单和MyMenu对象脱离关系,因为MyMenu对象是一个局部变量,马上就要超出作用域了,这一步是必须的。 
而SetMenu  是CMyWnd的成员函数,

是建立看得见的菜单同CMyWnd::m_cmenu的关系

DestroyMenu则根据m_cmenu 销毁看得见的菜单。




#5


既然detach分离了菜单对象和菜单资源(即句柄=null)
============
分离并不是将句柄设为NULL。

#6


Detach 是不是要先SetMenu才可以用能上面
而这个DestroyMenu应该不是销毁这个m_hMenu所对应的资源,应该是其他的资源吧
我在msdn上看到这个:

The call to Detach detaches the HMENU from the CMenu object, so that when the local CMenu variable passes out of scope,  the CMenu object destructor does not attempt to destroy a menu it no longer owns. The menu itself is automatically destroyed when the window is destroyed.

既然不再关联那销毁的应该不是m_hMenu关联的资源,detach的作用不就是为了供以后再使用吗??

#7


该回复于2011-04-25 11:09:55被版主删除

#8


这个到底怎么用呀

#9


该回复于2011-05-23 08:43:11被版主删除