📄 rtreview.cpp
字号:
////////////////////////////////////////////////////////////////////////////////
// RTreView.cpp : implementation of the CRTreView class
// $Header: /USB/Util/EzMr/RTreView.cpp 3 8/08/00 2:26p Tpm $
// Copyright (c) 2000 Cypress Semiconductor. May not be reproduced without permission.
// See the EzUsb Developer's Kit license agreement for more details.
////////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "EzMr.h"
#include "RTreDoc.h"
#include "RTreView.h"
//#include "ChooseLPARAMDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//////////
// CRTreView
IMPLEMENT_DYNCREATE(CRTreView, CTreeView)
BEGIN_MESSAGE_MAP(CRTreView, CTreeView)
//{{AFX_MSG_MAP(CRTreView)
ON_WM_CREATE()
ON_WM_RBUTTONDOWN()
ON_WM_CONTEXTMENU()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//////////
// Constructor
//
CRTreView::CRTreView()
: m_bShowHandlesAndLParams(FALSE)
{
m_pOldSel = 0;
}
//////////
// Destructor
//
CRTreView::~CRTreView()
{
}
//////////
// GetItemHandle() is a public member function which provides an operation
// which is not available in the TreeView control or any of the MFC tree
// classes (CTreeView, CTreeCtrl). It returns the HTREEITEM handle for
// the item you seek, when you provide the LPARAM value (TV_ITEM.lParam)
// which was specified for that item. A user specfied LPARAM value
// is often a pointer to the software representation of the visual element
// on a control window. The startHandle argument is an optional arguement
// which indicates that the search should begin at a specific "sub-root"
// in the tree control. If not specified by the user, the startHandle
// value will be TVI_ROOT which is treated by the control as a flag
// specifying the top-level item.
//
HTREEITEM CRTreView::GetItemHandle (LPARAM lParam,
HTREEITEM startHandle)
{
ASSERT(lParam);
SaveItemHandleUserParm_ userParm;
userParm.m_lParam = lParam;
userParm.m_hTreeItem = NULL;
ApplyIfTrue_ ( CRTreView::ItemHasLparam_,
CRTreView::SaveItemHandle_,
reinterpret_cast<DWORD>( & userParm ),
startHandle);
return userParm.m_hTreeItem;
}
//////////
// ApplyIfTrue() recurses the tree calling the predicate function for
// each item in the tree. If the predicate function returns TRUE,
// then the apply function is called. If the apply function returns
// TRUE, then the recursion continues. If the apply function returns
// FALSE, then the recursion ends.
//
BOOL CRTreView::ApplyIfTrue_ (TreePredicateFunction_ predicateFunction,
TreeApplyFunction_ applyFunction,
DWORD userParm,
HTREEITEM startHandle,
BOOL recurseRootSiblings)
{
// If startHandle is null, this is either a recursive base-case
// indicating there are no more items below the current HTREEITEM,
// or it indicates that the user called this function with a NULL
// value as the start handle. The default value for startHandle is
// TVI_ROOT whose value is considered by the TreeView control to
// be a flag indicating the root item.
if ( startHandle == NULL )
return FALSE;
// Get the starting location of the recursion.
// The starting location defaults to the absolute root.
HTREEITEM candidate;
if ( startHandle == TVI_ROOT )
{
candidate = GetTreeCtrl().GetRootItem ();
// If there are no items in the tree, then get out.
if (candidate == NULL)
return FALSE;
}
else
candidate = startHandle;
// Call the predicate function on the starting candidate.
// If the predicate returns TRUE, then call the apply
// function. If the apply function returns FALSE, it
// indicates that no more calls to the apply function
// should occur.
if ( (this->*predicateFunction)(candidate, userParm ) == TRUE )
if ( (this->*applyFunction)(candidate, userParm ) == FALSE )
return FALSE;
// Process each child of the candidate item. If that child
// also has children, process each of those children.
if ( GetTreeCtrl().ItemHasChildren( candidate ) )
{
// Get the first child's handle...
HTREEITEM child = GetTreeCtrl().GetChildItem ( candidate );
while (child)
{
// ... and recurse each child item.
if ( ApplyIfTrue_
(predicateFunction, applyFunction, userParm, child )
== FALSE)
return FALSE;
// Get the next sibling of the child just recursed.
child = GetTreeCtrl().GetNextSiblingItem ( child );
}
// Process siblings of the highest root
if (recurseRootSiblings)
candidate = GetTreeCtrl().GetNextSiblingItem(candidate);
else
candidate = NULL;
}
return TRUE;
}
//////////
// TruePredicate_() is a predicate function for use by ApplyIfTrue().
// It is used when an "apply" function is to be called for each item.
//
BOOL CRTreView::TruePredicate_(HTREEITEM candidate, DWORD userParm)
{
ASSERT(candidate);
return TRUE; // Call the apply function.
}
//////////
// ItemHasText_() is a predicate function for ApplyIfTrue_().
// It determines whether the item in the TreeView has text
// which equals some user specified value.
//
BOOL CRTreView::ItemHasText_(HTREEITEM candidate, DWORD userParm)
{
ASSERT(candidate && userParm);
return (reinterpret_cast<SaveItemHandleUserParm_*>(userParm)->m_text ==
GetTreeCtrl().GetItemText(candidate)) == 0;
}
//////////
// ItemHasLparam_() is a predicate function for ApplyIfTrue_().
// It determines whether the item in the TreeView has an LPARAM
// value which equals some user specified value.
//
BOOL CRTreView::ItemHasLparam_(HTREEITEM candidate, DWORD userParm)
{
ASSERT(candidate && userParm);
return reinterpret_cast<SaveItemHandleUserParm_*>(userParm)->m_lParam ==
LPARAM(GetTreeCtrl().GetItemData(candidate));
}
//////////
// SaveItemHandle_() is an apply function for ApplyIfTrue_().
// It saves the LPARAM for the candidate item in the user specified location.
//
BOOL CRTreView::SaveItemHandle_(HTREEITEM candidate, DWORD userParm)
{
ASSERT(candidate && userParm);
reinterpret_cast<SaveItemHandleUserParm_*>(userParm)->m_hTreeItem
= candidate;
return FALSE; // done
}
//////////
// SaveItemsLparams_() is an apply function for ApplyIfTrue_().
// It saves the LPARAM for the candidate item in the user specified array.
//
BOOL CRTreView::SaveItemsLParams_(HTREEITEM candidate, DWORD userParm)
{
ASSERT(candidate && userParm);
CDWordArray* arrayOfTreeViewLParams =
reinterpret_cast<CDWordArray*>(userParm);
DWORD data = GetTreeCtrl().GetItemData(candidate);
ASSERT(data);
arrayOfTreeViewLParams->Add(data);
return TRUE; // keep going...
}
//////////
// ShowHandleAndLParam() is an apply function for ApplyIfTrue_(). It shows
// the HTREEITEM handle and TVI_ITEM.lParam value for an item.
//
BOOL CRTreView::ShowHandleAndLParam_(HTREEITEM candidate, DWORD userParm)
{
TV_ITEM item;
TCHAR pszText[255];
item.mask = TVIF_HANDLE | TVIF_TEXT | TVIF_PARAM;
item.cchTextMax = sizeof pszText;
item.pszText = pszText;
item.hItem = candidate;
CString text;
GetTreeCtrl().GetItem(&item);
if (userParm == TRUE)
{
CString szTmp;
CString fmt("HTREEITEM=%#8x");
szTmp.Format(fmt, reinterpret_cast<int>(candidate));
text = CString(item.pszText) + CString(" ") + szTmp;
fmt = CString("LPARAM=%#8x");
szTmp.Format(fmt, item.lParam);
//szTmp.Format(fmt, reinterpret_cast<long>(item.lParam));
text = text + CString(" ") + szTmp;
}
else
{
text = item.pszText;
int offset = text.Find(_T(' '));
ASSERT(offset != -1);
text = text.Left( offset );
}
item.pszText = const_cast<TCHAR*>((LPCTSTR)text);
GetTreeCtrl().SetItem(&item);
return TRUE;
}
//////////
// CRTreCompare() is a global function which is called by the
// TreeView control when the user calls SortChildrenCB() and specifies
// this function as the value of LPTV_SORTCB.lpfnCompare.
//
int CALLBACK
CRTreViewCompare(LPARAM item1Param, LPARAM item2Param, LPARAM pThis)
{
return (reinterpret_cast<CRTreView*>(pThis))->
SortCompare(item1Param, item2Param);
}
//////////
// SortCompare() is a virtual function which implements a compare
// of two items in the tree. Since TreeView items are most
// often sorted by the text associated with that item,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -