VS之listview使用

本文简单地介绍下VS的listview使用:包括属性设置、背景设置、双击事件、右键弹出菜单等。

使用工具:VS2008

使用语言:C++

开发步骤:

1.新建单文档程序,View派生于ListView

2.添加菜单资源

VS之listview使用_第1张图片

3.设置属性

void CListViewDemoView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: 调用 GetListCtrl() 直接访问 ListView 的列表控件,
	//  从而可以用项填充 ListView。
	//属性设置
	CListCtrl& list = GetListCtrl();
	list.ModifyStyle(LVS_TYPEMASK,LVS_REPORT & LVS_TYPEMASK | LVS_SINGLESEL);//important for show
	DWORD dwStyle = list.GetExtendedStyle();
	list.SetExtendedStyle(dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	CImageList image;
	image.Create(1,20,ILC_COLOR24 | ILC_MASK,4,0);
	list.SetImageList(&image,LVSIL_SMALL);

	CFont font;
	font.CreateFont(
		16,
		0,
		0,
		0,
		FW_NORMAL,
		FALSE,
		FALSE,
		0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("宋体"));
	list.SetFont(&font);
	list.GetHeaderCtrl()->SetFont(&font);

	//菜单
	m_menu.LoadMenu(IDR_MENU1);
	list.SetMenu(&m_menu);

	//标题栏
	list.InsertColumn(0,_T("名称"),LVCFMT_LEFT,150,0);
	list.InsertColumn(1,_T("属性1"),LVCFMT_LEFT,120,0);
	list.InsertColumn(2,_T("属性2"),LVCFMT_LEFT,120,0);

	UpdateList();
}
VS之listview使用_第2张图片

4.更新数据

void CListViewDemoView::UpdateList(void)
{
	CListCtrl& list = GetListCtrl();
	CString str=_T("");

	//值设置
	for(int i=0; i<5; i++)
	{
		str.Format(_T("name %d"),i);
		list.InsertItem(i,str);
		str.Format(_T("属性1 %d"),i);
		list.SetItemText(i,1,str);
		str.Format(_T("属性2 %d"),i);
		list.SetItemText(i,2,str);
	}
}
VS之listview使用_第3张图片

5.背景效果

void CListViewDemoView::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
	int iRow = lplvcd->nmcd.dwItemSpec;
	switch(lplvcd->nmcd.dwDrawStage)
	{
		case CDDS_PREPAINT :
		{
			*pResult = CDRF_NOTIFYITEMDRAW;
			return;
		}
		// Modify item text and or background
		case CDDS_ITEMPREPAINT:
		{
			lplvcd->clrText = RGB(0,0,0);
			// If you want the sub items the same as the item,
			// set *pResult to CDRF_NEWFONT
			*pResult = CDRF_NOTIFYSUBITEMDRAW;
			return;
		}
		// Modify sub item text and/or background
		case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
		{
			if(iRow %2){
			 lplvcd->clrTextBk = m_colRow2;
			}
			else{
			  lplvcd->clrTextBk = m_colRow1;
			}
			
			*pResult = CDRF_DODEFAULT;
			return;
		}
	}
}
VS之listview使用_第4张图片

6.双击事件

void CListViewDemoView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
	//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<NMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	CPoint CursorPoint;
	if(!GetCursorPos(&CursorPoint))
	{
		return;
	}

	if(-1 == pNMListView->iItem)
	{
		m_nSelectItem = -1;
		return;
	}
	else
	{
		m_nSelectItem = pNMListView->iItem;
	}

	CListCtrl& list = GetListCtrl();
	CString sName = list.GetItemText(m_nSelectItem,0);
	CString str = _T("你双击了:") + sName;
	AfxMessageBox(str);

	*pResult = 0;
}
VS之listview使用_第5张图片

7.右击事件

void CListViewDemoView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<NMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	CPoint CursorPoint;
	if(!GetCursorPos(&CursorPoint))
	{
		return;
	}

	if(-1 == pNMListView->iItem)
	{
		m_nSelectItem = -1;
		return;
	}
	else
	{
		m_nSelectItem = pNMListView->iItem;
	}

	CMenu* pSubMenu = NULL;
	pSubMenu = m_menu.GetSubMenu(0);
	if(pSubMenu)
	{
		pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,CursorPoint.x,CursorPoint.y,this);
	}

	*pResult = 0;
}

void CListViewDemoView::OnMenuSub1()
{
	// TODO: 在此添加命令处理程序代码
	CString str = _T("这是菜单一");
	AfxMessageBox(str);
}

void CListViewDemoView::OnMenuSub2()
{
	// TODO: 在此添加命令处理程序代码
	CString str = _T("这是菜单二,哈哈");
	AfxMessageBox(str);
}
VS之listview使用_第6张图片  VS之listview使用_第7张图片  VS之listview使用_第8张图片

8.文档结构图

VS之listview使用_第9张图片 VS之listview使用_第10张图片

9.总代码如下

ListViewDemoView.h

// ListViewDemoView.h : CListViewDemoView 类的接口
//


#pragma once


class CListViewDemoView : public CListView
{
protected: // 仅从序列化创建
	CListViewDemoView();
	DECLARE_DYNCREATE(CListViewDemoView)

// 属性
public:
	CListViewDemoDoc* GetDocument() const;

// 操作
public:

// 重写
public:
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
	virtual void OnInitialUpdate(); // 构造后第一次调用

// 实现
public:
	virtual ~CListViewDemoView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// 生成的消息映射函数
protected:
	DECLARE_MESSAGE_MAP()

private:
	CMenu	m_menu;//菜单
	COLORREF  m_colRow1;
	COLORREF  m_colRow2;
	int m_nSelectItem;
public:
	void UpdateList(void);
	afx_msg void OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult);
	afx_msg void OnMenuSub1();
	afx_msg void OnMenuSub2();
};

#ifndef _DEBUG  // ListViewDemoView.cpp 中的调试版本
inline CListViewDemoDoc* CListViewDemoView::GetDocument() const
   { return reinterpret_cast<CListViewDemoDoc*>(m_pDocument); }
#endif

ListViewDemoView.cpp

// ListViewDemoView.cpp : CListViewDemoView 类的实现
//

#include "stdafx.h"
#include "ListViewDemo.h"

#include "ListViewDemoDoc.h"
#include "ListViewDemoView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CListViewDemoView

IMPLEMENT_DYNCREATE(CListViewDemoView, CListView)

BEGIN_MESSAGE_MAP(CListViewDemoView, CListView)
	ON_NOTIFY_REFLECT(NM_DBLCLK, &CListViewDemoView::OnNMDblclk)
	ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, &CListViewDemoView::OnNMCustomdraw)
	ON_NOTIFY_REFLECT(NM_RCLICK, &CListViewDemoView::OnNMRClick)
	ON_COMMAND(ID_MENU_SUB1, &CListViewDemoView::OnMenuSub1)
	ON_COMMAND(ID_MENU_SUB2, &CListViewDemoView::OnMenuSub2)
END_MESSAGE_MAP()

// CListViewDemoView 构造/析构

CListViewDemoView::CListViewDemoView()
{
	// TODO: 在此处添加构造代码
	m_nSelectItem = -1;

	m_colRow1  = RGB(238,254,245);
	m_colRow2  = RGB(232,242,250);
//	m_colRow1 = RGB(240,247,249);
//	m_colRow2 = RGB(229,232,239);
}

CListViewDemoView::~CListViewDemoView()
{
}

BOOL CListViewDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: 在此处通过修改
	//  CREATESTRUCT cs 来修改窗口类或样式

	return CListView::PreCreateWindow(cs);
}

void CListViewDemoView::OnInitialUpdate()
{
	CListView::OnInitialUpdate();


	// TODO: 调用 GetListCtrl() 直接访问 ListView 的列表控件,
	//  从而可以用项填充 ListView。
	//属性设置
	CListCtrl& list = GetListCtrl();
	list.ModifyStyle(LVS_TYPEMASK,LVS_REPORT & LVS_TYPEMASK | LVS_SINGLESEL);//important for show
	DWORD dwStyle = list.GetExtendedStyle();
	list.SetExtendedStyle(dwStyle | LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);

	CImageList image;
	image.Create(1,20,ILC_COLOR24 | ILC_MASK,4,0);
	list.SetImageList(&image,LVSIL_SMALL);

	CFont font;
	font.CreateFont(
		16,
		0,
		0,
		0,
		FW_NORMAL,
		FALSE,
		FALSE,
		0,
		ANSI_CHARSET,
		OUT_DEFAULT_PRECIS,
		CLIP_DEFAULT_PRECIS,
		DEFAULT_QUALITY,
		DEFAULT_PITCH | FF_SWISS,
		_T("宋体"));
	list.SetFont(&font);
	list.GetHeaderCtrl()->SetFont(&font);

	//菜单
	m_menu.LoadMenu(IDR_MENU1);
	list.SetMenu(&m_menu);

	//标题栏
	list.InsertColumn(0,_T("名称"),LVCFMT_LEFT,150,0);
	list.InsertColumn(1,_T("属性1"),LVCFMT_LEFT,120,0);
	list.InsertColumn(2,_T("属性2"),LVCFMT_LEFT,120,0);

	UpdateList();
}


// CListViewDemoView 诊断

#ifdef _DEBUG
void CListViewDemoView::AssertValid() const
{
	CListView::AssertValid();
}

void CListViewDemoView::Dump(CDumpContext& dc) const
{
	CListView::Dump(dc);
}

CListViewDemoDoc* CListViewDemoView::GetDocument() const // 非调试版本是内联的
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CListViewDemoDoc)));
	return (CListViewDemoDoc*)m_pDocument;
}
#endif //_DEBUG


// CListViewDemoView 消息处理程序

void CListViewDemoView::UpdateList(void)
{
	CListCtrl& list = GetListCtrl();
	CString str=_T("");

	//值设置
	for(int i=0; i<5; i++)
	{
		str.Format(_T("name %d"),i);
		list.InsertItem(i,str);
		str.Format(_T("属性1 %d"),i);
		list.SetItemText(i,1,str);
		str.Format(_T("属性2 %d"),i);
		list.SetItemText(i,2,str);
	}
}

void CListViewDemoView::OnNMDblclk(NMHDR *pNMHDR, LRESULT *pResult)
{
	//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<NMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	CPoint CursorPoint;
	if(!GetCursorPos(&CursorPoint))
	{
		return;
	}

	if(-1 == pNMListView->iItem)
	{
		m_nSelectItem = -1;
		return;
	}
	else
	{
		m_nSelectItem = pNMListView->iItem;
	}

	CListCtrl& list = GetListCtrl();
	CString sName = list.GetItemText(m_nSelectItem,0);
	CString str = _T("你双击了:") + sName;
	AfxMessageBox(str);

	*pResult = 0;
}

void CListViewDemoView::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
	LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	*pResult = 0;

	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
	int iRow = lplvcd->nmcd.dwItemSpec;
	switch(lplvcd->nmcd.dwDrawStage)
	{
		case CDDS_PREPAINT :
		{
			*pResult = CDRF_NOTIFYITEMDRAW;
			return;
		}
		// Modify item text and or background
		case CDDS_ITEMPREPAINT:
		{
			lplvcd->clrText = RGB(0,0,0);
			// If you want the sub items the same as the item,
			// set *pResult to CDRF_NEWFONT
			*pResult = CDRF_NOTIFYSUBITEMDRAW;
			return;
		}
		// Modify sub item text and/or background
		case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
		{
			if(iRow %2){
			 lplvcd->clrTextBk = m_colRow2;
			}
			else{
			  lplvcd->clrTextBk = m_colRow1;
			}
			
			*pResult = CDRF_DODEFAULT;
			return;
		}
	}
}

void CListViewDemoView::OnNMRClick(NMHDR *pNMHDR, LRESULT *pResult)
{
	//LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<NMITEMACTIVATE>(pNMHDR);
	// TODO: 在此添加控件通知处理程序代码
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	CPoint CursorPoint;
	if(!GetCursorPos(&CursorPoint))
	{
		return;
	}

	if(-1 == pNMListView->iItem)
	{
		m_nSelectItem = -1;
		return;
	}
	else
	{
		m_nSelectItem = pNMListView->iItem;
	}

	CMenu* pSubMenu = NULL;
	pSubMenu = m_menu.GetSubMenu(0);
	if(pSubMenu)
	{
		pSubMenu->TrackPopupMenu(TPM_LEFTALIGN,CursorPoint.x,CursorPoint.y,this);
	}

	*pResult = 0;
}

void CListViewDemoView::OnMenuSub1()
{
	// TODO: 在此添加命令处理程序代码
	CString str = _T("这是菜单一");
	AfxMessageBox(str);
}

void CListViewDemoView::OnMenuSub2()
{
	// TODO: 在此添加命令处理程序代码
	CString str = _T("这是菜单二,哈哈");
	AfxMessageBox(str);
}

好了,关于VS之listview使用就到这了。


源码下载


你可能感兴趣的:(ListView,vs)