// MutiTreeDemoDlg.cpp : implementation file
//
#include "stdafx.h"
#include "MutiTreeDemo.h"
#include "MutiTreeDemoDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMutiTreeDemoDlg dialog
CMutiTreeDemoDlg::CMutiTreeDemoDlg(CWnd* pParent /*=NULL*/)
: CDialog(CMutiTreeDemoDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMutiTreeDemoDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CMutiTreeDemoDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMutiTreeDemoDlg)
DDX_Control(pDX, IDC_TREE1, m_TripleTree);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMutiTreeDemoDlg, CDialog)
//{{AFX_MSG_MAP(CMutiTreeDemoDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_NOTIFY(NM_CLICK, IDC_TREE1, OnClickTree1)
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_NOTIFY(NM_RCLICK, IDC_TREE1, OnRclickTree1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMutiTreeDemoDlg message handlers
BOOL CMutiTreeDemoDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
HICON hIcon[3];
m_imList.Create(16,16,0,3,3);
hIcon[0]=AfxGetApp()->LoadIcon(IDI_ICON1);
hIcon[1]=AfxGetApp()->LoadIcon(IDI_ICON3);
hIcon[2]=AfxGetApp()->LoadIcon(IDI_ICON2);
for(int n=0;n<3;n++)
m_imList.Add(hIcon[n]);
m_TripleTree.SetImageList(&m_imList,TVSIL_NORMAL);
TV_INSERTSTRUCT tvinsert;
tvinsert.hParent=NULL;
tvinsert.hInsertAfter=TVI_LAST;
tvinsert.item.mask=TVIF_IMAGE|TVIF_SELECTEDIMAGE|TVIF_TEXT;
tvinsert.item.hItem=NULL;
tvinsert.item.state=0;
tvinsert.item.stateMask=0;
tvinsert.item.cchTextMax=6;
tvinsert.item.iSelectedImage=0;
tvinsert.item.cChildren=0;
tvinsert.item.lParam=0;
tvinsert.item.pszText="CObject";
tvinsert.item.iImage=0;
HTREEITEM hCObject=m_TripleTree.InsertItem(&tvinsert);
//second level
tvinsert.hParent=hCObject;
tvinsert.item.iImage=0;
tvinsert.item.pszText="CCmdTarget";
HTREEITEM hCCmdTarget=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCCmdTarget;
tvinsert.item.pszText="CDocTemplate";
HTREEITEM hCDocTemplate=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCDocTemplate;
tvinsert.item.pszText="CSingleDocTemplate";
HTREEITEM hCSingleDocTemplate=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCDocTemplate;
tvinsert.item.pszText="CMultiDocTemplate";
HTREEITEM hCMultiDocTemplate=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCCmdTarget;
tvinsert.item.pszText="CWinThread";
HTREEITEM hCWinThread=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCWinThread;
tvinsert.item.pszText="CWinApp";
HTREEITEM hCWinApp=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=hCWinApp;
tvinsert.item.pszText="COleControlModule";
HTREEITEM hCOleControlModule=m_TripleTree.InsertItem(&tvinsert);
tvinsert.hParent=NULL;
tvinsert.item.pszText="CHtmlStream";
tvinsert.item.iImage=0;
m_TripleTree.InsertItem(&tvinsert);
tvinsert.item.pszText="CHttpFilter";
tvinsert.item.iImage=0;
m_TripleTree.InsertItem(&tvinsert);
tvinsert.item.pszText="CHttpFilterContext";
tvinsert.item.iImage=0;
m_TripleTree.InsertItem(&tvinsert);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CMutiTreeDemoDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CMutiTreeDemoDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMutiTreeDemoDlg::OnClickTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
UINT uFlags=0;
HTREEITEM hPItem;
CPoint point = GetCurrentMessage()->pt;
ScreenToClient(&point);
point.Offset(-8,-16);
HTREEITEM hItem =m_TripleTree.HitTest(point, &uFlags);
if ( (uFlags&TVHT_ONITEM) )
{
hPItem=m_TripleTree.GetParentItem(hItem);
int m,n,m1,n1;
m_TripleTree.GetItemImage(hItem,m,n);
if(m!=1)
{
m_TripleTree.SetItemImage(hItem,1,1);
m_TripleTree.GetItemImage(hItem,m1,n1);
TravelChild(hItem, m1);
if(hPItem!=NULL)
TravelSiblingAndParent(hItem,m1);
}
else if(m==1)
{
m_TripleTree.SetItemImage(hItem,0,0);
m_TripleTree.GetItemImage(hItem,m1,n1);
TravelChild(hItem, m1);
if(hPItem!=NULL)
TravelSiblingAndParent(hItem,m1);
}
// MessageBox("xxxx");
}
*pResult = 0;
}
void CMutiTreeDemoDlg::OnRclickTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
//递归
void CMutiTreeDemoDlg::TravelChild(HTREEITEM hItem,int m)
{
HTREEITEM hChildItem,hBrotherItem;
hChildItem=m_TripleTree.GetChildItem(hItem);
m_TripleTree.SetItemImage(hChildItem,m,m);
if(hChildItem!=NULL)
TravelChild(hChildItem, m);
hBrotherItem=m_TripleTree.GetNextSiblingItem(hChildItem);
m_TripleTree.SetItemImage(hBrotherItem,m,m);
if(hBrotherItem!=NULL)
TravelChild(hBrotherItem, m);
}
//递归
void CMutiTreeDemoDlg::TravelSiblingAndParent(HTREEITEM hItem, int m)
{
int m1,n1;
HTREEITEM hNextSiblingItem,hPrevSiblingItem,hParentItem;
m1=m;
hNextSiblingItem=m_TripleTree.GetNextSiblingItem(hItem);
while(hNextSiblingItem!=NULL)
{
m_TripleTree.GetItemImage(hNextSiblingItem,m1,n1);
if(m1!=m)
break;
else
{
hNextSiblingItem=m_TripleTree.GetNextSiblingItem(hNextSiblingItem);
if(hNextSiblingItem!=NULL)
m_TripleTree.GetItemImage(hNextSiblingItem,m1,n1);
}
}
hPrevSiblingItem=m_TripleTree.GetPrevSiblingItem(hItem);
while(hPrevSiblingItem!=NULL)
{
m_TripleTree.GetItemImage(hPrevSiblingItem,m1,n1);
if(m1!=m)
break;
else
{
hPrevSiblingItem=m_TripleTree.GetPrevSiblingItem(hPrevSiblingItem);
if(hPrevSiblingItem!=NULL)
m_TripleTree.GetItemImage(hPrevSiblingItem,m1,n1);
}
}
if(m1==m)
{
hParentItem=m_TripleTree.GetParentItem(hItem);
if(hParentItem!=NULL)
{
m_TripleTree.SetItemImage(hParentItem,m1,m1);
TravelSiblingAndParent(hParentItem,m1);
}
}
if(m1!=m)
{
hParentItem=m_TripleTree.GetParentItem(hItem);
while(hParentItem!=NULL)
{
m_TripleTree.SetItemImage(hParentItem,2,2);
hParentItem=m_TripleTree.GetParentItem(hParentItem);
}
}
}