Determining right click on the header control


One of the notification that the header control sends is the HDN_ITEMCLICK notification. This leads us to wrongly expect a corresponding notification for a right click. The control does send a notification for a right button click, but this is a generic notification sent by all new controls in the form of NM_RCLICK. Although you can use a message map entry to add a handler for this notification message, don't use the class wizard. The class wizard adds an entry corresponding to the notification sent by the listview control to its parent and not the notification sent by the header control to the listview control.

You can add a check for the NM_RCLICK notfication in the OnNotify() function as shown below.

BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult) 
{
	HD_NOTIFY	*pHDN = (HD_NOTIFY*)lParam;

	LPNMHDR pNH = (LPNMHDR) lParam; 
	// wParam is zero for Header ctrl
	if( wParam == 0 && pNH->code == NM_RCLICK )
	{
		// Right button was clicked on header
		CPoint pt(GetMessagePos());
		CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
		pHeader->ScreenToClient(&pt);
		
		// Determine the column index
		int index;
		CRect rcCol;
		for( int i=0; Header_GetItemRect(pHeader->m_hWnd, i, &rcCol); i++ )
		{
			if( rcCol.PtInRect( pt ) )
			{
				index = i;
				break;
			}
		}

		// Add your right click action code 
	}
}

 



Comments

  • Awesome

    Posted by Legacy on 12/27/2003 12:00am

    Originally posted by: Lawrence Fong

    It is awesome! It is exactly what I am looking for.

    Reply
  • Thanks 2

    Posted by Legacy on 07/24/2000 12:00am

    Originally posted by: Pablo Tom�

    I had been looking for somes functions that you've used here!!!!

    Thanks a lot!!!!!!!

    Reply
  • Sending a notification to the parent

    Posted by Legacy on 10/19/1999 12:00am

    Originally posted by: Mike Burroughs

    I have added the following code where you handle the right click.  
    
    

    NMLISTVIEW NMListView;
    NMListView.hdr.code = LVEXN_COLUMNRCLICK;
    NMListView.hdr.hwndFrom = this->m_hWnd;
    NMListView.hdr.idFrom = GetDlgCtrlID( );
    NMListView.iSubItem = i;

    CWnd* pWnd = GetParent( );
    pWnd->SendMessage( WM_NOTIFY,
    GetDlgCtrlID( ),
    (LPARAM)&NMListView );
    return TRUE;


    This snippet of code causes a notification message to be sent to the parent so you can capture it in the same fashion that you capture the LVN_COLUMNCLICK.

    In the header for CListViewEx I added the following define.

    #define LVEXN_COLUMNRCLICK (LVN_LAST-1)


    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • The open source cloud computing project OpenStack has come a long way since NASA and Rackspace launched it in 2010. Backed by leading technology infrastructure providers including Cisco, Dell, EMC, HP, IBM, Intel, and VMware, OpenStack underpins significant workloads at an increasingly diverse set of organizations, including BWM, CERN, Comcast, eBay, and Wal-Mart. For CIOs engaged in broader programs to win, serve, and retain customers -- and refocus business technology (BT) spend -- a planned and pragmatic …

  • Microsoft® Office 365 is a top choice for enterprises that want a cloud–based suite of productivity collaboration applications. With Office 365, you get access to Microsoft™ Office solutions practically anytime, anywhere, on virtually any device. It's a great option for current Microsoft users who can now build on their experience with Microsoft™ solutions while enjoying the flexibility of a cloud-based delivery. But even organizations with no previous investment in Microsoft will find that …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date