HOWTO: Detect a Mouse Click on Any Column of List View Control

ID: Q147842


The information in this article applies to:
  • The Microsoft Foundation Classes (MFC), included with:
    • Microsoft Visual C++, 32-bit Editions, versions 4.0, 5.0, 6.0


SUMMARY

By design, a left-mouse click on a column other than the first column of a List View control when the control is in report view will not select the first column of that row. This article presents a way to detect the mouse click, and highlight the first column of the row where the mouse was clicked.


MORE INFORMATION

Step-by-Step Procedure

The List View control will send a NM_CLICK notification code to its parent window whenever the user has clicked the left mouse button within its client area. Below are the necessary steps to intercept this notification code in the control itself (instead of the parent window) and to highlight the first column of the row that is being clicked:

  1. Use ClassWizard to create a class that is derived from CListCtrl, and name it CMyListCtrl.


  2. When CMyListCtrl is the selected class name in both the Class Name combo box and the Object IDs: list box, select =NM_CLICK message from the Messages: list box. The following code will be generated to the CMyListCtrl class:
    
       // In the .h file of CMyListCtrl:
       class CMyListCtrl : public CListCtrl
       {
           ...
           protected:
           //{{AFX_MSG(CMyListCtrl)
           ...
           afx_msg void OnClick(NMHDR* pNMHDR, LRESULT* pResult);
           //}}AFX_MSG
    
           DECLARE_MESSAGE_MAP()
       };
    
       // In the .cpp file of CMyListCtrl:
    
       BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
           //{{AFX_MSG_MAP(CMyListCtrl)
           ...
           ON_NOTIFY_REFLECT(NM_CLICK, OnClick)
           //}}AFX_MSG_MAP
       END_MESSAGE_MAP()
    
       void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
       {
           // TODO: Add your control notification handler code here
    
           *pResult = 0;
       } 


  3. Modify the CMyListCtrl::OnClick() function to perform the tasks in the sample code below.


Sample Code


   void CMyListCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult)
   {
      // Get the current mouse location and convert it to client
      // coordinates.
      DWORD pos = GetMessagePos();
      CPoint pt(LOWORD(pos), HIWORD(pos));
      ScreenToClient(&pt);

      // Get indexes of the first and last visible items in listview
      // control.
      int index = GetTopIndex();
      int last_visible_index = index + GetCountPerPage();
      if (last_visible_index > GetItemCount())
          last_visible_index = GetItemCount();

      // Loop until number visible items has been reached.
      while (index <= last_visible_index)
      {
         // Get the bounding rectangle of an item. If the mouse
          // location is within the bounding rectangle of the item,
          // you know you have found the item that was being clicked.
          CRect r;
          GetItemRect(index, &r, LVIR_BOUNDS);
          if (r.PtInRect(pt))
          {
              UINT flag = LVIS_SELECTED | LVIS_FOCUSED;
              SetItemState(index, flag, flag);
              break;
          }

          // Get the next item in listview control.
          index++;
      }

      *pResult = 0;
   } 


REFERENCES

By design, a List View control in a report view will only highlight the first column of a row. To select an entire row, please see the following article in the Microsoft Knowledge Base:

Q131788 SAMPLE: Highlighting an Entire Row in a ListView Control

Additional query words: CListView

Keywords : kbcode kbprg kbMFC KbUIDesign kbVC kbVC400 kbVC500 kbVC600
Version : 4.0 5.0
Platform : NT WINDOWS
Issue type : kbhowto


Last Reviewed: July 16, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.