自己写的一个可以改变行高的CListCtrl函数

class newctrl : public CListCtrl
{
// Construction
public:
 newctrl();

// Attributes
public:
 UINT m_nItemHeight;
// Operations
public:
 void SetItemHeight(UINT nHeight);
// Overrides
 // ClassWizard generated virtual function overrides
 //{{AFX_VIRTUAL(newctrl)
  virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
 //}}AFX_VIRTUAL

// Implementation
public:
 virtual ~newctrl();

 // Generated message map functions
protected:
 //{{AFX_MSG(newctrl)
  // NOTE - the ClassWizard will add and remove member functions here.
  afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
 //}}AFX_MSG

 DECLARE_MESSAGE_MAP()
};

 

 

 

newctrl::newctrl()
{
  m_nItemHeight=0;
}

newctrl::~newctrl()
{
}


BEGIN_MESSAGE_MAP(newctrl, CListCtrl)
 //{{AFX_MSG_MAP(newctrl)
  // NOTE - the ClassWizard will add and remove mapping macros here.
   ON_WM_MEASUREITEM_REFLECT()
 //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/
// newctrl message handlers
void newctrl::SetItemHeight(UINT nHeight)
{
 m_nItemHeight = nHeight;
 CRect rcWin;
 GetWindowRect(&rcWin);
 WINDOWPOS wp;
 wp.hwnd = m_hWnd;
 wp.cx = rcWin.Width();
 wp.cy = rcWin.Height();
 wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;
 SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);
}

void newctrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
 TCHAR  lpBuffer[256];

 LV_ITEM lvi;

 lvi.mask = LVIF_TEXT | LVIF_PARAM ;
 lvi.iItem = lpDrawItemStruct->itemID ;  
 lvi.iSubItem = 0;
 lvi.pszText = lpBuffer ;
 lvi.cchTextMax = sizeof(lpBuffer);
 VERIFY(GetItem(&lvi));

 LV_COLUMN lvc, lvcprev ;
 ::ZeroMemory(&lvc, sizeof(lvc));
 ::ZeroMemory(&lvcprev, sizeof(lvcprev));
 lvc.mask = LVCF_WIDTH | LVCF_FMT;
 lvcprev.mask = LVCF_WIDTH | LVCF_FMT;

 for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
 {
  if ( nCol > 0 )
  {
   // Get Previous Column Width in order to move the next display item
   GetColumn(nCol-1, &lvcprev) ;
   lpDrawItemStruct->rcItem.left += lvcprev.cx ;
   lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ; 
  }

  // Get the text
  ::ZeroMemory(&lvi, sizeof(lvi));
  lvi.iItem = lpDrawItemStruct->itemID;
  lvi.mask = LVIF_TEXT | LVIF_PARAM;
  lvi.iSubItem = nCol;
  lvi.pszText = lpBuffer;
  lvi.cchTextMax = sizeof(lpBuffer);
  VERIFY(GetItem(&lvi));

  CDC* pDC;
  pDC = CDC::FromHandle(lpDrawItemStruct->hDC);

  if ( lpDrawItemStruct->itemState & ODS_SELECTED )
  {
   pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;
   pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
  }
  else
  {
   pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;
   pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;
  }
  
  pDC->SelectObject(GetStockObject(SYSTEM_FONT));

  UINT uFormat=DT_LEFT ;

  ::DrawText(lpDrawItemStruct->hDC, lpBuffer, wcslen(lpBuffer),
    &lpDrawItemStruct->rcItem, uFormat) ;

  pDC->SelectStockObject(SYSTEM_FONT) ;
 }
}

void newctrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
 if(m_nItemHeight>0)
 lpMeasureItemStruct->itemHeight=m_nItemHeight;
}

 

 

注意事项:

这几个函数都必须手动添加,不能够用继承的函数

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值