List Box Hit Testing

This is a discussion on List Box Hit Testing within the Windows Programming forums, part of the Platform Specific Boards category; Hello, I'm trying to determine which list box item the cursor is over when my parent window receives a WM_CONTEXTMENU ...

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262

    List Box Hit Testing

    Hello,

    I'm trying to determine which list box item the cursor is over when my parent window receives a WM_CONTEXTMENU message (the right mouse button was clicked). Unfortunately, the list box is owner-drawn with variable heights for each item, so it's not as simple as getting the index of the top visible item and doing some division, the actual visible area needs to be determined.

    Does anyone know how this could be done?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You could try LB_ITEMFROMPOINT or LBItemFromPt.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    List box hit testing doesn't work properly in Windows XP.

    Code:
    UINT CAddSeqDlg::ItemFromPointNT(CPoint pt, BOOL& bOutside) const
    
    // CListBox::ItemFromPoint does not work on NT.
    
    {
      if (m_lstFrames.GetCount()==0) return -1;
      int nFirstIndex, nLastIndex;
      nFirstIndex = m_lstFrames.GetTopIndex();
      nLastIndex = nFirstIndex  + m_lstFrames.GetCount(); 
      
      bOutside = TRUE;
        
      CRect Rect;
      int nResult = -1;
        
      for (int i = nFirstIndex;nResult==-1 && i<=nLastIndex;i++)
      {
        int result=m_lstFrames.GetItemRect(i,&Rect);
           
        if (result==LB_ERR)
        {
          nResult=nLastIndex-1;
          bOutside=FALSE;
        }
        else
        {
          if (Rect.PtInRect(pt))
          {
            nResult  = i;
            bOutside = FALSE;
          }
        }
      }
      return nResult;
    }
    This is from code I found either at codeguru or codeproject. This is MFC, but you should be able to convert to Win32 quite easily.

  4. #4
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    Really? I implemented the LB_ITEMFROMPOINT message in my program (running on XP) and it works fine. Gets me the item under the cursor or nothing.

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Perhaps MSDN meant that the MFC encapsulation of that does not work.

  6. #6
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,262
    Actually... this snippet could be quite useful indeed.
    The problem with LB_ITEMFROMPOINT is that it always returns the list item nearest the cursor. If I want to be explicit and only get what's immediately underneath the cursor then your code modified slightly would give the correct result.

    Cheers Bubba.

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Then modfiy away and hope it works for ya's. I needed this for a right click on a list box as well and the code works quite well.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  2. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 01:48 PM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 08:27 PM
  5. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21