Pregunta

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.

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;

However, the hit test always returns -1.

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.

¿Fue útil?

Solución 2

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.

Otros consejos

You cannot do it trying to handle message from a list’s header in a dialog, nor can you do it in the CListCtrl derived class.

MFC is using message reflection for certain controls and only for certain messages/notification codes.

Most likely you are passing coordinates of the mouse click on the list control, hence hit test fails. Try this:

Add class derived from CHeaderCtrl. Declare member variable of the derived class in a dialog. I assume you have already subclassed (have variable inserted by the wizard) list control. In OnInitDialog write the following:

// m_List is the dialog’s member of the subclassed list control, 
// m_header is a member variable of your new header class:
// insert this code after list control is already 
// initialized and all columns are added.
CHeaderCtrl* pHeaeder = m_List.GetHeaderCtrl();

m_Header.SubclassWindow(pHeaeder->m_hWnd);

Insert handler for WM_CONTEXTMENU or WM_LBUTTONUP in the derived class and popup menu. You will receive CPoint type for click position.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top