TrackPopupMenu运行程序崩溃

wshcy512 2009-09-01 10:10:48

void CMyListView::OnRButtonDown(UINT nFlags, CPoint point)
{
CListCtrl &listCtrl=(CListCtrl&)GetListCtrl();
CMenu menu;
menu.CreatePopupMenu();
if (menu.GetSafeHmenu() == NULL)
return;

menu.AppendMenu(MF_STRING, 1, "新建");
if (listCtrl.GetSelectedCount() > 0)
{
menu.AppendMenu(MF_STRING, 2, "修改");
menu.AppendMenu(MF_STRING, 3, "删除");
}
SetForegroundWindow();

long lr = menu.TrackPopupMenu(TPM_LEFTBUTTON |TPM_LEFTALIGN ,point.x,point.y,this);
menu.DestroyMenu();
//CListView::OnRButtonDown(nFlags, point);
}

第一次运行到TrackPopupMenu没有错,在点右键运行到TrackPopupMenu就程序崩溃了
另外,很奇怪,调试的时候有时候会蹦,有时候不会蹦
...全文
324 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wshcy512 2009-09-02
  • 打赏
  • 举报
回复
我新建了一个单文档,用上面代码测试没问题,可能是我程序其他地方报错吧,谢谢各位
用户 昵称 2009-09-02
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 stjay 的回复:]
不用menu.DestroyMenu();

MSDN
If the menu is assigned to a window, it is automatically destroyed when the window is destroyed. If the menu is added to an existing menu, it is automatically destroyed when that menu is destroyed.

Before exiting, an application must free system resources associated with a pop-up menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.

大概意思是 菜单关联到窗口时会自动销毁
[/Quote]
看不懂,按你贴的注释,应该调用DestroyMenu。
Conry 2009-09-01
  • 打赏
  • 举报
回复
除了弹出位置不对外没发现其他问题
添加红色代码位置就没问题了
SetForegroundWindow();

ClientToScreen(&point);
long lr = menu.TrackPopupMenu(TPM_LEFTBUTTON |TPM_LEFTALIGN ,point.x,point.y,this);
menu.DestroyMenu();
stjay 2009-09-01
  • 打赏
  • 举报
回复
还有最好将 CMenu menu 设为成员变量
stjay 2009-09-01
  • 打赏
  • 举报
回复
不用menu.DestroyMenu();

MSDN
If the menu is assigned to a window, it is automatically destroyed when the window is destroyed. If the menu is added to an existing menu, it is automatically destroyed when that menu is destroyed.

Before exiting, an application must free system resources associated with a pop-up menu if the menu is not assigned to a window. An application frees a menu by calling the DestroyMenu member function.

大概意思是 菜单关联到窗口时会自动销毁

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧