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

📄 treelistpage.cpp

📁 MFC窗口程序设计源代码。相信大家看得懂。
💻 CPP
字号:
// TreeListPage.cpp : implementation file
//

#include "stdafx.h"
#include "..\commonctrls.h"
#include "TreeListPage.h"
#include "..\TreeList\ListCtrlStylesDlg.h"
#include "..\TreeList\ListCtrlStylesExDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTreeListPage property page

IMPLEMENT_DYNCREATE(CTreeListPage, CPropertyPage)

CTreeListPage::CTreeListPage() : CPropertyPage(CTreeListPage::IDD)
{
	//{{AFX_DATA_INIT(CTreeListPage)
	//}}AFX_DATA_INIT
}

CTreeListPage::~CTreeListPage()
{
}

void CTreeListPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTreeListPage)
	DDX_Control(pDX, IDC_TREE1, m_cTreeCtrl);
	DDX_Control(pDX, IDC_LIST_CTRL_MODE, m_cTabListMode);
	DDX_Control(pDX, IDC_LIST_CTRL, m_cListCtrl);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CTreeListPage, CPropertyPage)
	//{{AFX_MSG_MAP(CTreeListPage)
	ON_BN_CLICKED(IDC_STYLES, OnStyles)
	ON_BN_CLICKED(IDC_STYLES_EX, OnStylesEx)
	ON_NOTIFY(TCN_SELCHANGE, IDC_LIST_CTRL_MODE, OnSelchangeListCtrlMode)
	ON_NOTIFY(LVN_ITEMCHANGED, IDC_LIST_CTRL, OnItemchangedListCtrl)
	ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTreeListPage message handlers

BOOL CTreeListPage::OnInitDialog() 
{
	CPropertyPage::OnInitDialog();
	
	// TODO: Add extra initialization here
	// Initial extended style for the list control on this dialog
	DWORD dwStyle = m_cListCtrl.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	m_cListCtrl.SetExtendedStyle(dwStyle);
	
	// Create the image list that is attached to the list control
	InitImageList();
	// Setup the tab header
	InitTabCtrl();
	// Setup the column headings
	InitTreeListCtrlCols();
	//Insert the default dummy items
	InsertItems();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

BOOL CTreeListPage::InitImageList()
{
	// Create 256 color image lists
	HIMAGELIST hList = ImageList_Create(32,32, ILC_COLOR8 |ILC_MASK , 8, 1);
	m_cImageListNormal.Attach(hList);
	
	hList = ImageList_Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 1);
	m_cImageListSmall.Attach(hList);
	
	
	// Load the large icons
	CBitmap cBmp;
	cBmp.LoadBitmap(IDB_IMAGES_NORMAL);
	m_cImageListNormal.Add(&cBmp, RGB(255,0, 255));
	cBmp.DeleteObject();
	
	// Load the small icons
	cBmp.LoadBitmap(IDB_BITMAP_DEFAULT);
	m_cImageListSmall.Add(&cBmp, RGB(255,0, 255));
	
	// Attach them
	m_cListCtrl.SetImageList(&m_cImageListNormal, LVSIL_NORMAL);
	m_cListCtrl.SetImageList(&m_cImageListSmall, LVSIL_SMALL);

	//TRee
	m_cTreeCtrl.SetImageList(&m_cImageListNormal, TVSIL_NORMAL);
	
	return TRUE;

}

void CTreeListPage::InitTreeListCtrlCols()
{
	// Insert some columns
	CRect rect;
	m_cListCtrl.GetClientRect(&rect);
	int nColInterval = rect.Width()/5;
	
	m_cListCtrl.InsertColumn(0, _T("项名"), LVCFMT_LEFT, nColInterval*3);
	m_cListCtrl.InsertColumn(1, _T("项值"), LVCFMT_LEFT, nColInterval);
	m_cListCtrl.InsertColumn(2, _T("时间"), LVCFMT_LEFT, rect.Width()-4*nColInterval);

/*	//TRee
	HTREEITEM hInsertItem;
	for(int i=0;i<m_cImageListNormal.GetImageCount()/8;i++)
	{
		hInsertItem=m_cTreeCtrl.InsertItem("根项一",0,1);
		m_cTreeCtrl.InsertItem("子项一",2,3,hInsertItem);
		hInsertItem=m_cTreeCtrl.InsertItem("根项二3",4,5);
		m_cTreeCtrl.InsertItem("子项二",6,7,hInsertItem);
	}NM_LISTVIEW
	*/
}

void CTreeListPage::InitTabCtrl()
{
	m_cTabListMode.InsertItem(0, _T("图标"));
	m_cTabListMode.InsertItem(1, _T("小图标"));
	m_cTabListMode.InsertItem(2, _T("列表"));
	m_cTabListMode.InsertItem(3, _T("报表"));
	m_cTabListMode.SetCurSel(3);

	CRect rec;
	m_cListCtrl.GetWindowRect(&rec);
	m_cTabListMode.SetMinTabWidth((int)rec.Width()/4);
}

void CTreeListPage::InsertItems()
{
	//删除所有项
	m_cListCtrl.DeleteAllItems();
	//使用LV_ITEM结构插入100项
	LVITEM lvi;
	CString strItem;
	for (int i = 0; i < 100; i++)
	{
		//插入第一项
		lvi.mask =  LVIF_IMAGE | LVIF_TEXT;
		strItem.Format(_T("Item %i"), i);
		
		lvi.iItem = i;
		lvi.iSubItem = 0;
		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
		//图像列表中有8个图像
		lvi.iImage = i%8;		
		m_cListCtrl.InsertItem(&lvi);
		
		//设置子项1
		strItem.Format(_T("%d"), 10*i);
		lvi.iSubItem =1;
		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
		m_cListCtrl.SetItem(&lvi);
		
		//设置子项2
		strItem.Format(_T("%s"), COleDateTime::GetCurrentTime().Format(_T("创建于: %I:%M:%S %p, %m/%d/%Y")));
		lvi.iSubItem =2;
		lvi.pszText = (LPTSTR)(LPCTSTR)(strItem);
		m_cListCtrl.SetItem(&lvi);
		
	}
	//TRee
	//删除所有内容
	m_cTreeCtrl.DeleteAllItems();
	//TRee
	HTREEITEM hRootItem;
	TV_ITEM tvItem;
	TV_INSERTSTRUCT tvIns;
	tvItem.mask = TVIF_TEXT | TVIF_PARAM | TVIF_IMAGE|TVIF_SELECTEDIMAGE;
	tvItem.pszText ="树根";		
	tvItem.iImage = 3;
	tvItem.iSelectedImage = 7;
	
	tvIns.item = tvItem;
	tvIns.hInsertAfter = TVI_ROOT;
	tvIns.hParent = TVI_ROOT;
	hRootItem = m_cTreeCtrl.InsertItem(&tvIns);

	HTREEITEM hInsertItem;
	for(i=0;i<m_cImageListNormal.GetImageCount()/8;i++)
	{
		hInsertItem=m_cTreeCtrl.InsertItem("根项一",0,1,hRootItem);
		m_cTreeCtrl.InsertItem("子项一",2,3,hInsertItem);
		hInsertItem=m_cTreeCtrl.InsertItem("根项二",4,5,hRootItem);
		m_cTreeCtrl.InsertItem("子项二",6,7,hInsertItem);
	}
	m_cTreeCtrl.Expand(hRootItem,TVE_EXPAND);
}

void CTreeListPage::OnStyles() 
{
	// TODO: Add your control notification handler code here
	// TODO: Add your control notification handler code here
	CListCtrlStylesDlg dlg(&m_cListCtrl,&m_cTreeCtrl);
	if (dlg.DoModal() != IDOK) //NM_TREEVIEW
		return;
	
	
	// Just recreate the control with the new styles, since some styles can't be
	// dynamically reset
	RecreateTreeListControl(dlg.GetStyles(), dlg.GetTreeStyles(),m_cListCtrl.GetExtendedStyle());
}

void CTreeListPage::OnStylesEx() 
{
	// TODO: Add your control notification handler code here
	CListCtrlStylesExDlg dlg(&m_cListCtrl,&m_cTreeCtrl);
	
	if (dlg.DoModal() != IDOK)
		return;
	
	// Just recreate the control with the new styles, since some styles can't be
	// dynamically reset
	RecreateTreeListControl(m_cListCtrl.GetStyle(),m_cTreeCtrl.GetStyle(), dlg.GetStyles());
}

void CTreeListPage::RecreateTreeListControl(const DWORD &dwStyles, const DWORD &dwTreeStyles,const DWORD &dwStylesEx)
{
	CRect rect;
	m_cListCtrl.GetWindowRect(&rect);
	ScreenToClient(&rect);
	m_cListCtrl.DestroyWindow();
	m_cListCtrl.Create(dwStyles, rect, this, IDC_LIST_CTRL);
	
	// CListCtrl::SetExtendedStyle works differently for LVS_EX_XXX styles than
	// CWnd::ModifyStyleEx works for CWnd extended styles.  It sends a LVM_SETEXTENDEDLISTVIEWSTYLE
	// message to the underlying CListCtrl, whereas ModifyStyleEx resolves to _AfxModifyStyle, 
	// which turns into ::SetWindowLong(GWL_EXSTYLE).  These calls don't seem to give the
	// same results.  For extended styles, always use CListCtrl::SetExtendedStyle or 
	// ListView_SetExtendedListViewStyle
	m_cListCtrl.SetExtendedStyle( dwStylesEx);
	
	// For standard CWnd extended styles, you can use this:
	m_cListCtrl.ModifyStyleEx(0, WS_EX_CLIENTEDGE);
	
	// Re attach the image lists
	m_cListCtrl.SetImageList(&m_cImageListNormal, LVSIL_NORMAL);
	m_cListCtrl.SetImageList(&m_cImageListSmall, LVSIL_SMALL);


	//TRee
/*	m_cTreeCtrl.GetWindowRect(&rect);
	ScreenToClient(&rect);
	m_cTreeCtrl.DestroyWindow();
	m_cTreeCtrl.Create(dwTreeStyles, rect, this, IDC_TREE1);*/
	m_cTreeCtrl.ModifyStyle(0,dwTreeStyles);
	
	// Re attach the image lists
//	m_cTreeCtrl.SetImageList(&m_cImageListNormal, TVSIL_NORMAL);
	
	// Re create the columns for report mode 
	InitTreeListCtrlCols();
	
	// Insert the items again
	InsertItems(); // repopulate with a new item group
}

void CTreeListPage::OnSelchangeListCtrlMode(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	// 在设置期间,不允许重绘
	m_cListCtrl.SetRedraw(FALSE);
	//首先去除所有的风格
	m_cListCtrl.ModifyStyle(LVS_ICON | LVS_LIST | LVS_REPORT | LVS_SMALLICON ,0);
	
	//切换标签模式
	switch (m_cTabListMode.GetCurSel())
	{
	case 0: //图标模式
		m_cListCtrl.ModifyStyle(0, LVS_ICON);
		break;
	case 1:	//小图标模式
		m_cListCtrl.ModifyStyle(0, LVS_SMALLICON);
		break;
	case 2: //列表模式
		m_cListCtrl.ModifyStyle(0, LVS_LIST);
		break;
	case 3:	//报表模式
		m_cListCtrl.ModifyStyle(0, LVS_REPORT);
		
		break;
	}
	//填充列表控件
	InsertItems();
	//设置完毕,打开重绘标志
	m_cListCtrl.SetRedraw(TRUE);
	*pResult = 0;
}


void CTreeListPage::OnItemchangedListCtrl(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(pNMListView->uNewState&LVIS_SELECTED)
		AfxMessageBox(m_cListCtrl.GetItemText(pNMListView->iItem,0));
	*pResult = 0;
}


void CTreeListPage::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(pNMTreeView->itemNew.state&TVIS_SELECTED)
		AfxMessageBox(m_cTreeCtrl.GetItemText(pNMTreeView->itemNew.hItem));
	*pResult = 0;
}

⌨️ 快捷键说明

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