ttreectrl.cpp

来自「AUTOCAD 程序员使用的」· C++ 代码 · 共 225 行

CPP
225
字号
//-----------------------------------------------------------------------------
//----- By Cyrille Fauvel
//----- Developer Consulting Group
//----- Autodesk
//----- Mai 25th, 1997
//-----------------------------------------------------------------------------
#include "StdAfx.h"
#include "StdArx.h"
#include "resource.h"

#include "Adesk.h"
#include "dbgroup.h"
#include "dbmain.h"
#include "dbsymtb.h"

#include "rxmfcapi.h"

#include "TTreeCtrl.h"

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

//-----------------------------------------------------------------------------
BEGIN_MESSAGE_MAP(TTreeCtrl, CTreeCtrl)
	//{{AFX_MSG_MAP(TTreeCtrl)
	ON_WM_NCHITTEST()
	ON_WM_RBUTTONDOWN()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	ON_COMMAND (ID_AUTOSCROLL, OnAutoScroll)
END_MESSAGE_MAP()

//-----------------------------------------------------------------------------
TTreeCtrl::TTreeCtrl () {
	mbEnableMenu =TRUE ;
	mbAutoScroll =FALSE ;
}

TTreeCtrl::~TTreeCtrl () {
}

//-----------------------------------------------------------------------------
HTREEITEM TTreeCtrl::GetRootParentItem (HTREEITEM hItem) {
	HTREEITEM hParent ;
	while ( (hParent =GetParentItem (hItem)) != NULL )
		hItem =hParent ;
	return (hItem) ;
}

//-----------------------------------------------------------------------------
UINT TTreeCtrl::OnNcHitTest (CPoint point) {
	if ( mbAutoScroll ) {
		CPoint pt =point ;
		ScreenToClient (&pt) ;

		CRect rectClient, rectLimit ;
		GetClientRect (rectClient) ;
		rectLimit =rectClient ;
		rectLimit.DeflateRect (14, 14) ;

		if ( rectClient.PtInRect (pt) && !rectLimit.PtInRect (pt) ) {
			if ( pt.y < rectLimit.top )
				SendMessage (WM_VSCROLL, SB_LINEUP, NULL) ;
			else if ( pt.y > rectLimit.bottom )
				SendMessage (WM_VSCROLL, SB_LINEDOWN, NULL) ;
			else if ( pt.x < rectLimit.left )
				SendMessage (WM_HSCROLL, SB_LINELEFT, NULL) ;
			else if ( pt.x > rectLimit.right )
				SendMessage (WM_HSCROLL, SB_LINERIGHT, NULL) ;
		}
	}
	return (CTreeCtrl::OnNcHitTest (point));
}

//-----------------------------------------------------------------------------
void TTreeCtrl::OnRButtonDown (UINT nFlags, CPoint point) {
	if ( mbEnableMenu ) {
		CMenu menu ;
		menu.CreatePopupMenu () ;
		menu.AppendMenu (MF_STRING | (mbAutoScroll ? MF_CHECKED : MF_UNCHECKED), ID_AUTOSCROLL, _T("AutoScroll")) ;

		POINT pt ;
		::GetCursorPos (&pt) ;
		menu.TrackPopupMenu (TPM_LEFTALIGN, pt.x, pt.y, this, NULL) ;
		menu.DestroyMenu () ;
	} else {
		CTreeCtrl::OnRButtonDown (nFlags, point) ;
	}
}

//-----------------------------------------------------------------------------
void TTreeCtrl::OnAutoScroll () {
	mbAutoScroll =!mbAutoScroll ;
}

//-----------------------------------------------------------------------------
//----- Tree Control Colored Item Support
void TTreeCtrl::SetItemFont (HTREEITEM hItem, LOGFONT &logfont) {
	Color_Font cf ;
	if ( !mMapColorFont.Lookup (hItem, cf) )
		cf.color =(COLORREF)-1 ;
	cf.logfont =logfont ;
	mMapColorFont [hItem] =cf ;
}

void TTreeCtrl::SetItemColor (HTREEITEM hItem, COLORREF color) {
	Color_Font cf ;
	if ( !mMapColorFont.Lookup (hItem, cf) )
		cf.logfont.lfFaceName [0] ='\0' ;
	cf.color =color ;
	mMapColorFont [hItem] =cf ;
}

BOOL TTreeCtrl::GetItemFont (HTREEITEM hItem, LOGFONT *plogfont) {
	Color_Font cf ;
	if ( !mMapColorFont.Lookup (hItem, cf) )
		return (FALSE) ;
	if ( cf.logfont.lfFaceName [0] == '\0' ) 
		return (FALSE) ;
	*plogfont =cf.logfont ;
	return (TRUE) ;
}

COLORREF TTreeCtrl::GetItemColor (HTREEITEM hItem) {
	//----- Returns (COLORREF)-1 if color was not set
	Color_Font cf ;
	if ( !mMapColorFont.Lookup (hItem, cf) )
			return ((COLORREF)-1) ;
	return (cf.color) ;
}

void TTreeCtrl::ResetItemProperty (HTREEITEM hItem) {
	mMapColorFont.RemoveKey (hItem) ;
}

void TTreeCtrl::OnPaint () {
	CPaintDC dc (this) ;

	//----- Create a memory DC compatible with the paint DC
	CDC memDC ;
	memDC.CreateCompatibleDC (&dc) ;

	CRect rcClip, rcClient ;
	dc.GetClipBox (&rcClip) ;
	GetClientRect (&rcClient) ;

	//----- Select a compatible bitmap into the memory DC
	CBitmap bitmap ;
	bitmap.CreateCompatibleBitmap (&dc, rcClient.Width (), rcClient.Height ()) ;
	memDC.SelectObject (&bitmap) ;

	//----- Set clip region to be same as that in paint DC
	CRgn rgn ;
	rgn.CreateRectRgnIndirect (&rcClip) ;
	memDC.SelectClipRgn (&rgn) ;
	rgn.DeleteObject () ;

	//----- First let the control do its default drawing.
	CWnd::DefWindowProc (WM_PAINT, (WPARAM)memDC.m_hDC, 0) ;

	HTREEITEM hItem =GetFirstVisibleItem () ;

	int n =GetVisibleCount () + 1 ;
	while ( hItem && n-- ) {
		CRect rect ;

		//----- Do not meddle with selected items or drop highlighted items
		UINT selflag =TVIS_DROPHILITED | TVIS_SELECTED ;
		Color_Font cf ;

		if ( !(GetItemState (hItem, selflag) & selflag ) && mMapColorFont.Lookup (hItem, cf) ) {
			CFont *pFontDC;
			CFont fontDC ;
			LOGFONT logfont ;

			if ( cf.logfont.lfFaceName[0] != '\0' ) {
				logfont =cf.logfont ;
			} else {
				//----- No font specified, so use window font
				CFont *pFont =GetFont () ;
				pFont->GetLogFont (&logfont) ;
			}

			if ( GetItemState (hItem, TVIS_BOLD) & TVIS_BOLD )
				logfont.lfWeight =700 ;
			fontDC.CreateFontIndirect (&logfont) ;
			pFontDC =memDC.SelectObject (&fontDC) ;

			if ( cf.color != (COLORREF)-1 )
				memDC.SetTextColor (cf.color) ;

			CString sItem =GetItemText (hItem) ;

			GetItemRect (hItem, &rect, TRUE) ;
			memDC.SetBkColor (GetSysColor (COLOR_WINDOW)) ;
			memDC.TextOut (rect.left+2, rect.top+1, sItem) ;

			memDC.SelectObject (pFontDC) ;
		}
		hItem =GetNextVisibleItem (hItem) ;
	}
	dc.BitBlt (rcClip.left, rcClip.top, rcClip.Width (), rcClip.Height (), &memDC, rcClip.left, rcClip.top, SRCCOPY) ;
}

//-----------------------------------------------------------------------------
void TTreeCtrl::ApplyToAll (HTREEITEM hItem, TTREECTRLCB fct) {
	ASSERT ( fct == NULL ) ;

	if ( !hItem )
		hItem =GetRootItem () ;
	(*fct) (this, hItem) ;

	if ( ItemHasChildren (hItem) ) {
		HTREEITEM hChild =GetNextItem (hItem, TVGN_CHILD) ;
		while ( hChild ) {
			ApplyToAll (hChild, fct) ;
			hChild =GetNextItem (hChild, TVGN_NEXT) ;
		}
	}
}

⌨️ 快捷键说明

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