自绘CListCtrl类

自绘CListCtrl类,重载虚函数DrawItem

void CNewListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 // TODO: Add your code to draw the specified item
 ASSERT(lpDrawItemStruct->CtlType == ODT_LISTVIEW);
 CDC dc;
 dc.Attach(lpDrawItemStruct->hDC);
 ASSERT(NULL != dc.GetSafeHdc());
 // Save these value to restore them when done drawing.
 COLORREF crOldTextColor = dc.GetTextColor();
 COLORREF crOldBkColor = dc.GetBkColor();
 
 // If this item is selected, set the background color
 // and the text color to appropriate values. Also, erase
 // rect by filling it with the background color.
 if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&
  (lpDrawItemStruct->itemState & ODS_SELECTED))
 {
  dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
  dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
  dc.FillSolidRect(&lpDrawItemStruct->rcItem,
   ::GetSysColor(COLOR_HIGHLIGHT));
 }
 else
 {
  if(lpDrawItemStruct->itemID%2)
   dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));
  else
   dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));
 }
 
 // If this item has the focus, draw a red frame around the
 // item's rect.
 if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&
  (lpDrawItemStruct->itemState & ODS_FOCUS))
 {
  CBrush br(RGB(0, 0, 128));
  dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
 }

 // Draw the text.
 CString strText(_T(""));
 CRect rcItem;

 for(int i=0; i<GetHeaderCtrl()->GetItemCount(); i++)
 {
  strText = GetItemText(lpDrawItemStruct->itemID, i);
  GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem);
  rcItem.left += 5;
  dc.DrawText(
   strText,
   strText.GetLength(),
   &rcItem,
  DT_LEFT|DT_SINGLELINE|DT_VCENTER);
 }
 
 // Reset the background color and the text color back to their
 // original values.
 dc.SetTextColor(crOldTextColor);
 dc.SetBkColor(crOldBkColor);
 
 dc.Detach();
}

// 调用

CNewListCtrl m_list; // 类的成员变量

#define IDC_LIST 0x1101
 m_list.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|WS_HSCROLL|LVS_OWNERDRAWFIXED, CRect(0, 0, 280, 280), this, IDC_LIST);
 m_list.ModifyStyle(0, LVS_REPORT|LVS_SINGLESEL);
 m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
 m_list.InsertColumn(0, _T("AAA"), LVCFMT_LEFT, 100);
 m_list.InsertColumn(1, _T("BBB"), LVCFMT_LEFT, 100);

 CString strText(_T(""));
 for(int i=0; i<20; i++)
 {
  m_list.InsertItem(i, _T(""));
  strText.Format(_T("%d - Hello, World!"), i+1);
  m_list.SetItemText(i, 0, strText);
  strText.Format(_T("%d - ABCDEFG"), i+1);
  m_list.SetItemText(i, 1, strText);
 }

 


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/VisualEleven/archive/2010/10/18/5948057.aspx

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值