⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listctrldlg.cpp

📁 《VC++ 编程技巧与示例 .rar》各个示例代码绝对可用
💻 CPP
字号:
// ListCtrlDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ControlTest.h"
#include "ListCtrlDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListCtrlDlg dialog


CListCtrlDlg::CListCtrlDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CListCtrlDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CListCtrlDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_ImageAdded=FALSE;
	m_ImageList=new CImageList;
	m_ImageListSmall=new CImageList;
}

CListCtrlDlg::~CListCtrlDlg()
{
	if(m_ImageList)
	{
		delete m_ImageList;
	}
	if(m_ImageListSmall)
	{
		delete m_ImageListSmall;
	}
}

void CListCtrlDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CListCtrlDlg)
	DDX_Control(pDX, IDC_LIST, m_ListCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
	//{{AFX_MSG_MAP(CListCtrlDlg)
	ON_BN_CLICKED(IDC_MODE_ICON, OnModeIcon)
	ON_BN_CLICKED(IDC_MODE_LIST, OnModeList)
	ON_BN_CLICKED(IDC_MODE_REPORT, OnModeReport)
	ON_BN_CLICKED(IDC_MODE_SMALL, OnModeSmall)
	ON_NOTIFY(NM_DBLCLK, IDC_LIST, OnDblclkList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CListCtrlDlg message handlers

BOOL CListCtrlDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	CString str;
//0. 设置背景颜色:
	m_ListCtrl.SetTextBkColor(RGB(255,255,255));
	m_ListCtrl.SetBkColor(RGB(0,0,255));
//1. 创建图象列表:
	if(!m_ImageAdded)
	{
		CBitmap bmp,sbmp;
		bmp.LoadBitmap(IDB_LISTDRAFT);
		sbmp.LoadBitmap(IDB_SLISTDRAFT);
		m_ImageList->Create(32,32,FALSE,1,0);
		m_ImageList->Add(&bmp,RGB(0,0,0));
		m_ImageListSmall->Create(16,16,FALSE,1,0);
		m_ImageListSmall->Add(&sbmp,RGB(0,0,0));
		m_ImageAdded=TRUE;
	}
	m_ListCtrl.SetImageList(m_ImageList,LVSIL_NORMAL);
	m_ListCtrl.SetImageList(m_ImageListSmall,LVSIL_SMALL);

//2. 设置列表控件属性:
    SetWindowLong(m_ListCtrl.m_hWnd,
				  GWL_STYLE,
				  WS_VISIBLE | WS_CHILD | WS_BORDER |
				  LVS_ICON | LVS_EDITLABELS
				  );
	CButton * btn;
	btn=(CButton *)GetDlgItem(IDC_MODE_ICON);
	btn->SetCheck(1);
//3. 添加列:
	LV_COLUMN lc;
	lc.mask = LVCF_FMT | LVCF_WIDTH |
		      LVCF_TEXT | LVCF_SUBITEM;
	lc.fmt = LVCFMT_CENTER;
	lc.cx = 75;
	lc.iSubItem = 0;
	lc.pszText = "Column 0";
	m_ListCtrl.InsertColumn(0, &lc);
	
	lc.iSubItem = 1;
	lc.pszText = "Column 1";
	m_ListCtrl.InsertColumn(1, &lc);

	lc.iSubItem = 2;
	lc.pszText = "Column 2";
	m_ListCtrl.InsertColumn(2, &lc);

//4. 添加各个项目(各行):
	LVITEM Item[6];
	UINT i,j;
	for(i=0;i<6;i++)
	{
		Item[i].mask=LVIF_IMAGE|LVIF_TEXT;
		Item[i].iItem=i;
		Item[i].iSubItem=0;
		Item[i].iImage=i;
	}
	str="姓名:"+m_Name;
	Item[0].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[0]);
	str="年龄:"+m_Age;
	Item[1].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[1]);

	str="性别:"+m_Male;
	Item[2].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[2]);

	str="生日:"+m_Date.Format ("%A, %B %d, %Y");
	Item[3].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[3]);

	str="职业:"+m_Career;
	Item[4].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[4]);

	str="爱好:"+m_treeitem;
	Item[5].pszText=str.GetBuffer(str.GetLength());
	m_ListCtrl.InsertItem(&Item[5]);


//5. 设置各个子项的字符串:
	for(i=0;i<5;i++)
	{
		for(j=1;j<3;j++)
		{
			str.Format("第 %d 行,第 %d 列",i,j);
			m_ListCtrl.SetItemText(i,j,str);
		}
	}
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CListCtrlDlg::OnModeIcon() 
{
	// TODO: Add your control notification handler code here
    SetWindowLong(m_ListCtrl.m_hWnd,
				  GWL_STYLE,
				  WS_VISIBLE | WS_CHILD | WS_BORDER |
				  LVS_ICON | LVS_EDITLABELS
				  );
}

void CListCtrlDlg::OnModeList() 
{
	// TODO: Add your control notification handler code here
    SetWindowLong(m_ListCtrl.m_hWnd,
				  GWL_STYLE,
				  WS_VISIBLE | WS_CHILD | WS_BORDER |
				  LVS_LIST | LVS_EDITLABELS
				  );
}

void CListCtrlDlg::OnModeReport() 
{
	// TODO: Add your control notification handler code here
    SetWindowLong(m_ListCtrl.m_hWnd,
				  GWL_STYLE,
				  WS_VISIBLE | WS_CHILD | WS_BORDER |
				  LVS_REPORT | LVS_EDITLABELS
				  );
}


void CListCtrlDlg::OnModeSmall() 
{
	// TODO: Add your control notification handler code here
    SetWindowLong(m_ListCtrl.m_hWnd,
				  GWL_STYLE,
				  WS_VISIBLE | WS_CHILD | WS_BORDER |
				  LVS_SMALLICON | LVS_EDITLABELS
				  );
}

void CListCtrlDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -