15,979
社区成员
发帖
与我相关
我的任务
分享
void CListEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if( m_nItemHeight > 0 )
lpMeasureItemStruct->itemHeight = m_nItemHeight;
}
其中 m_nItemHeight 是我在头文件中声明的一个成员变量,用于从外部修改列表项高度.
然后我们添加一个方法,便于从外部直接修改列表项高度:
//设置行高
void SetItemHeight(UINT nHeight);
//设置行高方法的定义
void CListEx::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);
}
这个方法的最后,使用了 SendMessage 发送 WM_WINDOWPOSCHANGED 消息让 CListCtrl 进入 MeasureItem 的消息响应函数,对列表高度进行修改.
因为我们这里使用了列表的自绘风格,因此列表项需要自己绘制.
首先在类的声明中添加 DrawItem 虚函数声明:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
然后自画 CListCtrl:
void CListEx::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
int nItem = lpDrawItemStruct->itemID;
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
......
CRect rcBound, rcLabel, rcIcon;
//获得列表项图标,标签,及项的区域
GetItemRect ( nItem, rcIcon, LVIR_ICON );
GetItemRect ( nItem, rcLabel, LVIR_LABEL );
GetItemRect ( nItem, rcBound, LVIR_BOUNDS );
......
}
现在这个 CListCtrl 的重载类就支持自定义列表项高度了.