首页 > 代码库 > VCCListCtrl显示长字符串自定义类

VCCListCtrl显示长字符串自定义类

1.h

 1 #pragma once
 2 
 3 
 4 // CMyListCtrl
 5 
 6 class CMyListCtrl : public CListCtrl
 7 {
 8     DECLARE_DYNAMIC(CMyListCtrl)
 9 
10 public:
11     CMyListCtrl();
12     virtual ~CMyListCtrl();
13 
14     CString GetItemText(int nItem, int nSubItem) const;
15     void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
16 
17 protected:
18     DECLARE_MESSAGE_MAP()
19 };

2.cpp

  1 // MyListCtrl.cpp : 实现文件
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "YdTool.h"
  6 #include "MyListCtrl.h"
  7 
  8 #ifdef _AFX
  9 #ifdef _DEBUG
 10 #define new DEBUG_NEW
 11 #undef THIS_FILE
 12 static const char THIS_FILE[] = __FILE__;
 13 #endif
 14 #endif
 15 
 16 // CMyListCtrl
 17 
 18 IMPLEMENT_DYNAMIC(CMyListCtrl, CListCtrl)
 19 
 20 CMyListCtrl::CMyListCtrl()
 21 {
 22 
 23 }
 24 
 25 CMyListCtrl::~CMyListCtrl()
 26 {
 27 }
 28 
 29 
 30 BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
 31 END_MESSAGE_MAP()
 32 
 33 
 34 
 35 // CMyListCtrl 消息处理程序
 36 
 37 
 38 CString CMyListCtrl::GetItemText(int nItem, int nSubItem) const
 39 {
 40     ASSERT(::IsWindow(m_hWnd));
 41     LVITEM lvi;
 42     memset(&lvi, 0, sizeof(LVITEM));
 43     lvi.iSubItem = nSubItem;
 44     CString str;
 45     int nLen = 640;
 46     int nRes;
 47     do
 48     {
 49         nLen *= 2;
 50         lvi.cchTextMax = nLen;
 51         lvi.pszText = str.GetBufferSetLength(nLen);
 52         nRes  = (int)::SendMessage(m_hWnd, LVM_GETITEMTEXT, (WPARAM)nItem,
 53             (LPARAM)&lvi);
 54     } while (nRes >= nLen-1);
 55     str.ReleaseBuffer();
 56     return str;
 57 }
 58 
 59 void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
 60 {
 61     CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
 62     CRect rcItem(lpDrawItemStruct->rcItem);
 63     int nItem = lpDrawItemStruct->itemID;
 64     COLORREF clrTextSave, clrBkSave;
 65     static _TCHAR szBuff[50000];//这里是你要显示的字符串长度,想多长有多长
 66     LV_ITEM lvi;
 67     lvi.mask = LVIF_TEXT | LVIF_STATE;//LVIF_IMAGE |
 68     lvi.iItem = nItem;
 69     lvi.iSubItem = 0;
 70     lvi.pszText = szBuff;
 71     lvi.cchTextMax = sizeof(szBuff);
 72     lvi.stateMask = 0xFFFF;
 73     GetItem(&lvi);
 74 
 75     BOOL bSelected = (lvi.state & LVIS_SELECTED);
 76     CRect rcAllLabels;
 77     GetItemRect(nItem, rcAllLabels, LVIR_BOUNDS);
 78     if (bSelected)
 79     {
 80         clrTextSave = pDC->SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));
 81         clrBkSave = pDC->SetBkColor(::GetSysColor(COLOR_HIGHLIGHT));
 82         pDC->FillRect(rcAllLabels, &CBrush(::GetSysColor(COLOR_HIGHLIGHT)));
 83     }
 84     GetItemRect(nItem, rcItem, LVIR_LABEL);
 85     pDC->DrawText(szBuff, -1, rcItem, DT_LEFT | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
 86 
 87     LV_COLUMN lvc;
 88     lvc.mask = LVCF_FMT | LVCF_WIDTH;
 89     for (int nColumn = 1; GetColumn(nColumn, &lvc); nColumn++)
 90     {
 91         rcItem.left = rcItem.right;
 92         rcItem.right += lvc.cx;
 93 
 94         int nRetLen = CListCtrl::GetItemText(nItem, nColumn,
 95             szBuff, sizeof(szBuff));
 96         if (nRetLen == 0)
 97             continue;
 98         UINT nJustify = DT_LEFT;
 99         switch (lvc.fmt & LVCFMT_JUSTIFYMASK)
100         {
101         case LVCFMT_RIGHT:
102             nJustify = DT_RIGHT;
103             break;
104         case LVCFMT_CENTER:
105             nJustify = DT_CENTER;
106             break;
107         default:
108             break;
109         }//单步执行时,szBuff里面长30000字节的字符串是正确的,但是执行完之后发现输出为空白
110         pDC->DrawText(szBuff, -1, rcItem, 
111             nJustify | DT_SINGLELINE | DT_NOPREFIX | DT_NOCLIP | DT_VCENTER);
112     }
113     if (lvi.state & LVIS_FOCUSED)
114         pDC->DrawFocusRect(rcAllLabels);
115     if (bSelected)
116     {
117         pDC->SetTextColor(clrTextSave);
118         pDC->SetBkColor(clrBkSave);
119     }
120 }

 

VCCListCtrl显示长字符串自定义类