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

📄 listctrlstylesdlg.cpp

📁 MFC窗口程序设计源代码。相信大家看得懂。
💻 CPP
字号:
// ListCtrlStylesDlg.cpp : implementation file
//
// Written by matt weagle (matt_weagle@hotmail.com)
// Copyright (c) 2000.
//
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in  this file is used in any commercial application 
// then a simple email would be nice.
//
// This file is provided "as is" with no expressed or implied warranty.
// The author accepts no liability if it causes any damage whatsoever.
// Consider yourself lucky if it works, unlucky if it doesn't.
//
// Thanks to Chris Maunder (chris@codeproject.com) for the 
// foregoing disclaimer.
// 
// Please use and enjoy. Please let me know of any bugs/mods/improvements 
// that you have found/implemented and I will fix/incorporate them into this
// file. 
//
#include "stdafx.h"
#include "..\commonctrls.h"
#include "ListCtrlStylesDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CListCtrlStylesDlg dialog


CListCtrlStylesDlg::CListCtrlStylesDlg(CListCtrl *pListCtrl /*=NULL*/,CTreeCtrl *pTreeCtrl/*=NULL*/, CWnd* pParent /*=NULL*/)
	: CDialog(CListCtrlStylesDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CListCtrlStylesDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	ASSERT(pListCtrl);
	ASSERT(pTreeCtrl);
	
	m_pListCtrl = pListCtrl;
	m_pTreeCtrl=pTreeCtrl;

}


void CListCtrlStylesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CListCtrlStylesDlg)
	DDX_Control(pDX, IDC_TREE_STYLES, m_cTreeStyles);
	DDX_Control(pDX, IDC_LIST_STYLES, m_cListStyles);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CListCtrlStylesDlg, CDialog)
	//{{AFX_MSG_MAP(CListCtrlStylesDlg)
	//}}AFX_MSG_MAP
	ON_NOTIFY( LVN_GETINFOTIP, IDC_LIST_STYLES, OnInfoTip )
	ON_NOTIFY( LVN_GETINFOTIP, IDC_TREE_STYLES, OnTreeInfoTip )
END_MESSAGE_MAP()



/////////////////////////////////////////////////////////////////////////////
// CListCtrlStylesDlg message handlers

BOOL CListCtrlStylesDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Setup the list control
	CRect rect;
	m_cListStyles.GetClientRect(&rect);
	m_cListStyles.InsertColumn(0, _T("列表控件风格"), LVCFMT_LEFT, rect.Width());

	ListView_SetExtendedListViewStyle(m_cListStyles.m_hWnd, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
	
	
	// Setup the list control
	DWORD dwStyles = m_pListCtrl->GetStyle();

	AddStyleItem( _T("LVS_ALIGNLEFT"), (dwStyles & LVS_ALIGNLEFT) != 0, LVS_ALIGNLEFT);
	AddStyleItem( _T("LVS_ALIGNTOP"), (dwStyles & LVS_ALIGNTOP) != 0, LVS_ALIGNTOP);
	AddStyleItem( _T("LVS_AUTOARRANGE"), (dwStyles & LVS_AUTOARRANGE) != 0, LVS_AUTOARRANGE);
	AddStyleItem( _T("LVS_NOCOLUMNHEADER"), (dwStyles & LVS_NOCOLUMNHEADER) != 0, LVS_NOCOLUMNHEADER);
	AddStyleItem( _T("LVS_NOLABELWRAP"), (dwStyles & LVS_NOLABELWRAP) != 0, LVS_NOLABELWRAP);
	AddStyleItem( _T("LVS_NOSCROLL"), (dwStyles & LVS_NOSCROLL) != 0, LVS_NOSCROLL);
	AddStyleItem( _T("LVS_NOSORTHEADER"), (dwStyles & LVS_NOSORTHEADER) != 0, LVS_NOSORTHEADER);
	AddStyleItem( _T("LVS_SHOWSELALWAYS"), (dwStyles & LVS_SHOWSELALWAYS) != 0, LVS_SHOWSELALWAYS);
	AddStyleItem( _T("LVS_SINGLESEL"), (dwStyles & LVS_SINGLESEL) != 0, LVS_SINGLESEL);
	AddStyleItem( _T("LVS_SORTASCENDING"), (dwStyles & LVS_SORTASCENDING) != 0, LVS_SORTASCENDING);
	AddStyleItem( _T("LVS_SORTDESCENDING"), (dwStyles & LVS_SORTDESCENDING) != 0, LVS_SORTDESCENDING);


	//TREE
	m_cTreeStyles.GetClientRect(&rect);
	m_cTreeStyles.InsertColumn(0, _T("树形控件风格"), LVCFMT_LEFT, rect.Width());
	
	ListView_SetExtendedListViewStyle(m_cTreeStyles.m_hWnd, LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT | LVS_EX_INFOTIP);
	
	
	// Setup the list control
	dwStyles = m_pTreeCtrl->GetStyle();
	
	AddTreeStyleItem( _T("TVS_HASLINES"), (dwStyles & TVS_HASLINES) != 0, TVS_HASLINES);
	AddTreeStyleItem( _T("TVS_LINESATROOT"), (dwStyles & TVS_LINESATROOT) != 0, TVS_LINESATROOT);
	AddTreeStyleItem( _T("TVS_HASBUTTONS"), (dwStyles & TVS_HASBUTTONS) != 0, TVS_HASBUTTONS);
	AddTreeStyleItem( _T("TVS_EDITLABELS"), (dwStyles & TVS_EDITLABELS) != 0,TVS_EDITLABELS);
	AddTreeStyleItem( _T("TVS_SHOWSELALWAYS"), (dwStyles &TVS_SHOWSELALWAYS) != 0,TVS_SHOWSELALWAYS);
	AddTreeStyleItem( _T("TVS_NOTOOLTIPS"), (dwStyles & TVS_NOTOOLTIPS) != 0,TVS_NOTOOLTIPS);
	AddTreeStyleItem( _T("TVS_SINGLEEXPAND"), (dwStyles &TVS_SINGLEEXPAND) != 0, TVS_SINGLEEXPAND);

	AddTreeStyleItem( _T("TVS_CHECKBOXES"), (dwStyles & TVS_CHECKBOXES) != 0, TVS_CHECKBOXES);
	AddTreeStyleItem( _T("TVS_FULLROWSELECT"), (dwStyles & TVS_FULLROWSELECT) != 0,TVS_FULLROWSELECT);
	AddTreeStyleItem( _T("TVS_TRACKSELECT"), (dwStyles &TVS_TRACKSELECT) != 0,TVS_TRACKSELECT);

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

/////////////////////////////////////////////////////////////////////////////
/*
	OnOK

	Slightly different than CListCtrlStylesDlgEx's handling of the updated
	style bits.  Whereas that class dynamically tracks the style bits, this
	one just waits until the user is done.  Then we run through the 
	entire list control items and create the new styles that should be applied
	to the list control back in the main dialog.
  
	Params
		None
  
	Returns 
		void
*/
/////////////////////////////////////////////////////////////////////////////
void CListCtrlStylesDlg::OnOK() 
{
	// Loop through the list control and get all the styles that are selected.  We
	// make this easier by storing the style value as the data member of each item.
	//List
	m_dwStyles = m_pListCtrl->GetStyle();
	int nNumItems = m_cListStyles.GetItemCount();
	for (int i = 0; i < nNumItems; i++)
	{
		if (ListView_GetCheckState(m_cListStyles.GetSafeHwnd(), i))
			m_dwStyles |= m_cListStyles.GetItemData(i);
		else	
			m_dwStyles &= ~(m_cListStyles.GetItemData(i));

	}

	//Tree
	m_dwTreeStyles = m_pTreeCtrl->GetStyle();
	nNumItems = m_cTreeStyles.GetItemCount();
	for (i = 0; i < nNumItems; i++)
	{
		if (ListView_GetCheckState(m_cTreeStyles.GetSafeHwnd(), i))
			m_dwTreeStyles |= m_cTreeStyles.GetItemData(i);
		else	
			m_dwTreeStyles &= ~(m_cTreeStyles.GetItemData(i));
		
	}

	CDialog::OnOK();
}

/////////////////////////////////////////////////////////////////////////////
/*
	AddStyleItem

	Helper function to insert the items into the list control.  Shows
	how to use CListCtrl::InsertItem without using the LVITEM 
	struct.
  
	Params
		lpszItem	Item text
		bState		Should the item have a check mark?
		dwData		Data to associate with item
	
	Returns 
		void
*/
/////////////////////////////////////////////////////////////////////////////
void CListCtrlStylesDlg::AddStyleItem(LPCTSTR lpszItem, BOOL bState, DWORD dwData)
{
	int nPos = max(m_cListStyles.GetItemCount(),0);
	nPos = m_cListStyles.InsertItem(nPos, lpszItem);
	m_cListStyles.SetItemData(nPos, dwData);

	// Use macro to set check state
	ListView_SetItemState(m_cListStyles.m_hWnd, nPos, UINT((int(bState) + 1) << 12), LVIS_STATEIMAGEMASK);

}

void CListCtrlStylesDlg::AddTreeStyleItem(LPCTSTR lpszItem, BOOL bState, DWORD dwData)
{
	int nPos = max(m_cTreeStyles.GetItemCount(),0);
	nPos = m_cTreeStyles.InsertItem(nPos, lpszItem);
	m_cTreeStyles.SetItemData(nPos, dwData);
	
	// Use macro to set check state
	ListView_SetItemState(m_cTreeStyles.m_hWnd, nPos, UINT((int(bState) + 1) << 12), LVIS_STATEIMAGEMASK);
	
}


/////////////////////////////////////////////////////////////////////////////
/*
	OnInfoTip

	This is a hand-entered handler for the LVN_GETINFOTIP WM_NOTIFY message
	which we enabled by including LVS_EX_INFOTIP in the extended style
	bits of the list control.  When this style bit is enabled, it sends 
	the parent window a LVN_GETINFOTIP message with a pointer to a 
	LVGETINFOTIP struct.  We populate the pszParam of this struct with 
	documentation for the item selected.  A more robust implementation might 
	associate the data member of each list control item with a string 
	allocated on the heap or some user defined struct.
  
	Params
		<Standard>
	
	Returns 
		void
*/
/////////////////////////////////////////////////////////////////////////////
void CListCtrlStylesDlg::OnInfoTip( NMHDR * pNMHDR, LRESULT * pResult )
{
	NMLVGETINFOTIP*	pInfoTip = reinterpret_cast<NMLVGETINFOTIP*>(pNMHDR);
	ASSERT(pInfoTip);

	// Get the item data for this unit
	DWORD dwData = m_cListStyles.GetItemData(pInfoTip->iItem);
	

	// Based on the flag value, show a tool tip with the information about this style bit
	CString strText;
	switch (dwData)
	{
	case LVS_ALIGNLEFT:
		strText = _T("Specifies that items are left-aligned in icon and small icon view."); break;
	case LVS_ALIGNTOP:
		strText = _T("Specifies that items are aligned with the top of the control in icon and small icon view."); break;
	case LVS_AUTOARRANGE:
		strText = _T("Specifies that icons are automatically kept arranged in icon view and small icon view."); break;
	case LVS_NOCOLUMNHEADER:
		strText = _T("Specifies that a column header is not displayed in report view."); break;
	case LVS_NOLABELWRAP:
		strText = _T("Displays item text on a single line in icon view."); break;
	case LVS_NOSORTHEADER:
		strText = _T("Specifies that column headers do not work like buttons.");break;
	case LVS_SHOWSELALWAYS:
		strText = _T("Always show the selection, if any, even if the control does not have the focus."); break;
	case LVS_SINGLESEL:
		strText = _T("Allows only one item at a time to be selected."); break;
	case LVS_SORTASCENDING:
		strText = _T("Sorts items based on item text in ascending order."); break;
	case LVS_SORTDESCENDING:
		strText = _T("Sorts items based on item text in descending order."); break;	
	default:
		strText = _T("Unknown style option."); break;

	}
	_tcscpy(pInfoTip->pszText, (LPCTSTR)strText);

}

void CListCtrlStylesDlg::OnTreeInfoTip( NMHDR * pNMHDR, LRESULT * pResult )
{
	NMLVGETINFOTIP*	pInfoTip = reinterpret_cast<NMLVGETINFOTIP*>(pNMHDR);
	ASSERT(pInfoTip);
	
	// Get the item data for this unit
	DWORD dwData = m_cTreeStyles.GetItemData(pInfoTip->iItem);
	
	
	// Based on the flag value, show a tool tip with the information about this style bit
	CString strText;
	switch (dwData)
	{
	case TVS_HASLINES:
		strText = _T("The tree view control has lines linking child items to their corresponding parent items."); break;
	case TVS_LINESATROOT:
		strText = _T("The tree view control has lines linking child items to the root of the hierarchy."); break;
	case TVS_HASBUTTONS:
		strText = _T("The tree view control adds a button to the left of each parent item."); break;
	case TVS_EDITLABELS:
		strText = _T("The tree view control allows the user to edit the labels of tree view items."); break;
	case TVS_SHOWSELALWAYS:
		strText = _T("Causes a selected item to remain selected when the tree-view control loses focus."); break;
	case TVS_NOTOOLTIPS:
		strText = _T("The tree view control uses no tooltips.");break;
	case TVS_SINGLEEXPAND:
		strText = _T("When this style is enabled, changing the selection in the tree view will automatically cause the item being selected to expand and the item being unselected to collapse. ");break;
	//
	case TVS_CHECKBOXES:
		strText = _T("Version 4.70. Enables check boxes for items in a tree view control. "); break;
	case TVS_FULLROWSELECT:
		strText = _T("Version 4.71. Enables full-row selection in the tree view."); break;
	case TVS_TRACKSELECT:
		strText = _T("Version 4.70. Enables hot tracking in a tree view control.");break;
	default:
		strText = _T("Unknown style option."); break;
		
	}
	_tcscpy(pInfoTip->pszText, (LPCTSTR)strText);
	
}

⌨️ 快捷键说明

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