自绘ListCtrl -- 设置行高

以下是通过重载DramItem()方法来实现自绘,

故需要设置ListCtrl控件属性"Owner Draw Fixed"为TRUE,"Owner Data"为FALSE(默认为FLASE);


1.  准备工作
(1).新建一个MFC类CMyListCtrl,其基类为CListCtrl,
(2).将ListCtrl控件属性"Owner Draw Fixed"设置为TRUE,"Owner Data"设置为FALSE(默认为FLASE);
(3).在ListCtrl控件所在对话框类声明中,修改控件变量声明为CMyListCtrl m_List;
(4).在MyListCtrl.h文件里,添加变量int m_nRowHeight;

2. 手动添加消息宏ON_WM_MEASUREITEM_REFLECT(),并添加下面的函数,实现修改行高 

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

3. 手动添加消息宏ON_WM_MEASUREITEM,响应消息处理OnMeasureItem() 

void CMyListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

4. 添加成员变量int m_nRowHeight,并且添加对外修改接口

void CMyListCtrl::SetRowHeigt(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);
}

5. 手动添加消息宏ON_WM_DRAWITEM(),重载DrawItem()重画列表控件

void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
  CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);    
  LVITEM lvi = {0}; 
  lvi.mask = LVIF_STATE;//|LVIF_IMAGE; 
  lvi.stateMask = LVIS_FOCUSED | LVIS_SELECTED ; 
  lvi.iItem = lpDrawItemStruct->itemID; 
  BOOL bGet = GetItem(&lvi); 
  
  //高亮显示  
  BOOL bHighlight =((lvi.state & LVIS_DROPHILITED)||((lvi.state & LVIS_SELECTED) 
  && ((GetFocus() == this)|| (GetStyle() & LVS_SHOWSELALWAYS))));
  
  //画文本背景 
  CRect rcBack = lpDrawItemStruct->rcItem; 
  pDC->SetBkMode(TRANSPARENT); 
  
  if( bHighlight ) //如果被选中
  { 
    pDC->SetTextColor(RGB(255,255,255));              //文本颜色
    pDC->FillRect(rcBack, &CBrush(RGB(90,162,100)));  //行背景色
  } 
  else 
  { 
    pDC->SetTextColor(RGB(0,0,0));                    //文本颜色
    pDC->FillRect(rcBack, &CBrush(RGB(255,255,255))); //行背景色
  } 
  
  //绘制文本
  if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE) 
  { 
    //得到列数
    int nCollumn = GetHeaderCtrl()->GetItemCount();
    
    //循环处理
    CString szText; 
    for (int i = 0; i < GetHeaderCtrl()->GetItemCount(); i++) 
    {  
      CRect rcItem; 
      if ( !GetSubItemRect(lpDrawItemStruct->itemID, i, LVIR_LABEL, rcItem ))
      { 
        continue; 
      }
      
      szText = GetItemText( lpDrawItemStruct->itemID, i );
      
      rcItem.left += 5; rcItem.right -= 1; 
      pDC->DrawText(szText, lstrlen(szText), &rcItem,  
      DT_LEFT | DT_VCENTER | DT_NOPREFIX | DT_SINGLELINE);  
    } 
  } 
}


改变行高的方法II:也可以通过在每一行插入图标来撑起高,图标可以根据需要是否要真正插入

先设置ListCtrl属性

//加载ListCtrl
LONG lStyle;
lStyle = GetWindowLong(m_TaskList.m_hWnd, GWL_STYLE); //获取当前窗口Style
lStyle &= ~LVS_TYPEMASK; //清除显示方式
lStyle |= LVS_REPORT | LVSIL_SMALL | LVS_ALIGNTOP | LVS_SHOWSELALWAYS; //设置Style
SetWindowLong(m_TaskList.m_hWnd, GWL_STYLE, lStyle);
DWORD dwStyle = m_TaskList.GetExtendedStyle();
//dwStyle |= LVS_EX_FULLROWSELECT; //选中某行使整行高亮(只适用与report风格的listctrl)
dwStyle |= LVS_EX_SUBITEMIMAGES; //subitem添加图标
dwStyle |= LVS_EX_DOUBLEBUFFER;
m_TaskList.SetExtendedStyle(dwStyle);
//::SendMessage(m_TaskList.m_hWnd, LVM_SETEXTENDEDLISTVIEWSTYLE, LVS_EX_FULLROWSELECT, LVS_EX_FULLROWSELECT);

再设置图标(行高)

//设置ListCtrl行高(仅通过cy=44来控制行高,但这里并不需要加载图标,需要再APP下载时根据不同的APP动态加载具体的图标)
m_ImageList.Create(44, 44, ILC_COLOR32 | ILC_MASK, 0, 10); //如果用32*32的会改变行高.ILC_MASK将图标背景设为透明,否则空白地方均为黑色
m_ImageList.SetBkColor(RGB(255, 255, 255)); //需要设置背景色为白色,否则图标镂空的地方都是黑色,且图标有黑边框
//m_nInstall = m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_ICON1));   //只能是.ico图片,.bmp格式不显示, Add返回索引0
////m_nInstall = m_ImageList.Add(LoadIcon(AfxGetResourceHandle(), MAKEINTRESOURCE(IDI_ICON1)));  //或者这个
m_TaskList.SetImageList(&m_ImageList, LVSIL_SMALL); //LVSIL_SMALL为小图标(16*16),LVSIL_NORMAL为大图标(32*32),一定要配套使用,否则不显示


引用:

http://bbs.csdn.net/topics/390471207

http://www.cnblogs.com/rainbowzc/archive/2009/08/30/1556904.html


你可能感兴趣的:(自绘ListCtrl -- 设置行高)