列表框设置每行行高

来源:互联网 发布:站长之家seo综合查询 编辑:程序博客网 时间:2024/05/01 14:15

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添加处理函数,不要用类向导。

在函数中设置行高度,就像这样:

[cpp] view plaincopyprint?
  1. void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)  
  2. {  
  3.     lpMeasureItemStruct->itemHeight = m_nRowHeight;  
  4. }  
void CMyListCtrl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct){lpMeasureItemStruct->itemHeight = m_nRowHeight;}


PS:4,5步为什么可以这么做?这涉及到消息的反射机制,我暂时也不清楚。

 

6.为CMyListCtrl类添加一个接口方法用来设置m_nRowHeight的高度;

[cpp] view plaincopyprint?
  1. void CMyListCtrl::SetRowHeight(int nHeight)  
  2. {  
  3.     m_nRowHeight = nHeight;  
  4.     //如果没有下面的代码,设置并不能奏效   
  5.     CRect rcWin;  
  6.     GetWindowRect(&rcWin);  
  7.     WINDOWPOS wp;  
  8.     wp.hwnd = m_hWnd;  
  9.     wp.cx = rcWin.Width();  
  10.     wp.cy = rcWin.Height();  
  11.     wp.flags = SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOOWNERZORDER | SWP_NOZORDER;  
  12.     SendMessage(WM_WINDOWPOSCHANGED, 0, (LPARAM)&wp);  
  13. }  
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代码如下,你可以根据需要修改

[cpp] view plaincopyprint?
  1. void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)   
  2. {  
  3. TCHAR lpBuffer[256];  
  4.   
  5. LV_ITEM lvi;  
  6.   
  7. lvi.mask = LVIF_TEXT | LVIF_PARAM ;  
  8. lvi.iItem = lpDrawItemStruct->itemID ;   
  9. lvi.iSubItem = 0;  
  10. lvi.pszText = lpBuffer ;  
  11. lvi.cchTextMax = sizeof(lpBuffer);  
  12. VERIFY(GetItem(&lvi));  
  13.   
  14. LV_COLUMN lvc, lvcprev ;  
  15. ::ZeroMemory(&lvc, sizeof(lvc));  
  16. ::ZeroMemory(&lvcprev, sizeof(lvcprev));  
  17. lvc.mask = LVCF_WIDTH | LVCF_FMT;  
  18. lvcprev.mask = LVCF_WIDTH | LVCF_FMT;  
  19.   
  20. for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)  
  21. {  
  22.    if ( nCol > 0 )   
  23.    {  
  24.     // Get Previous Column Width in order to move the next display item  
  25.     GetColumn(nCol-1, &lvcprev) ;  
  26.     lpDrawItemStruct->rcItem.left += lvcprev.cx ;  
  27.     lpDrawItemStruct->rcItem.right += lpDrawItemStruct->rcItem.left ;   
  28.    }  
  29.   
  30.    // Get the text    
  31.    ::ZeroMemory(&lvi, sizeof(lvi));  
  32.    lvi.iItem = lpDrawItemStruct->itemID;  
  33.    lvi.mask = LVIF_TEXT | LVIF_PARAM;  
  34.    lvi.iSubItem = nCol;  
  35.    lvi.pszText = lpBuffer;  
  36.    lvi.cchTextMax = sizeof(lpBuffer);  
  37.    VERIFY(GetItem(&lvi));  
  38.   
  39.    CDC* pDC;  
  40.    pDC = CDC::FromHandle(lpDrawItemStruct->hDC);  
  41.   
  42.    if ( lpDrawItemStruct->itemState & ODS_SELECTED )  
  43.    {  
  44.     pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_HIGHLIGHT)) ;   
  45.     pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;  
  46.    }  
  47.    else  
  48.    {  
  49.     pDC->FillSolidRect(&lpDrawItemStruct->rcItem, GetSysColor(COLOR_WINDOW)) ;  
  50.     pDC->SetTextColor(GetSysColor(COLOR_WINDOWTEXT)) ;   
  51.    }  
  52.     
  53.    pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));  
  54.   
  55.    UINT   uFormat    = DT_LEFT ;  
  56.   
  57.    ::DrawText(lpDrawItemStruct->hDC, lpBuffer, strlen(lpBuffer),   
  58.     &lpDrawItemStruct->rcItem, uFormat) ;  
  59.   
  60.    pDC->SelectStockObject(SYSTEM_FONT) ;  
  61. }  
  62. }  
原创粉丝点击