目录
2.CMyListCtrlEx类(继承自CListCtrl)
1.效果图
2.CMyListCtrlEx类(继承自CListCtrl)
2.1 CMyListCtrlEx.h
#pragma once
// CMyListCtrlEx 窗体视图
class CMyListCtrlEx : public CListCtrl
{
private:
unsigned int m_uRowHeight;
CRect m_listRect;
public:
CMyListCtrlEx();
virtual ~CMyListCtrlEx();
protected:
virtual void DoDataExchange(CDataExchange* pDX);
DECLARE_MESSAGE_MAP()
public:
void SetRowHeigt(int nHeight);
afx_msg void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
2.2 CMyListCtrlEx.cpp
#include "stdafx.h"
#include "CMyListCtrlEx.h"
CMyListCtrlEx::CMyListCtrlEx()
{
m_uRowHeight = 60;
}
CMyListCtrlEx::~CMyListCtrlEx()
{
}
void CMyListCtrlEx::DoDataExchange(CDataExchange* pDX)
{
CListCtrl::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CMyListCtrlEx, CListCtrl)
ON_WM_MEASUREITEM_REFLECT()
ON_WM_MEASUREITEM()
ON_WM_DRAWITEM()
END_MESSAGE_MAP()
void CMyListCtrlEx::SetRowHeigt(int nHeight)
{
m_uRowHeight = 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 CMyListCtrlEx::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
void CMyListCtrlEx::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct->itemHeight = m_uRowHeight;
}
void CMyListCtrlEx::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 bSelect = ((lvi.state & LVIS_DROPHILITED) || ((lvi.state & LVIS_SELECTED)
&& ((GetFocus() == this) || (GetStyle() & LVS_SHOWSELALWAYS))));
//画文本背景
CRect bgRect = lpDrawItemStruct->rcItem;
if (bSelect)//设置选中颜色
{
pDC->SetTextColor(RGB(255, 255, 255));//白色文本
pDC->FillRect(bgRect, &CBrush(RGB(88, 88, 88)));//深灰色背景
}
else
{
int iRow = lvi.iItem;
if (iRow % 2 == 0)//设置偶数行文字颜色和背景颜色
{
pDC->SetTextColor(RGB(0, 0, 0));//黑色文本
pDC->FillRect(bgRect, &CBrush(RGB(128, 128, 128))); //灰色背景
}
else//设置奇数行文字颜色和背景颜色
{
pDC->SetTextColor(RGB(0, 0, 0));//黑色文本
pDC->FillRect(bgRect, &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);
}
}
}
3.使用CMyListCtrlEx创建表格
CRect rect;
auto parent = this->GetParent();
if (parent)
{
parent->GetClientRect(&rect);
parent->ClientToScreen(&rect);//647*450
}
m_listCtrl.Create(WS_BORDER | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS
| LVS_NOCOLUMNHEADER | LVS_OWNERDRAWFIXED, rect, this, 101);
m_listCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | WS_VSCROLL);
m_listCtrl.InsertColumn(0, _T(""), LVCFMT_LEFT, 0);//第0列无法居中显示,一般将其隐藏
m_listCtrl.InsertColumn(1, _T("No"), LVCFMT_LEFT, 30); // 插入第2列的列名
m_listCtrl.InsertColumn(2, _T("Name"), LVCFMT_LEFT, 450); // 插入第3列的列名
m_listCtrl.InsertColumn(3, _T("State"), LVCFMT_LEFT, 70); // 插入第4列的列名
m_listCtrl.InsertColumn(4, _T("Operation"), LVCFMT_LEFT, 90); // 插入第4列的列名
m_listCtrl.SetColumnWidth(4, LVSCW_AUTOSIZE_USEHEADER);
CString strNo;
CString strName;
CString strState;
CString strOpt;
for (int i = 0; i <= 5; i++) {
strNo.Format(_T("%d"), i);
strName.Format(_T("Ecu%d"), i);
strState.Format(_T("未扫描"), 20 + i);
strOpt = _T("进入系统");
m_listCtrl.InsertItem(i, _T("")); // 插入行
m_listCtrl.SetItemText(i, 1, strNo);
m_listCtrl.SetItemText(i, 2, strName);
m_listCtrl.SetItemText(i, 3, strState);
m_listCtrl.SetItemText(i, 4, strOpt);
}
如有问题,可以留言咨询。