MFC CListCtrl右键单击标题 [英] MFC CListCtrl Right Click On Header

查看:121
本文介绍了MFC CListCtrl右键单击标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用Visual Studio 6编写的MFC应用程序,正在向其中添加一些新功能.我想要做的是,当用户右键单击属性页内列表控件上的标题列时,显示一个上下文菜单.在类向导中添加了CListCtrl类.

I have a MFC app written in Visual Studio 6 which I am adding some new functionality to. What I want to be able to do is display a context menu when the user right clicks on the header column on the list control within a property page. The CListCtrl class was added view the Class Wizard.

通过属性页的ClassWizard,我为listctrl上的右键单击添加了一个处理程序.确实会调用此方法,并且我添加了以下代码来计算rclick是否位于标题部分上,如果是,则显示哪个标题项.像这样

Via the ClassWizard for the property page, I have added a handler for the right click on the listctrl. This does get called and I added the following code to work out if the rclick was over the header section and if so which header item. Like this

POINT Point;
GetCursorPos (&Point);
ScreenToClient(&Point);

HDHITTESTINFO HitTest;

//Offset of right scrolling  
HitTest.pt.x = Point.x+ m_ctrlRecordList.GetScrollPos(SB_HORZ); //Offset of right scrolling
HitTest.pt.y = Point.y;

//Send the Hit Test Message
m_ctrlRecordList.GetHeaderCtrl()->SendMessage(HDM_HITTEST,0,(LPARAM)&HitTest);

    // Check hit test result.

*pResult = 0;

但是,命中测试始终返回-1.

However, the hit test always returns -1.

我尝试通过单击鼠标左键来代替,方法是处理属性页中标头控件的HDN_ItemClick消息.这都是在ClassWizard中完成的,因此我希望能够在此处理此通知.但是,到目前为止,根据我的研究,MFC中可能存在一个错误,即ClassWizard将此代码代入您的代码中,但是此通知永远不会达到列表控件的父级.是这样吗?

I tried just on left click instead by handling the HDN_ItemClick message of the header control in the property page. This is all done in the ClassWizard so I expected to be able to handle this notification here. However, from what I have researched so far, there may be a bug in MFC where the ClassWizard puts this code into your code for you but this notification will never get as far as the parent of your list control. Is this the case?

做到这一点的最佳方法是什么?我希望右键单击,但如有必要,可以单击鼠标左键.

What would be the best way to do this? I would prefer on right click but left click would do if necessary.

推荐答案

我设法解决了这个问题,并认为我会添加答案,以防其他任何人在遇到相同问题时偶然发现.我最初发布的代码很好,但是需要进入CListCtrl派生的类的OnNotify处理程序中. ClassWizard允许您向列表控件的父级添加一个反射处理程序,但是消息永远不会那么远.

I have managed to sort this out and thought I would add the answer in case anyone else stumbles upon this with the same problem. The code I posted originally is fine but it needs to go in the OnNotify handler of a class derived from CListCtrl. The ClassWizard allows you to add a reflect handler to the parent of the list control but the message never gets that far.

这篇关于MFC CListCtrl右键单击标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆