CListCtrl 几点经验

个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);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值