100分,一个简单的功能(关于CTreeCtrl)

时间:2022-08-31 16:33:48
偶初学VC,请各位大侠帮忙
// TreeDlg.h : 头文件
//

#pragma once
#include "afxcmn.h"


// CTreeDlg 对话框
class CTreeDlg : public CDialog
{
// 构造
public:
CTreeDlg(CWnd* pParent = NULL); // 标准构造函数

// 对话框数据
enum { IDD = IDD_TREE_DIALOG };

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持


CString m_path;
HTREEITEM m_htreex;
    void ReBuildTreem(HTREEITEM m_htreeFrom,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo);
    HTREEITEM LGetHtreeItemFormName1(HTREEITEM hitem/*pFrame->m_cTree树*/,CString str,CTreeCtrl *m_TreeA);
void ReBuildTreec(HTREEITEM hitem/*pFrame->m_cTree树*/,HTREEITEM parent/*m_Tree1树*/,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo); 
    CString FindStrFromText(CString listtext);
    HTREEITEM LGetHtreeItemFormName(CString str,CTreeCtrl *m_TreeA);
// 实现
protected:
HICON m_hIcon;

// 生成的消息映射函数
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()

public:
CTreeCtrl m_Tree1;
CTreeCtrl m_Tree2;

afx_msg void OnBnClickedButton1();
afx_msg void OnBnClickedButton2();
};

9 个解决方案

#1


// TreeDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Tree.h"
#include "TreeDlg.h"
#include ".\treedlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// 对话框数据
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CTreeDlg 对话框



CTreeDlg::CTreeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTreeDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_path="";
m_htreex=NULL;
}

void CTreeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE1, m_Tree1);
DDX_Control(pDX, IDC_TREE2, m_Tree2);
DDX_Control(pDX, IDC_TREE3, m_Tree3);
}

BEGIN_MESSAGE_MAP(CTreeDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
END_MESSAGE_MAP()


// CTreeDlg 消息处理程序

BOOL CTreeDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// 将\“关于...\”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码
HTREEITEM  hti1 = m_Tree1.InsertItem ( _T("AA"),0,1 );
m_Tree1.InsertItem(_T("AA1"),0,1,hti1);
m_Tree1.InsertItem(_T("AA2"),0,1,hti1);
m_Tree1.InsertItem(_T("AA3"),0,1,hti1);
HTREEITEM hti1subitem=m_Tree1.InsertItem(_T("AA4"),0,1,hti1);
HTREEITEM myitem=m_Tree1.InsertItem(_T("AA41"),0,1,hti1subitem);
m_Tree1.InsertItem(_T("AA411"),0,1,myitem);

//HTREEITEM  hti2=m_Tree1.InsertItem(_T("BB"),0,1);
    m_Tree1.InsertItem(_T("BB1"),0,1,hti1);
m_Tree1.InsertItem(_T("BB2"),0,1,hti1);
m_Tree1.InsertItem(_T("BB3"),0,1,hti1);
m_Tree1.Expand(m_Tree1.GetRootItem(),TVE_EXPAND);
return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CTreeDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作矩形中居中
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;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CTreeDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

#2


//接上面Cpp文件
void CTreeDlg::ReBuildTreem(HTREEITEM m_htreeFrom,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo) 
{
      m_path="";
  HTREEITEM hItem9=m_htreeFrom;
  //从根结点起记录路径
  hItem9=m_TreeFrom->GetParentItem(hItem9);

  while (hItem9!=NULL)
  {
CString str=m_TreeFrom->GetItemText(hItem9);
//str.TrimLeft();
//str.TrimRight();
str.Trim();
str=FindStrFromText(str);
if (str!="")
m_path=_T("[") +str+ _T("]")+m_path;
hItem9=m_TreeFrom->GetParentItem(hItem9);
  }
  m_path.TrimLeft();
  m_path.TrimRight();
  CString m_str="";
  HTREEITEM m_k1=m_TreeFrom->GetRootItem( );
  HTREEITEM m_k2=m_TreeTo->GetRootItem( );


  if (m_k1==m_htreeFrom && m_k2!=NULL)
  {
     m_htreex=m_k2;
   ReBuildTreec(m_htreeFrom,m_htreex,m_TreeFrom,m_TreeTo);

 HTREEITEM m_J1=m_TreeFrom->GetParentItem(m_htreeFrom);//取父句柄
 m_TreeFrom->DeleteItem(m_htreeFrom);//删本句柄
 if (m_TreeFrom->GetChildItem(m_J1)==NULL)
 m_TreeFrom->DeleteItem(m_J1);//删父句柄


  }
  else
  {
m_htreex=TVI_ROOT;
//建新路径
for(int i=0;i<m_path.GetLength();i++)
{
char ch1=m_path.GetAt(i);
if (ch1=='[')
{
continue;
}
if (ch1==']')
{
//插入新结点
HTREEITEM m_1=LGetHtreeItemFormName(m_str,m_TreeTo);
if (m_1==NULL)
{
//if(m_TreeTo->GetRootItem()==NULL)
//{
// m_htreex=m_TreeTo->InsertItem(m_str,TVI_ROOT,TVI_LAST);
//}
//else
//{
m_htreex=m_TreeTo->InsertItem(m_str,m_htreex,TVI_LAST);
//}
}
else
{
m_htreex=m_1;
}
m_str="";
continue;      
}
m_str+=ch1;
  
}
CString str1=m_TreeFrom->GetItemText(m_htreeFrom);
str1=FindStrFromText(str1);
HTREEITEM item1=LGetHtreeItemFormName(str1,m_TreeTo);
if (item1==NULL)
item1=m_TreeTo->InsertItem(str1,m_htreex,TVI_LAST);
ReBuildTreec(m_htreeFrom,item1,m_TreeFrom,m_TreeTo);
  }
  HTREEITEM m_J1=NULL;
  HTREEITEM m_J2=m_htreeFrom;
      m_J1=m_TreeFrom->GetParentItem(m_J2);//取父句柄
      m_TreeFrom->DeleteItem(m_J2);//删本句柄
    while(1)
{
    m_J2=m_TreeFrom->GetParentItem(m_J1);//取父句柄
    if (m_TreeFrom->GetChildItem(m_J1)==NULL)
{
       m_TreeFrom->DeleteItem(m_J1);//删本句柄
}
else
{
   break;
}
if (m_J2==NULL)
{
HTREEITEM m_c=m_TreeFrom->GetRootItem();
if (m_TreeFrom->GetChildItem(m_c)==NULL)
            m_TreeFrom->DeleteItem(m_c);//删本句柄
                break;
}
}
 
      m_TreeTo->Expand(m_TreeTo->GetRootItem(),TVE_EXPAND);

 

}
HTREEITEM CTreeDlg::LGetHtreeItemFormName1(HTREEITEM hitem/*pFrame->m_cTree树*/,CString str,CTreeCtrl *m_TreeA) 
{
     HTREEITEM nextitem1=m_TreeA->GetNextItem(hitem,TVGN_CHILD);
  while (nextitem1!=NULL)
  {
     CString str2=m_TreeA->GetItemText(nextitem1);
 str2.TrimLeft();
 str2.TrimRight();
 if (str2==str)
 return nextitem1;
 HTREEITEM M_2=LGetHtreeItemFormName1(nextitem1,str,m_TreeA);
 if (M_2!=NULL)
 return M_2;
         nextitem1=m_TreeA->GetNextItem(nextitem1,TVGN_NEXT);
  }
  return NULL;
}
void CTreeDlg::ReBuildTreec(HTREEITEM hitem/*pFrame->m_cTree树*/,HTREEITEM parent/*m_Tree1树*/,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo) 
{
     HTREEITEM nextitem1=m_TreeFrom->GetNextItem(hitem,TVGN_CHILD);
  while (nextitem1!=NULL)
  {
     CString str2=m_TreeFrom->GetItemText(nextitem1);
 str2=FindStrFromText(str2);
 str2.TrimLeft();
 str2.TrimRight();
     HTREEITEM item2=m_TreeTo->InsertItem(str2,parent,TVI_LAST );
 ReBuildTreec(nextitem1,item2,m_TreeFrom,m_TreeTo);
         nextitem1=m_TreeFrom->GetNextItem(nextitem1,TVGN_NEXT);
  }
}
CString CTreeDlg::FindStrFromText(CString listtext)
{
listtext.TrimLeft();
listtext.TrimRight();
int len=listtext.GetLength();
CString m_str="";
    for (int i=0;i<len;i++)
{
  char ch1=listtext.GetAt(i);
  if(ch1=='(')
  break;
      m_str+=ch1;
}
    return m_str;
}
HTREEITEM CTreeDlg::LGetHtreeItemFormName(CString str,CTreeCtrl *m_TreeA)
{
 HTREEITEM m_htree1=m_TreeA->GetRootItem();
     if (m_htree1==NULL)
 return NULL;
 str.TrimLeft();
 str.TrimRight();
 CString str1=m_TreeA->GetItemText(m_htree1);
 str1.TrimLeft();
 str1.TrimRight();
 if (str1==str)
 return m_htree1;
 HTREEITEM M_1=LGetHtreeItemFormName1(m_htree1,str,m_TreeA);
 if (M_1!=NULL)
 return M_1;
 return NULL;
}
void CTreeDlg::OnBnClickedButton1()
{
HTREEITEM myitem=m_Tree1.GetSelectedItem();
    ReBuildTreem(myitem,&m_Tree1,&m_Tree2);
}

void CTreeDlg::OnBnClickedButton2()
{
HTREEITEM item=m_Tree2.GetSelectedItem();
    ReBuildTreem(item,&m_Tree2,&m_Tree1);

}

#3


贴了这么多,什么问题啊,你应该先说问题,需要的话再贴代码

#4


基本功能是实现了,但是先从m_Tree1移动项目到m_Tree2,再从m_Tree2移动项到m_Tree1时,m_Tree2没有项时,再从m_Tree1移动项到m_Tree2时,项就丢失了!不知道为什么,自己觉得代码没有错的。

#5


huaboy408(闭关修练)
    我是不知道怎么问了,所以把代码一起贴出来算了

#6


是啊,什么问题?

#7


晕~这么多怎么看啊

#8


大概懂了你的意思,参照一下下面的例子吧
http://www.vccode.com/file_show.php?id=961
http://www.vccode.com/file_show.php?id=710

#9


谢谢!

#1


// TreeDlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Tree.h"
#include "TreeDlg.h"
#include ".\treedlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialog
{
public:
CAboutDlg();

// 对话框数据
enum { IDD = IDD_ABOUTBOX };

protected:
virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CTreeDlg 对话框



CTreeDlg::CTreeDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTreeDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_path="";
m_htreex=NULL;
}

void CTreeDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_TREE1, m_Tree1);
DDX_Control(pDX, IDC_TREE2, m_Tree2);
DDX_Control(pDX, IDC_TREE3, m_Tree3);
}

BEGIN_MESSAGE_MAP(CTreeDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
END_MESSAGE_MAP()


// CTreeDlg 消息处理程序

BOOL CTreeDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// 将\“关于...\”菜单项添加到系统菜单中。

// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);

CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}

// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
//  执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标

// TODO: 在此添加额外的初始化代码
HTREEITEM  hti1 = m_Tree1.InsertItem ( _T("AA"),0,1 );
m_Tree1.InsertItem(_T("AA1"),0,1,hti1);
m_Tree1.InsertItem(_T("AA2"),0,1,hti1);
m_Tree1.InsertItem(_T("AA3"),0,1,hti1);
HTREEITEM hti1subitem=m_Tree1.InsertItem(_T("AA4"),0,1,hti1);
HTREEITEM myitem=m_Tree1.InsertItem(_T("AA41"),0,1,hti1subitem);
m_Tree1.InsertItem(_T("AA411"),0,1,myitem);

//HTREEITEM  hti2=m_Tree1.InsertItem(_T("BB"),0,1);
    m_Tree1.InsertItem(_T("BB1"),0,1,hti1);
m_Tree1.InsertItem(_T("BB2"),0,1,hti1);
m_Tree1.InsertItem(_T("BB3"),0,1,hti1);
m_Tree1.Expand(m_Tree1.GetRootItem(),TVE_EXPAND);
return TRUE;  // 除非设置了控件的焦点,否则返回 TRUE
}

void CTreeDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}

// 如果向对话框添加最小化按钮,则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
//  这将由框架自动完成。

void CTreeDlg::OnPaint() 
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文

SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

// 使图标在工作矩形中居中
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;

// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}

//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CTreeDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}

#2


//接上面Cpp文件
void CTreeDlg::ReBuildTreem(HTREEITEM m_htreeFrom,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo) 
{
      m_path="";
  HTREEITEM hItem9=m_htreeFrom;
  //从根结点起记录路径
  hItem9=m_TreeFrom->GetParentItem(hItem9);

  while (hItem9!=NULL)
  {
CString str=m_TreeFrom->GetItemText(hItem9);
//str.TrimLeft();
//str.TrimRight();
str.Trim();
str=FindStrFromText(str);
if (str!="")
m_path=_T("[") +str+ _T("]")+m_path;
hItem9=m_TreeFrom->GetParentItem(hItem9);
  }
  m_path.TrimLeft();
  m_path.TrimRight();
  CString m_str="";
  HTREEITEM m_k1=m_TreeFrom->GetRootItem( );
  HTREEITEM m_k2=m_TreeTo->GetRootItem( );


  if (m_k1==m_htreeFrom && m_k2!=NULL)
  {
     m_htreex=m_k2;
   ReBuildTreec(m_htreeFrom,m_htreex,m_TreeFrom,m_TreeTo);

 HTREEITEM m_J1=m_TreeFrom->GetParentItem(m_htreeFrom);//取父句柄
 m_TreeFrom->DeleteItem(m_htreeFrom);//删本句柄
 if (m_TreeFrom->GetChildItem(m_J1)==NULL)
 m_TreeFrom->DeleteItem(m_J1);//删父句柄


  }
  else
  {
m_htreex=TVI_ROOT;
//建新路径
for(int i=0;i<m_path.GetLength();i++)
{
char ch1=m_path.GetAt(i);
if (ch1=='[')
{
continue;
}
if (ch1==']')
{
//插入新结点
HTREEITEM m_1=LGetHtreeItemFormName(m_str,m_TreeTo);
if (m_1==NULL)
{
//if(m_TreeTo->GetRootItem()==NULL)
//{
// m_htreex=m_TreeTo->InsertItem(m_str,TVI_ROOT,TVI_LAST);
//}
//else
//{
m_htreex=m_TreeTo->InsertItem(m_str,m_htreex,TVI_LAST);
//}
}
else
{
m_htreex=m_1;
}
m_str="";
continue;      
}
m_str+=ch1;
  
}
CString str1=m_TreeFrom->GetItemText(m_htreeFrom);
str1=FindStrFromText(str1);
HTREEITEM item1=LGetHtreeItemFormName(str1,m_TreeTo);
if (item1==NULL)
item1=m_TreeTo->InsertItem(str1,m_htreex,TVI_LAST);
ReBuildTreec(m_htreeFrom,item1,m_TreeFrom,m_TreeTo);
  }
  HTREEITEM m_J1=NULL;
  HTREEITEM m_J2=m_htreeFrom;
      m_J1=m_TreeFrom->GetParentItem(m_J2);//取父句柄
      m_TreeFrom->DeleteItem(m_J2);//删本句柄
    while(1)
{
    m_J2=m_TreeFrom->GetParentItem(m_J1);//取父句柄
    if (m_TreeFrom->GetChildItem(m_J1)==NULL)
{
       m_TreeFrom->DeleteItem(m_J1);//删本句柄
}
else
{
   break;
}
if (m_J2==NULL)
{
HTREEITEM m_c=m_TreeFrom->GetRootItem();
if (m_TreeFrom->GetChildItem(m_c)==NULL)
            m_TreeFrom->DeleteItem(m_c);//删本句柄
                break;
}
}
 
      m_TreeTo->Expand(m_TreeTo->GetRootItem(),TVE_EXPAND);

 

}
HTREEITEM CTreeDlg::LGetHtreeItemFormName1(HTREEITEM hitem/*pFrame->m_cTree树*/,CString str,CTreeCtrl *m_TreeA) 
{
     HTREEITEM nextitem1=m_TreeA->GetNextItem(hitem,TVGN_CHILD);
  while (nextitem1!=NULL)
  {
     CString str2=m_TreeA->GetItemText(nextitem1);
 str2.TrimLeft();
 str2.TrimRight();
 if (str2==str)
 return nextitem1;
 HTREEITEM M_2=LGetHtreeItemFormName1(nextitem1,str,m_TreeA);
 if (M_2!=NULL)
 return M_2;
         nextitem1=m_TreeA->GetNextItem(nextitem1,TVGN_NEXT);
  }
  return NULL;
}
void CTreeDlg::ReBuildTreec(HTREEITEM hitem/*pFrame->m_cTree树*/,HTREEITEM parent/*m_Tree1树*/,CTreeCtrl *m_TreeFrom,CTreeCtrl *m_TreeTo) 
{
     HTREEITEM nextitem1=m_TreeFrom->GetNextItem(hitem,TVGN_CHILD);
  while (nextitem1!=NULL)
  {
     CString str2=m_TreeFrom->GetItemText(nextitem1);
 str2=FindStrFromText(str2);
 str2.TrimLeft();
 str2.TrimRight();
     HTREEITEM item2=m_TreeTo->InsertItem(str2,parent,TVI_LAST );
 ReBuildTreec(nextitem1,item2,m_TreeFrom,m_TreeTo);
         nextitem1=m_TreeFrom->GetNextItem(nextitem1,TVGN_NEXT);
  }
}
CString CTreeDlg::FindStrFromText(CString listtext)
{
listtext.TrimLeft();
listtext.TrimRight();
int len=listtext.GetLength();
CString m_str="";
    for (int i=0;i<len;i++)
{
  char ch1=listtext.GetAt(i);
  if(ch1=='(')
  break;
      m_str+=ch1;
}
    return m_str;
}
HTREEITEM CTreeDlg::LGetHtreeItemFormName(CString str,CTreeCtrl *m_TreeA)
{
 HTREEITEM m_htree1=m_TreeA->GetRootItem();
     if (m_htree1==NULL)
 return NULL;
 str.TrimLeft();
 str.TrimRight();
 CString str1=m_TreeA->GetItemText(m_htree1);
 str1.TrimLeft();
 str1.TrimRight();
 if (str1==str)
 return m_htree1;
 HTREEITEM M_1=LGetHtreeItemFormName1(m_htree1,str,m_TreeA);
 if (M_1!=NULL)
 return M_1;
 return NULL;
}
void CTreeDlg::OnBnClickedButton1()
{
HTREEITEM myitem=m_Tree1.GetSelectedItem();
    ReBuildTreem(myitem,&m_Tree1,&m_Tree2);
}

void CTreeDlg::OnBnClickedButton2()
{
HTREEITEM item=m_Tree2.GetSelectedItem();
    ReBuildTreem(item,&m_Tree2,&m_Tree1);

}

#3


贴了这么多,什么问题啊,你应该先说问题,需要的话再贴代码

#4


基本功能是实现了,但是先从m_Tree1移动项目到m_Tree2,再从m_Tree2移动项到m_Tree1时,m_Tree2没有项时,再从m_Tree1移动项到m_Tree2时,项就丢失了!不知道为什么,自己觉得代码没有错的。

#5


huaboy408(闭关修练)
    我是不知道怎么问了,所以把代码一起贴出来算了

#6


是啊,什么问题?

#7


晕~这么多怎么看啊

#8


大概懂了你的意思,参照一下下面的例子吧
http://www.vccode.com/file_show.php?id=961
http://www.vccode.com/file_show.php?id=710

#9


谢谢!

相关文章