MFC中,重载CListCtrl类,响应其双击消息,函数为OnDblclk(NMHDR* pNMHDR, LRESULT* pResult),如何得知用户点击的是哪一行的哪一列呢?

解决方案 »

  1.   

    使用GetCurSel()可以得到是那一行,至于那一列,就需要自己判断了
      

  2.   

    不好意思,刚才没有看清楚,在CListCtrl中可以使用HitTest来得到是那一行那一列
    CListCtrl.HitTest(LVHITTESTINFO* pHitTestInfo)
    LVHITTESTINFO
    Has been extended to accommodate subitem hit-testing. The LVHITTESTINFO structure contains information about a hit test. It is used in association with the LVM_HITTEST and LVM_SUBITEMHITTEST messages and their related macros. This structure supersedes the LV_HITTESTINFO structure. typedef struct _LVHITTESTINFO { 
        POINT pt; 
        UINT flags; 
        int iItem; 
        int iSubItem;
     } LVHITTESTINFO, FAR *LPLVHITTESTINFO;Members
    pt 
    Position to hit test, in client coordinates. 
    flags 
    Variable that receives information about the results of a hit test. This member can be one or more of the following values: LVHT_ABOVE  The position is above the control's client area. 
    LVHT_BELOW  The position is below the control's client area. 
    LVHT_NOWHERE  The position is inside the list view control's client window, but it is not over a list item. 
    LVHT_ONITEMICON  The position is over a list view item's icon. 
    LVHT_ONITEMLABEL The position is over a list view item's text. 
    LVHT_ONITEMSTATEICON The position is over the state image of a list view item. 
    LVHT_TOLEFT  The position is to the left of the list view control's client area. 
    LVHT_TORIGHT  The position is to the right of the list view control's client area. You can use LVHT_ABOVE, LVHT_BELOW, LVHT_TOLEFT, and LVHT_TORIGHT to determine whether to scroll the contents of a list view control. Two of these values may be combined. For example, if the position is above and to the left of the client area, you could use both LVHT_ABOVE and LVHT_TOLEFT. You can test for LVHT_ONITEM to determine whether a specified position is over a list view item. This value is a bitwise-OR operation on LVHT_ONITEMICON, LVHT_ONITEMLABEL, and LVHT_ONITEMSTATEICON. iItem 
    Receives the index of the matching item. Or if hit-testing a subitem, this value represents the subitem's parent item. 
    iSubItem 
    Version 4.70. Receives the index of the matching subitem. When hit-testing an item, this member will be zero. 
      

  3.   

    在OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)中处理int nItemIndex = -1; //列表中被选行的索引if((nItemIndex = m_ListCtrl.GetNextItem(-1,LVNI_SELECTED))!=-1)
    {
    ....
    }列,可能你自己要处理
      

  4.   

    给个代码.你看看吧,
    if (!EnsureVisible (Item, TRUE)) 
        {
    InsertItemEx (Item);
    if (!EnsureVisible (Item, TRUE)) 
    return NULL;
        }    // Make sure that nCol is valid
        CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
        int nColumnCount = pHeader->GetItemCount();
        if (Column >= nColumnCount || GetColumnWidth (Column) < 5)
    return NULL;    // Get the column offset
        int Offset = 0;
        for (int iColumn = 0; iColumn < Column; iColumn++)
    Offset += GetColumnWidth (iColumn);    CRect Rect;
        GetItemRect (Item, &Rect, LVIR_BOUNDS);    // Now scroll if we need to expose the column
        CRect ClientRect;
        GetClientRect (&ClientRect);
        if (Offset + Rect.left < 0 || Offset + Rect.left > ClientRect.right)
        {
    CSize Size;
    if (Offset + Rect.left > 0)
    Size.cx = -(Offset - Rect.left);
    else
    Size.cx = Offset - Rect.left;
    Size.cy = 0;
    Scroll (Size);
    Rect.left -= Size.cx;
        }
      

  5.   

    Cxxx::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult)
    {
    HD_NOTIFY *phdn = (HD_NOTIFY*)pNMHDR;
    phdn->iButton就是当前双击的列的编号,0为第一列
    *pResult = 0;
    }
      

  6.   

    LPNMITEMACTIVATE temp = (LPNMITEMACTIVATE) pNMHDR;
          //get the row number
         int nItem = temp->iItem;
         //get the column number
          int  nSubItem = temp->iSubItem;
      

  7.   

    不要忘了设置属性
    m_ListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT |LVS_EX_FLATSB |LVS_EX_GRIDLINES );
      

  8.   

    //使用GetCurSel()可以得到是那一行,
    //用下面的函数可以得到点击的列
    int 
    gxListCtrl::HitTestEx (CPoint& Point, int* pColumn)//返回所点击的列数
    {
        int ColumnNum = 0;
        int Row = HitTest (Point, NULL);
        
        if (pColumn)
    *pColumn = 0;    // Make sure that the ListView is in LVS_REPORT
        if ((GetWindowLong (m_hWnd, GWL_STYLE) & LVS_TYPEMASK) != LVS_REPORT)
    return Row;    // Get the top and bottom row visible
        Row = GetTopIndex();
        int Bottom = Row + GetCountPerPage();//得到总页数
        if (Bottom > GetItemCount())
        Bottom = GetItemCount();//得到总行数
        
        // Get the number of columns
        CHeaderCtrl* pHeader = (CHeaderCtrl*) GetDlgItem(0);
        int nColumnCount = pHeader->GetItemCount();//得到总列数
        // Loop through the visible rows
        for(; Row <= Bottom; Row++)
        {
    // Get bounding rect of item and check whether point falls in it.
    CRect Rect;
    GetItemRect (Row, &Rect, LVIR_BOUNDS);//得到第一个框的区域
    if (Rect.PtInRect (Point))
    {
    // Now find the column
    for (ColumnNum = 0; ColumnNum < nColumnCount; ColumnNum++)
    {
    int ColWidth = GetColumnWidth (ColumnNum);
    if (Point.x >= Rect.left && Point.x <= (Rect.left + ColWidth))
    {
    if (pColumn)
    *pColumn = ColumnNum;
    return Row;
    }
    Rect.left += ColWidth;
    }
    }
        }
        return -1;
    }
      

  9.   

    谢谢各位.
    只是 flyelf(空谷清音)老兄的代码虽可运行(win2000)但MSDN说需winCE.