RT (部分转载)

解决方案 »

  1.   

    (部分转载)
    每个List控件都有一个CHeaderCtrl。且它的ID是0。
    CHeaderCtrl* pHeader =(CHeaderCtrl*)m_listCtrl.GetDlgItem(0);
    即使List控件非report模式,Header控件也存在,只是此时它的尺寸为0。
    可利用以下代码使得控件的第一列自适应大小:
    m_listctrl.SetColumnWidth( 0, LVSCW_AUTOSIZE );
    List 控件中的图标初始化时可如下设置:
    m_listCtrl.InsertItem( LVIF_TEXT | LVIF_IMAGE, nRow, sItemText, 0, 0, nImage
    , NULL);
    在运行中需动态改变可调用SetItem()函数.
    m_listCtrl.SetItem( 0, 0, LVIF_IMAGE, NULL, nImage, 0, 0, 0 );
    删去图标可将nImage设置为-1。以下代码可将列表框的第一列限定大小:
    BOOL CMyListCtrl::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
    {
            HD_NOTIFY   *pHDN = (HD_NOTIFY*)lParam;
            if((pHDN->hdr.code == HDN_BEGINTRACKW || pHDN->hdr.code == HDN_BEGIN
    TRACKA)
                    && pHDN->iItem == 0)            // Prevent only first (col# 
    0) from resizing
            {
                    *pResult = TRUE;                // disable tracking
                    return TRUE;                    // Processed message
            }
            return CListCtrl::OnNotify(wParam, lParam, pResult);
    }
    选定cell
    CListCtrl::OnClick(...)
    {
        int column;
        CRect m_rect;
        //the function below is provided in CListCtrl inPlace editing
        int index = GetRowColumnIndex(point, &column);
        if(index == -1)return;
        int offset = 0;
        for(int i = 0; i < column; i++)
          offset += GetColumnWidth(i);
        //Get the rectangle of the label and the icon
        GetItemRect(index, &m_rect, LVIR_BOUNDS);
        m_rect.left += offset + 4;
        //Get the columnWidth of the selected column
        m_rect.right = m_rect.left + GetColumnWidth(column);
        Update(index);
        CClientDC dc(this);    //this is the pointer of the current view
        dc.DrawFocusRect(m_rect);
    }Select NonFirst cell:void CMyListCtrl::OnLButtonDown(UINT nFlags, CPoint point)
    {
            CListCtrl::OnLButtonDown(nFlags, point);
            int index;
            point.x = 2;
            if( ( index = HitTest( point, NULL )) != -1 )
            {
                    SetItemState( index, LVIS_SELECTED | LVIS_FOCUSED ,
                                    LVIS_SELECTED | LVIS_FOCUSED);
            }
    }
    HitTestEx的代码如下:
    判断点击的是哪一列:
    // HitTestEx - Determine the row index and column index for a point
    // Returns - the row index or -1 if point is not over a row
    // point - point to be tested.
    // col  - to hold the column index
    int CMyListCtrl::HitTestEx(CPoint &point, int *col) const
    {
     int colnum = 0;
     int row = HitTest( point, NULL );
     if( col ) *col = 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( colnum = 0; colnum < nColumnCount; colnum++ )
       {
        int colwidth = GetColumnWidth(colnum);
        if( point.x >= rect.left
         && point.x <= (rect.left + colwidth ) )
        {
         if( col ) *col = colnum;
         return row;
        }
        rect.left += colwidth;
       }
      }
     }
     return -1;
    }
    选中一定范围内的Item
    // SelItemRange - Selects/Deselect a range of items
    // Returns              - The number of new items selected
    // bSelect              - TRUE to select, FALSE to deselect
    // nFirstItem           - index of first item to select
    // nLastItem            - index of last item to select
    int CMyListCtrl::SelItemRange(BOOL bSelect, int nFirstItem, int nLastItem)
    {
            // make sure nFirstItem and nLastItem are valid
            if( nFirstItem >= GetItemCount() || nLastItem >= GetItemCount() )
                    return 0;
            int nItemsSelected = 0;
            int nFlags = bSelect ? 0 : LVNI_SELECTED;
            int nItem = nFirstItem - 1;
            while( (nItem = GetNextItem( nItem, nFlags )) >=0
                            && nItem <= nLastItem )
            {
                    nItemsSelected++;
                    SetItemState(nItem, bSelect ? LVIS_SELECTED : 0, LVIS_SELECT
    ED );
            }
            return nItemsSelected;
    }
    SetHeaderBitmap:static int _gnCols = 5;
    static int _gnColSize[] =
    {
     18,21,22,18,380
    };
    static CString _gcsColLabel[] =
    {
     _T("x"),
     _T("x"),
     _T("x"),
     _T("x"),
     _T("Description:")
    };
    void CRightView::BuildColumns()
    {
     // Insert the columns into the list control
     LV_COLUMN lvCol;
     lvCol.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
     for (int i = 0; i < _gnCols; ++i)
     {
      lvCol.iSubItem = i;
      lvCol.pszText = (char*)(LPCTSTR)_gcsColLabel[i];
      lvCol.cx = _gnColSize[i];
      lvCol.fmt = LVCFMT_LEFT;
      m_ListCtrl->InsertColumn(i, &lvCol);
     }
     for (int x = 0; x < _gnCols-1; x++)
      SetHeaderBitmap(x, nHeaderBmps[x], HDF_STRING);
    }
    void CRightView::SetHeaderBitmap(int nCol, int nBitmap, DWORD dwRemove)
    {
     CHeaderCtrl* pHeader = (CHeaderCtrl*)GetDlgItem(0);
     HD_ITEM hdi;
     hdi.mask = HDI_FORMAT;
     pHeader->GetItem (nCol, &hdi);
     hdi.mask = HDI_BITMAP | HDI_FORMAT;
     hdi.fmt  |= HDF_BITMAP;
     hdi.hbm = (HBITMAP)::LoadImage(AfxGetInstanceHandle(),
      MAKEINTRESOURCE(nBitmap),IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
     if (dwRemove)
      hdi.fmt &= ~dwRemove;
     pHeader->SetItem (nCol, &hdi);
    }
      

  2.   

    删除功能的实现(VCBASE)要实现删除功能,必须取得选中表项的数和表项总数,并且需要从后向前进行依次删除,其原因是每个表项被删除后,其后各表项的索引号均会发生递减变化,如果采取从前向后删除的方法,就会造成无法正常删除选中的表项,其功能代码如下:void CVCLISTDlg::OnDel() //删除按钮功能{ // TODO: Add your control notification handler code hereint i,iState;int nItemSelected=m_ListCtrl.GetSelectedCount();//所选表项数int nItemCount=m_ListCtrl.GetItemCount();//表项总数if(nItemSelected<1) return;for(i=nItemCount-1;i>=0;i--){iState=m_ListCtrl.GetItemState(i,LVIS_SELECTED);if(iState!=0) m_ListCtrl.DeleteItem(i);}}通过专用按钮来实现排序功能,如本文的排序按钮对应的功能代码如下:(VCBASE)void CVCLISTDlg::OnSort() { // TODO: Add your control notification handler code herem_ListCtrl.SortItems((PFNLVCOMPARE)CompareFunc,0);
    }还有很多,以后再贴吧!^_^