void CMFCTreeDlg::InitTreeCtrl()
{
//设置控件的图标列表
m_ilNormal.Create(IDB_NORMAL,16,1,RGB(0,0,0));
m_wndTree.SetImageList(&m_ilNormal,TVSIL_NORMAL);
/*
//添加测试数据
HTREEITEM hRoot=m_wndTree.InsertItem("北京市",0,2);
HTREEITEM hCY=m_wndTree.InsertItem("朝阳区",1,3,hRoot);
HTREEITEM hHD=m_wndTree.InsertItem("海淀区",1,3,hRoot);
HTREEITEM hDC=m_wndTree.InsertItem("东城区",1,3,hRoot);
*/
//设置数据项的高度
m_wndTree.SetItemHeight(25);
//插入根目录
HTREEITEM hRoot=m_wndTree.InsertItem("C:\\",0,2);
ShowData("C:",hRoot);
//在根节点展开
m_wndTree.Expand(hRoot,TVE_EXPAND);
}
void CMFCTreeDlg::ShowData(CString strPath, HTREEITEM hParent)
{
//防止重复添加
if (m_wndTree.ItemHasChildren(hParent))
{
return;
}
CFileFind find;
BOOL bRet=find.FindFile(strPath+"\\*.*");
while(bRet)
{
HTREEITEM hItem;//插入节点的节点句柄
bRet=find.FindNextFile();
CString strName=find.GetFileName();
if (!find.IsDots())
{
if (find.IsDirectory())
{
hItem=m_wndTree.InsertItem(strName,0,2,
hParent);
}
else
{
hItem=m_wndTree.InsertItem(strName,1,3,
hParent);
}
//保存路径
CString *pPath=new CString;
*pPath=find.GetFilePath();
m_wndTree.SetItemData(hItem,(DWORD)pPath);
}
}
find.Close();
}
void CMFCTreeDlg::OnDblclkTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
//获取双击时选择的数据项
LPNMITEMACTIVATE pNMItem=(LPNMITEMACTIVATE)pNMHDR;
int nSel=pNMItem->iItem;
if (-1!=nSel)
{
CString *pPath=(CString*)
m_wndList.GetItemData(nSel);
//MessageBox(*pPath);
ShowData(*pPath);
}
*pResult = 0;
}
class CMyDlg : public CDialog
{
public:
CMyDlg(CWnd* pParent = NULL); // standard constructor
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
protected:
DECLARE_MESSAGE_MAP()
};
HTREEITEM hCurItem=m_wndTree.GetSelectedItem();
CString* pPath=(CString*)
m_wndTree.GetItemData(hCurItem);
ShowData(*pPath,hCurItem);
*pResult = 0;
}
//线程函数
UINT WorkerThreadProc(LPVOID lParam)
{
CMFCThreadDlg *pDlg=(CMFCThreadDlg *)lParam;
for (int i=0;i<10;i++)
{
pDlg->m_wndList.AddString("Hello worker");
Sleep(100);
}
return 0;
}
void CMFCThreadDlg::OnWorker()
{
AfxBeginThread(WorkerThreadProc,this);
}
#include "MyThread.h"
void CMFCThreadDlg::OnUi()
{
AfxBeginThread(RUNTIME_CLASS(CMyThread));
}
class CMyThread : public CWinThread
{
DECLARE_DYNCREATE(CMyThread)
protected:
CMyThread();
public:
virtual BOOL InitInstance();
virtual int ExitInstance();
protected:
virtual ~CMyThread();
DECLARE_MESSAGE_MAP()
};
BOOL CMyThread::InitInstance()
{
CMyDlg dlg;
m_pMainWnd=&dlg;
dlg.DoModal();
return TRUE;
}