BOOL CEnhancedListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
// TODO: Add your specialized code here and/or call the base class
// wParam is zero for Header ctrl
LPNMHDR pNH = (LPNMHDR) lParam; 
if( wParam == 0 && pNH->code == NM_RCLICK )
{
// Right button was clicked on header
CHeaderCtrl* pHeader = CListCtrl::GetHeaderCtrl();
if(pHeader){
CPoint pt(GetMessagePos());
CPoint posMouse=pt;
pHeader->ScreenToClient(&pt);
// Determine the column index
int index=-1;
CRect rcCol;
for( int i=0; pHeader->GetItemRect(i, &rcCol); i++ )
{
if( rcCol.PtInRect( pt ) )
{
index = i;
break;
}
}
*pResult=TRUE;
OnHeaderRClick(posMouse,i);
return TRUE;
}
}
return CListCtrl::OnNotify(wParam, lParam, pResult);
}

解决方案 »

  1.   

    Handling right-click on the CListCtrl header control --------------------------------------------------------------------------------This article was contributed by Alberto Gattegno.Environment: VC6 An alternative to Zafir Andum's approach on determining right click on the header control is to use the HDM_HITTEST message in order to determine  on which header item the user right clicked. This saves you the "for" loop for the determination of the column.Here is an example:BOOL CMyListtCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
     if( wParam==0 && ((NMHDR*)lParam)->code ==NM_RCLICK)
     {
      POINT Point;
      GetCursorPos (&Point);
      ScreenToClient(&Point);
      HDHITTESTINFO HitTest;  HitTest.pt.x = Point.x;
      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. Finally on the part of my colleagues and me, we would like to thank codeguru for the immense help it has given us the last year.