实现CListCtrl自定义行高

1. 自定义CMyListCtrl:CListCtrl,并添加如下变量及函数:

 int m_nRowHeight;

    int m_nRowHeight;


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

void CMyListCtrl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
    CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}

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);
}

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

}

 

 

2. 添加消息:

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

 

3. 在使用该控件的地方,设置该控件资源:

Report、Ower draw fixed

4. 初始化该控件测试。如:

    m_list.InsertColumn( 0, "名称");
     m_list.InsertColumn( 1, "消息详情");
    m_list.SetColumnWidth( 0, 220);
    m_list.SetColumnWidth( 1,243);
    m_list.InsertItem(0,"检查轴位");//插入行
    m_list.InsertItem(1,"故障尺寸");
    m_list.InsertItem(2,"尺寸单位");
    m_list.InsertItem(3,"处理关门"); 
    m_list.SetItemText(0,1,"L1");//默认显示
    m_list.SetItemText(1,1,"0");
    m_list.SetItemText(2,1,"mm");
    m_list.SetItemText(3,1,"");

    m_list.SetRowHeigt(50);//调用设置行高

     m_list.SetItemState(0, LVIS_SELECTED, LVIS_SELECTED);

 

posted @ 2013-03-22 22:07  Roarsun  阅读(10143)  评论(0编辑  收藏  举报