在MFC编程中,我重写PreTranslateMessage来处理来自CTreeCtrl的RCLICK消息,但是我有一些问题. [英] In MFC Programming, I Override the PreTranslateMessage to handle the RCLICK message From CTreeCtrl,But i have some problem.
本文介绍了在MFC编程中,我重写PreTranslateMessage来处理来自CTreeCtrl的RCLICK消息,但是我有一些问题.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在对话框中覆盖了PreTranslateMessage函数,但是当我第一次右键单击CTreeCtrl控件时,菜单按预期加载,但是当我第二次右键单击时,菜单消失了.第三遍再次加载菜单,第四遍消失.
我想了解消息路由的机制.
问题代码在这里.
I override my PreTranslateMessage function in my dialog, but when i right click the CTreeCtrl control the first time, the Menu loaded as i expected, but when i right click the second time, the Menu disappeared. and the third time the menu loaded again, and fourth it disappeared.
i want to understand the mechmanism of the message routing.
the problem code is here.
if(WM_RBUTTONDOWN==pMsg->message)
{
DWORD dwPos = GetMessagePos();
CPoint point(LOWORD(dwPos), HIWORD(dwPos));
CMenu menu;
VERIFY(menu.LoadMenu(IDR_RCLKMENU));
CMenu *popup=menu.GetSubMenu(0);
ASSERT(popup!=NULL);
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
}
return CDialogEx::PreTranslateMessage(pMsg);
当我添加一行代码时,问题就消失了,并且可以正常工作了.
When i add one line code the problem disappear and works as i expceted.
if(WM_RBUTTONDOWN==pMsg->message)
{
DWORD dwPos = GetMessagePos();
CPoint point(LOWORD(dwPos), HIWORD(dwPos));
CMenu menu;
VERIFY(menu.LoadMenu(IDR_RCLKMENU));
CMenu *popup=menu.GetSubMenu(0);
ASSERT(popup!=NULL);
popup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this);
return true; // this line make all message routing normal.
}
return CDialogEx::PreTranslateMessage(pMsg);
谢谢大家.
Thank you all.
推荐答案
如果从PreTranslateMessage()
返回TRUE
,则消息被标记为已处理,并且不再执行进一步的处理.这是您可能想要的行为.因为您已处理了消息.
右键单击时,Windows提供消息WM_CONTEXTMENU
.因此,更好的解决方案是通过向CTreeCtrl
派生类中添加此消息的处理程序来使用此方法.为此,请对树控件类使用属性窗口.它将创建一个ON_WM_CONTEXTMENU()
条目和一个函数主体OnContextMenu()
.
If you returnTRUE
fromPreTranslateMessage()
the message is marked as handled and no further handling is executed. This is the behaviour you probably want here. Because you have handled the message.
Windows provides the messageWM_CONTEXTMENU
when right-clicking. So a better solution would be to use this by adding a handler for this message to yourCTreeCtrl
derived class. To do this, use the properties window for the tree control class. It will create aON_WM_CONTEXTMENU()
entry and the function bodyOnContextMenu()
.
这篇关于在MFC编程中,我重写PreTranslateMessage来处理来自CTreeCtrl的RCLICK消息,但是我有一些问题.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文