zoukankan      html  css  js  c++  java
  • 简单的自绘CListBox(多行显示)(覆盖DrawItem函数,然后用CDC绘制)

          之前写过一个自绘的CListBox类,详细请参考http://blog.csdn.net/VisualEleven/archive/2010/10/12/5935430.aspx
    现在修改这之前的代码,使该CListBox能够支持多行显示的问题。

    [cpp] view plain copy
     
    1. // 重写DrawItem虚函数  
    2. void CNewListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)   
    3. {  
    4.  // TODO: Add your code to draw the specified item  
    5.  ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);  
    6.  LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData;  
    7.  ASSERT(lpszText != NULL);  
    8.  CDC dc;  
    9.    
    10.  dc.Attach(lpDrawItemStruct->hDC);  
    11.    
    12.  // Save these value to restore them when done drawing.  
    13.  COLORREF crOldTextColor = dc.GetTextColor();  
    14.  COLORREF crOldBkColor = dc.GetBkColor();  
    15.    
    16.  // If this item is selected, set the background color   
    17.  // and the text color to appropriate values. Also, erase  
    18.  // rect by filling it with the background color.  
    19.  if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&  
    20.   (lpDrawItemStruct->itemState & ODS_SELECTED))  
    21.  {  
    22.   dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));  
    23.   dc.SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));  
    24.   dc.FillSolidRect(&lpDrawItemStruct->rcItem,   
    25.    ::GetSysColor(COLOR_HIGHLIGHT));  
    26.  }  
    27.  else  
    28.  {  
    29.   if(lpDrawItemStruct->itemID%2)  
    30.    dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128,128,128));  
    31.   else  
    32.    dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255,128,255));  
    33.  }  
    34.    
    35.  // If this item has the focus, draw a red frame around the  
    36.  // item's rect.  
    37.  if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&  
    38.   (lpDrawItemStruct->itemState & ODS_FOCUS))  
    39.  {  
    40.   CBrush br(RGB(0, 0, 128));  
    41.   dc.FrameRect(&lpDrawItemStruct->rcItem, &br);  
    42.  }  
    43.  lpDrawItemStruct->rcItem.left += 5;  
    44.  // Draw the text.  
    45.  dc.DrawText(  
    46.   lpszText,  
    47.   strlen(lpszText),  
    48.   &lpDrawItemStruct->rcItem,  
    49.   DT_WORDBREAK);  
    50.    
    51.  // Reset the background color and the text color back to their  
    52.  // original values.  
    53.  dc.SetTextColor(crOldTextColor);  
    54.  dc.SetBkColor(crOldBkColor);  
    55.    
    56.  dc.Detach();  
    57. }  
    58.   
    59.    
    60.   
    61. // 重写MeasureItem虚函数  
    62.   
    63. void CNewListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)   
    64. {  
    65.  // TODO: Add your code to determine the size of specified item  
    66.  ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);  
    67.  LPCTSTR lpszText = (LPCTSTR) lpMeasureItemStruct->itemData;  
    68.  ASSERT(lpszText != NULL);  
    69.  CRect rect;  
    70.  GetItemRect(lpMeasureItemStruct->itemID, &rect);  
    71.   
    72.  CDC* pDC = GetDC();   
    73.  lpMeasureItemStruct->itemHeight = pDC->DrawText(lpszText, -1, rect, DT_WORDBREAK | DT_CALCRECT);   
    74.  ReleaseDC(pDC);  
    75. }  
    76.   
    77. // 调用方法  
    78.   
    79. CNewListBox m_listBox; // 成员变量  
    80.   
    81.    
    82.   
    83. #define IDC_LISTBOX 0x1010  
    84.  m_listBox.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |  
    85.   LBS_OWNERDRAWVARIABLE , CRect(0, 0, 300, 200), this, IDC_LISTBOX);  
    86.   
    87.  for(int i=0; i<10; i++)  
    88.  {  
    89.   if(i%2)  
    90.    m_listBox.AddString(_T("Hello,World1/r/nHello,World2"));  
    91.   else  
    92.    m_listBox.AddString(_T("Hello,World"));  
    93.  }  
      

    效果图如下所示:

     
     

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

  • 相关阅读:
    在响应式项目中连接设计与开发
    社交APP的痛点及九大流派解析,微信陌陌,咱还约吗
    网传奶茶妹将进入红杉资本
    OLE、OCX和ActiveX控件之间的比较
    Unity3D网络游戏实战(第2版)
    离婚?在Facebook上把已婚状态改为单身就可以!
    同样酷炫但却失败了的谷歌眼镜,能否给Apple Watch一些前车之鉴?
    Apple Watch首批评测放出:有吐槽、有体贴……毒哭了,暖哭了
    如何培养战略领导力,赢得“长久游戏”?
    博客社交已死,数据社交“永生”
  • 原文地址:https://www.cnblogs.com/findumars/p/6005599.html
Copyright © 2011-2022 走看看