.h文件源代码
#ifndef __TREE_LAYOUT_H__
#define __TREE_LAYOUT_H__
namespace DuiLib{
#define kTreeDataInterface _T("TreeData")
#define kTreeLayoutInterface _T("TreeLayout")
#define kTreeChildElementInterface _T("TreeChildElement")
#define kTreeElementInterface _T("TreeElement")
class CTreeLayoutUI;
class CTreeElementUI;
typedef int ( *TREESORTPROC)(CTreeElementUI * pFirst, CTreeElementUI * pSecond, CTreeLayoutUI * pTree);
//数据节点
class UILIB_API CTreeDataUI :public CHorizontalLayoutUI
{
public:
CTreeDataUI();
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
};
//树子节点
class UILIB_API CTreeChildElementUI : public CVerticalLayoutUI
{
public:
CTreeChildElementUI();
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
virtual bool GetVisible();
};
//树节点
class UILIB_API CTreeElementUI : public CVerticalLayoutUI
{
public:
CTreeElementUI();
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
public:
CTreeDataUI * GetDataElement();
};
//树
class UILIB_API CTreeLayoutUI : public CVerticalLayoutUI
{
public:
CTreeLayoutUI(void);
public:
LPCTSTR GetClass() const;
LPVOID GetInterface(LPCTSTR pstrName);
UINT GetControlFlags() const;
void DoEvent(TEventUI& event);
void SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue);
public:
LPCTSTR GetItemHotImage();
void SetItemHotImage(LPCTSTR pStrImage);
LPCTSTR GetItemSelectedImage();
void SetItemSelectedImage(LPCTSTR pStrImage);
void SetItemHotColor(DWORD dwColor);
DWORD GetItemHotColor() const;
void SetItemSelectedColor(DWORD dwColor);
DWORD GetItemSelectedColor() const;
void DrawItemBk(HDC hDC);
void DoPaint(HDC hDC, const RECT& rcPaint);
public:
bool Add(CTreeElementUI* pParent, CTreeElementUI * pControl, bool bInsertLast = true);
bool Remove(CTreeElementUI* pControl, bool bDelete);
void RemoveAll();
CTreeElementUI * GetRootItem();
CTreeElementUI * GetNextItem(CTreeElementUI * pElement);
CTreeElementUI * GetChildItem(CTreeElementUI * pElement);
void Expand(CTreeElementUI * pElement, UINT nCode);
bool IsExpand(CTreeElementUI * pElement);
bool ItemHasChildren(CTreeElementUI * pElement);
void SelectItem(CTreeElementUI * pElement, UINT nCode);
CTreeElementUI * GetSelectedItem();
CTreeElementUI * GetParentItem(CTreeElementUI * pElement);
CTreeElementUI * HitTest(POINT pt);
RECT GetItemRect(CTreeElementUI * pElement);
void SortChildren(CContainerUI * pElement, TREESORTPROC pSortProc);
void SetIndent(int nIndentation);
int GetIndent();
protected:
//收缩节点
void CollapseElement(CTreeElementUI * pElement);
//展开节点
void ExpandElement(CTreeElementUI * pElement);
void QuickSort(CContainerUI * pElement, TREESORTPROC pSortProc, int nFirst, int nEnd);
protected:
CTreeElementUI *m_pSelectedItem;
CTreeElementUI *m_pUpHotItem;
DWORD m_dwItemHotColor;
DWORD m_dwItemSelectedColor;
CStdString m_sItemHotImage;
CStdString m_sItemSelectedImage;
int m_nIndentation;//缩进值
};
}
#endif
.cpp文件源代码
#include "StdAfx.h"
#include "UITree.h"
namespace DuiLib{
//数据节点//
CTreeDataUI::CTreeDataUI()
{
}
LPCTSTR CTreeDataUI::GetClass() const
{
return _T("TreeDataUI");
}
LPVOID CTreeDataUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, kTreeDataInterface) == 0 ) return static_cast<CTreeDataUI*>(this);
return CContainerUI::GetInterface(pstrName);
}
//树子节点//
CTreeChildElementUI::CTreeChildElementUI()
{
}
LPCTSTR CTreeChildElementUI::GetClass() const
{
return _T("TreeChildElementUI");
}
LPVOID CTreeChildElementUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, kTreeChildElementInterface) == 0 ) return static_cast<CTreeChildElementUI*>(this);
return CContainerUI::GetInterface(pstrName);
}
bool CTreeChildElementUI::GetVisible()
{
return m_bVisible;
}
//树节点
CTreeElementUI::CTreeElementUI()
{
}
LPCTSTR CTreeElementUI::GetClass() const
{
return _T("TreeElementUI");
}
LPVOID CTreeElementUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, kTreeElementInterface) == 0 ) return static_cast<CTreeElementUI*>(this);
return CContainerUI::GetInterface(pstrName);
}
CTreeDataUI * CTreeElementUI::GetDataElement()
{
CTreeDataUI * pTreeData = NULL;
for (int i = 0; i < GetCount(); i++)
{
pTreeData = static_cast<CTreeDataUI *>(GetItemAt(i)->GetInterface(kTreeDataInterface));
if (pTreeData) break;
}
return pTreeData;
}
树///
CTreeLayoutUI::CTreeLayoutUI(void)
{
m_dwItemHotColor = 0xFFE9F5FF;
m_dwItemSelectedColor = 0xFFC1E3FF;
m_pSelectedItem = NULL;
m_pUpHotItem = NULL;
m_nIndentation = 10;
}
LPCTSTR CTreeLayoutUI::GetClass() const
{
return _T("TreeLayoutUI");
}
LPVOID CTreeLayoutUI::GetInterface(LPCTSTR pstrName)
{
if( _tcscmp(pstrName, kTreeLayoutInterface) == 0 ) return static_cast<CTreeLayoutUI*>(this);
return CContainerUI::GetInterface(pstrName);
}
UINT CTreeLayoutUI::GetControlFlags() const
{
if( IsEnabled() && m_iSepHeight != 0 ) return UIFLAG_SETCURSOR;
else return 0;
}
void CTreeLayoutUI::DoEvent(TEventUI& event)
{
if( !IsMouseEnabled() && event.Type > UIEVENT__MOUSEBEGIN && event.Type < UIEVENT__MOUSEEND ) {
if( m_pParent != NULL ) m_pParent->DoEvent(event);
else CControlUI::DoEvent(event);
return;
}
if( event.Type == UIEVENT_SETFOCUS )
{
m_bFocused = true;
return;
}
if( event.Type == UIEVENT_KILLFOCUS )
{
m_bFocused = false;
return;
}
if( m_pVerticalScrollBar != NULL && m_pVerticalScrollBar->IsVisible() && m_pVerticalScrollBar->IsEnabled() )
{
if( event.Type == UIEVENT_KEYDOWN )
{
switch( event.chKey ) {
case VK_DOWN:
LineDown();
return;
case VK_UP:
LineUp();
return;
case VK_NEXT:
PageDown();
return;
case VK_PRIOR:
PageUp();
return;
case VK_HOME:
HomeUp();
return;
case VK_END:
EndDown();
return;
}
}
else if( event.Type == UIEVENT_SCROLLWHEEL )
{
swi