MFC的标准列表框控件并没有给出设置每一行高度的接口。
有两种方法可以设置:
1.取巧的方法:为列表框插入图标,每一行的高度自动变为图标的高度。这个方法很快,但总觉得有点别扭,且不好移植。所以我没用这种方法。
2.自绘列表框,这正是本文要说的。
效果预览
实现过程
1.在对话框上拖拉出一个List Control 控件,并设置属性Owen Draw Fixed属性为TRUR;(重载代码在本文最后)
2.为工程添加一个MFC类(这里我假设类名叫CMyListCtrl),派生于MFC的CListCtrl类。并重载CListCtrl::DrawItem方法;
3.为CMyListCtrl类添加变量int m_nRowHeight变量,用来保存指定的行高度;
4.在CMyListCtrl的MESSAGE_MAP中添加宏ON_WM_MEASUREITEM_REFLECT()
具体是到CMyLsitCtrl的cpp文件中添加,看起来就像这样:
BEGIN_MESSAGE_MAP(CMyListCtrl, CListCtrl)
ON_WM_MEASUREITEM_REFLECT()
END_MESSAGE_MAP()
5.重载CMyListCtrl::MeasureItem方法。这一步需要靠手工添加,类向导并不能帮你。注意:这一步不是给CMyListCtrl添加处理函数,不要用类向导。
在函数中设置行高度,就像这样:
- void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
- {
- lpMeasureItemStruct->itemHeight = m_nRowHeight;
- }
void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_nRowHeight;
}
PS:4,5步为什么可以这么做?这涉及到消息的反射机制,我暂时也不清楚。
6.为CMyListCtrl类添加一个接口方法用来设置m_nRowHeight的高度;
- void CMyListCtrl::SetRowHeight(int nHeight)
- {
- m_nRowHeight = 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 CMyListCtrl::SetRowHeight(int nHeight)
{
m_nRowHeight = 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);
}
7.为在1中添加的列表框控件添加一个变量m_MyListCtrl,类型为CMyListCtrl。在对话框的初始化函数中调用m_MyListCtrl.SetRowHeight(60)。
8.加入其它代码,编译、运行,如果没有什么问题,你可以看到设置行高度起作用了。
重载的CListCtrl::DrawItem代码如下,你可以根据需要修改
- void CMyListCtrl::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(DEFAULT_GUI_FONT));
- UINT uFormat = DT_LEFT ;
- ::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer),
- &lpDrawItemStruct->rcItem, uFormat) ;
- pDC->SelectStockObject(SYSTEM_FONT) ;
- }
- }