VC编程之VC 自绘CListCtrl类
小标 2018-11-15 来源 : 阅读 1015 评论 0

摘要:本文主要向大家介绍了VC编程之VC 自绘CListCtrl类,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

本文主要向大家介绍了VC编程之VC 自绘CListCtrl类,通过具体的内容向大家展示,希望对大家学习VC编程有所帮助。

实现功能:鼠标在ListCtrl上滑动的时候,滑动到哪一行哪一行就高亮,有且仅有一条是高亮的。


如何实现下面两个功能:
1.当鼠标离开ListCtrl的时候,高亮行取消高亮。
2.更改高亮行的背景色,默认是蓝色的,客户想要其它的颜色。


    //自绘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; iGetItemCount(); 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);  
     } 

以上就介绍了VC/MFC的学习,希望对VC/MFC有兴趣的朋友有所帮助。了解更多内容,请关注职坐标编程语言VC/MFC频道!

本文由 @小标 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程