65.9K
CodeProject is changing. Read more.
Home

Handling right-click on the CListCtrl header control

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.56/5 (12 votes)

May 8, 2000

CPOL
viewsIcon

101172

Determining the right click on the header of the CListCtrl

A good way to handle a right click on the header control of a list control is to use the HDM_HITTEST message to determine on which header item the user right clicked.

Here is an example:

BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
    if( wParam==0 && ((NMHDR*)lParam)->code == NM_RCLICK)
    {
        POINT Point;
        GetCursorPos (&Point);
        ScreenToClient(&Point);
        
        HDHITTESTINFO HitTest;
       
        //Offset of right scrolling  
        HitTest.pt.x = Point.x+ GetScrollPos(SB_HORZ); //Offset of right scrolling
        HitTest.pt.y = Point.y;
        
        //Send the Hit Test Message
        GetHeaderCtrl()->SendMessage(HDM_HITTEST,0,(LPARAM)&HitTest);

        .....
    }
    .....
}

The HDHITTESTINFO structure holds the column index in its iItem member.