• 简单的自绘CListBox,重载虚MeasureItem和DrawItem这两个虚函数


     
    1. //例如CNewListBox继承自CListBox,重载虚MeasureItem和DrawItem这两个虚函数,代码如下:  
    2.   
    3. void CNewListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)   
    4. {  
    5.  // TODO: Add your code to draw the specified item  
    6.  ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);  
    7.  LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;  
    8.  ASSERT(lpszText != NULL);  
    9.  CDC dc;  
    10.    
    11.  dc.Attach(lpDrawItemStruct->hDC);  
    12.    
    13.  // Save these value to restore them when done drawing.  
    14.  COLORREF crOldTextColor = dc.GetTextColor();  
    15.  COLORREF crOldBkColor = dc.GetBkColor();  
    16.    
    17.  // If this item is selected, set the background color   
    18.  // and the text color to appropriate values. Also, erase  
    19.  // rect by filling it with the background color.  
    20.  if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&  
    21.   (lpDrawItemStruct->itemState & ODS_SELECTED))  
    22.  {  
    23.   dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));  
    24.   dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));  
    25.   dc.FillSolidRect(&lpDrawItemStruct->rcItem,   
    26.    ::GetSysColor(COLOR_HIGHLIGHT));  
    27.  }  
    28.  else  
    29.  {  
    30.   if(lpDrawItemStruct->itemID%2)  
    31.    dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));  
    32.   else  
    33.    dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));  
    34.  }  
    35.    
    36.  // If this item has the focus, draw a red frame around the  
    37.  // item's rect.  
    38.  if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&  
    39.   (lpDrawItemStruct->itemState & ODS_FOCUS))  
    40.  {  
    41.   CBrush br(RGB(0, 0, 128));  
    42.   dc.FrameRect(&lpDrawItemStruct->rcItem, &br);  
    43.  }  
    44.  lpDrawItemStruct->rcItem.left += 5;  
    45.  // Draw the text.  
    46.  dc.DrawText(  
    47.   lpszText,  
    48.   strlen(lpszText),  
    49.   &lpDrawItemStruct->rcItem,  
    50.   DT_LEFT|DT_SINGLELINE|DT_VCENTER);  
    51.    
    52.  // Reset the background color and the text color back to their  
    53.  // original values.  
    54.  dc.SetTextColor(crOldTextColor);  
    55.  dc.SetBkColor(crOldBkColor);  
    56.    
    57.  dc.Detach();  
    58. }  
    59.   
    60. void CNewListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)   
    61. {  
    62.  // TODO: Add your code to determine the size of specified item  
    63.  ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);  
    64.  LPCTSTR lpszText = (LPCTSTR) lpMeasureItemStruct->itemData;  
    65.  ASSERT(lpszText != NULL);  
    66.  CSize   sz;  
    67.  CDC*    pDC = GetDC();  
    68.    
    69.  sz = pDC->GetTextExtent(lpszText);  
    70.    
    71.  ReleaseDC(pDC);  
    72.    
    73.  lpMeasureItemStruct->itemHeight = 2*sz.cy;  
    74. }  
    75.   
    76. // 其中m_listBox为CNewListBox类型的对象  
    77. #define IDC_LISTBOX 0x1101  
    78.   
    79.  m_listBox.Create(WS_CHILD|WS_VISIBLE|WS_BORDER|WS_VSCROLL|WS_HSCROLL|  
    80.   LBS_OWNERDRAWVARIABLE, CRect(0, 0, 380, 280), this, IDC_LISTBOX);  
      

    效果图如下所示:

    http://blog.csdn.net/visualeleven/article/details/5935430

  • 相关阅读:
    pycharm-1
    WIN7、WIN10 右键在目录当前打开命令行Cmd窗口
    富文本框TinyMCE上传本地图片基本配置
    安装win10正式版后网速变慢的解决方法
    win10远程桌面出现身份验证错误。要求的函数不受支持
    C# 利用VS自带的WSDL工具生成WebService服务类
    解决IIS服务使用C#代码在Windows Server 2012上无法启动Excel的问题
    chorme浏览器不支持audio/video中的autoplay属性的解决方法
    iOS 内购笔记
    利用SortedMap对HashMap进行排序
  • 原文地址:https://www.cnblogs.com/findumars/p/6002456.html
Copyright © 2020-2023  润新知