Help and Support
 

powered byLive Search

How to detect a mouse click on any column of a List View control in Visual C++

Article ID:147842
Last Review:November 21, 2006
Revision:5.1
This article was previously published under Q147842
Note Microsoft Visual C++ .NET 2002 and Microsoft Visual C++ .NET 2003 support both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.
On This Page

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.

Back to the top

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.Open Solution Explorer. On the Project menu, click Add Class.
2.In the Add Class dialog box, double-click the MFC Class in the Templates pane.
3.In the MFC Class Wizard, type a class name of CMyListCtrl and a base class of CListCtrl, and then click Finish to add the new class to your project.
4.Add a message event handler for the NM_CLICK message to the CMyListCtrl class:
a. In the Class View window, click to select CMyListCtrl, open the Properties window, and then click Messages.
b. In the Properties window, click to select the =NM_CLICK entry.
c. Click to select the drop-down list that results, and then add the OnNMClick handler to your class. The following code is generated in the CMyListCtrl class:
   // In the .h file of CMyListCtrl:
   class CMyListCtrl : public CListCtrl
   {
       ...
       protected:
       DECLARE_MESSAGE_MAP()
       public:
       afx_msg void OnClickOnNMClick(NMHDR* pNMHDR, LRESULT* pResult);

   };

   // In the .cpp file of CMyListCtrl:

   BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
       ...
       ON_NOTIFY_REFLECT(NM_CLICK, OnClickOnNMClick)
   END_MESSAGE_MAP()

   void CMyListCtrl::OnClickOnNMClick(NMHDR* pNMHDR, LRESULT* pResult)
   {
       // TODO: Add your control notification handler code here

       *pResult = 0;
   }
					
5.Modify the CMyListCtrl::OnClick() function to perform the tasks in the sample code below.

Back to the top

Sample code

   void CMyListCtrl::OnClickOnNMClick(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;
   }
				

Back to the top

REFERENCES

By design, a List View control in a report view will only highlight the first column of a row. For more information about how to select an entire row, click the following article number to view the article in the Microsoft Knowledge Base:
131788 (http://support.microsoft.com/kb/131788/) OdListVw.exe highlights entire row in a ListView control

Back to the top


APPLIES TO
Microsoft Foundation Class Library 4.2, when used with:
  Microsoft Visual C++ 4.0 Standard Edition
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++, 32-bit Learning Edition 6.0
  Microsoft Visual C++ .NET 2002 Standard Edition
Microsoft Visual C++ .NET 2003 Standard Edition

Back to the top

Keywords: 
kbhowto kblistview kbuidesign kbprogramming kbcode KB147842

Back to the top

Article Translations

 

Related Support Centers

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.