对于CListContainerElementUI 表头拖拽不起作用的问题,我也转载过一篇文章,用了他的东西没起作用,主要因为他没有深入的解析,自己研究了一番,终于解决了问题!
关于转载的文章中,他修改该问题是直接修改源码的,因为这不是bug!所以该人家的源码是不合理的,我认为对于一个已经完成的组件,要扩展它的功能一定不能修改他本来的东西(虽然是开源的,修改也能满足要求)但是不符合软件设计的开辟原则!!!
关于拖拽的问题,其实转载的文章中提到了实现方法,但是由于个人xml编写的不同则代码也不一样,文章中没有提到如何修改,修改如何与xml相关联。本文中将根据xml代码告知如何修改代码!
当CListHeader发生变化的时候CListContainerElementUI同样也发生位置变化,所以我们想移动不同的子控件到不同的位置在实际情况下是可行的,也就是将对应的控件移动到不同的位置即可,这个位置就是左边和右边与CListHeader的各个子控件位置的左边和右边相等即可!这就是重点,当关键点不能丢失,如果你的ListContainerElement又包含(或者说添加)了几个动态的XML,那么问题就来了:
CListHeaderUI中包含的CListHeaderItemUI个数==CListContainerElementUI的一行(xml)中的子项数目;我的CDeviceStateItem继承与CListContainerElementUI,也就是说CListContainerElementUI会自动载入一个xml作为CListContainerElementUI的一行,所有CListContainerElementUI中Add的对象因为是一个XML;换句话说CListContainerElementUI中只有一项,该项就是xml项目的跟元素;xml根元素这包含n个列项目;
所以在SetPos要做一下几步:
(1)将CListContainerElementUI(这里就是CDeviceStateItem)的唯一一个容器(也就是xml--描述包含的列,所以必定是一个容器)移动到CListHeader的位置;
(2)将唯一的容器中包含的所有列对应移动到CListHeaderItem的左右相等的位置;
如果CDeviceStateItem直接添加的不是一个xml容器(包含所有列)而是通过代码直接添加ControlUI,那么CDeviceStateItem的子项(也就是列)的左右与CListHeaderItem的左右位置相等;当在大多数情况下一般是用我用的那种将xml封装成一个,然后载入添加到CListContainerElement中;
xml(对应list的一行)容器代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Window>
<HorizontalLayout>
<Label name="guid" visible="false" showhtml="true"/>
<Label name="devicetype" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="ip" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="webstate" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="recordstate" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="diskstate" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="hardwarestate" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="linkcount" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="cpu" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
<Label name="memory" align="center" showhtml="true" textcolor="#FFFFFFFF"/>
</HorizontalLayout>
</Window>
list描述如下:
<?xml version="1.0" encoding="UTF-8"?>
<Window size="1000,600" >
<Font name="黑体" size="12" bold="true" />
<Default name="VScrollBar" value="button1normalimage="file='scrollbar.bmp' source='0,90,16,106' mask='#FFFF00FF'" button1hotimage="file='scrollbar.bmp' source='18,90,34,106' mask='#FFFF00FF'" button1pushedimage="file='scrollbar.bmp' source='36,90,52,106' mask='#FFFF00FF'" button1disabledimage="file='scrollbar.bmp' source='54,90,70,106' mask='#FFFF00FF'" button2normalimage="file='scrollbar.bmp' source='0,108,16,124' mask='#FFFF00FF'" button2hotimage="file='scrollbar.bmp' source='18,108,34,124' mask='#FFFF00FF'" button2pushedimage="file='scrollbar.bmp' source='36,108,52,124' mask='#FFFF00FF'" button2disabledimage="file='scrollbar.bmp' source='54,108,70,124' mask='#FFFF00FF'" thumbnormalimage="file='scrollbar.bmp' source='0,126,16,142' corner='2,2,2,2' mask='#FFFF00FF'" thumbhotimage="file='scrollbar.bmp' source='18,126,34,142' corner='2,2,2,2' mask='#FFFF00FF'" thumbpushedimage="file='scrollbar.bmp' source='36,126,52,142' corner='2,2,2,2' mask='#FFFF00FF'" thumbdisabledimage="file='scrollbar.bmp' source='54,126,70,142' corner='2,2,2,2' mask='#FFFF00FF'" railnormalimage="file='scrollbar.bmp' source='0,144,16,160' corner='2,2,2,2' mask='#FFFF00FF'" railhotimage="file='scrollbar.bmp' source='18,144,34,160' corner='2,2,2,2' mask='#FFFF00FF'" railpushedimage="file='scrollbar.bmp' source='36,144,52,160' corner='2,2,2,2' mask='#FFFF00FF'" raildisabledimage="file='scrollbar.bmp' source='54,144,70,160' corner='2,2,2,2' mask='#FFFF00FF'" bknormalimage="file='scrollbar.bmp' source='0,162,16,178' corner='2,2,2,2' mask='#FFFF00FF'" bkhotimage="file='scrollbar.bmp' source='18,162,34,178' corner='2,2,2,2' mask='#FFFF00FF'" bkpushedimage="file='scrollbar.bmp' source='36,162,52,178' corner='2,2,2,2' mask='#FFFF00FF'" bkdisabledimage="file='scrollbar.bmp' source='54,162,70,178' corner='2,2,2,2' mask='#FFFF00FF'" " />
<Default name="HScrollBar" value="button1normalimage="file='scrollbar.bmp' source='0,0,16,16' mask='#FFFF00FF'" button1hotimage="file='scrollbar.bmp' source='18,0,34,16' mask='#FFFF00FF'" button1pushedimage="file='scrollbar.bmp' source='36,0,52,16' mask='#FFFF00FF'" button1disabledimage="file='scrollbar.bmp' source='54,0,70,16' mask='#FFFF00FF'" button2normalimage="file='scrollbar.bmp' source='0,18,16,34' mask='#FFFF00FF'" button2hotimage="file='scrollbar.bmp' source='18,18,34,34' mask='#FFFF00FF'" button2pushedimage="file='scrollbar.bmp' source='36,18,52,34' mask='#FFFF00FF'" button2disabledimage="file='scrollbar.bmp' source='54,18,70,34' mask='#FFFF00FF'" thumbnormalimage="file='scrollbar.bmp' source='0,36,16,52' corner='2,2,2,2' mask='#FFFF00FF'" thumbhotimage="file='scrollbar.bmp' source='18,36,34,52' corner='2,2,2,2' mask='#FFFF00FF'" thumbpushedimage="file='scrollbar.bmp' source='36,36,52,52' corner='2,2,2,2' mask='#FFFF00FF'" thumbdisabledimage="file='scrollbar.bmp' source='54,36,70,52' corner='2,2,2,2' mask='#FFFF00FF'" railnormalimage="file='scrollbar.bmp' source='0,54,16,70' corner='2,2,2,2' mask='#FFFF00FF'" railhotimage="file='scrollbar.bmp' source='18,54,34,70' corner='2,2,2,2' mask='#FFFF00FF'" railpushedimage="file='scrollbar.bmp' source='36,54,52,70' corner='2,2,2,2' mask='#FFFF00FF'" raildisabledimage="file='scrollbar.bmp' source='54,54,70,70' corner='2,2,2,2' mask='#FFFF00FF'" bknormalimage="file='scrollbar.bmp' source='0,72,16,88' corner='2,2,2,2' mask='#FFFF00FF'" bkhotimage="file='scrollbar.bmp' source='18,72,34,88' corner='2,2,2,2' mask='#FFFF00FF'" bkpushedimage="file='scrollbar.bmp' source='36,72,52,88' corner='2,2,2,2' mask='#FFFF00FF'" bkdisabledimage="file='scrollbar.bmp' source='54,72,70,88' corner='2,2,2,2' mask='#FFFF00FF'" " />
<VerticalLayout bkcolor="FF696969">
<List name="statelist" bkcolor="#FF535353" inset="0,1,0,0" itemshowhtml="true" vscrollbar="true" hscrollbar="true" headerbkimage="file='播放背景.png'"
itemalign="center" itemaltbk="true" menu="true" itemtextcolor="#FFFFFFFF" itemhottextcolor="#FFFFFFFF" itembkcolor="#FF535353" itemselectedbkcolor="#FF7E6849" itemhotbkcolor="#FF696969" itemendellipsis="ture">
<ListHeader height="28" menu="true" >
<ListHeaderItem name="GUID" text="" textcolor="#FF696969" width="1" visible="false"/>
<ListHeaderItem text="设备类型" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="IP地址" textcolor="#FFFFFFFF" width="200" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="网络状态" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="录像状态" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="硬盘状态" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="硬件状态" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="用户连接数" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="CPU使用率" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
<ListHeaderItem text="内存使用率" textcolor="#FFFFFFFF" font="0" sepimage="分割线_竖2.png" sepwidth="1" normalimage="列表选中背景.png" hotimage="列表高亮.png" pushedimage="列表高亮.png" />
</ListHeader>
</List>
</VerticalLayout>
</Window>
然后动态载入某一行的代码如下(扩展而不是修改源码):
.h文件:
#pragma once
#include "duilib/UIlib.h"
using namespace DuiLib;
class CDeviceStateItem
: public CListContainerElementUI
{
public:
CDeviceStateItem(CPaintManagerUI* pPaintMgr, CListHeaderUI* pHeader);
virtual ~CDeviceStateItem(void);
public:
BOOL Initailize();
bool OnTheEvent(void* pParam);
void SetText(int nCol, CString strText);
CDuiString GetText(int nCol);
void SetTooTip(int nCol, CString strTip);
void SetPos(RECT rc);
protected:
CListHeaderUI* m_pHeader;
CContainerUI* m_pContainer;
CPaintManagerUI* m_pPaintMgr;
};
.cpp文件:
#include "StdAfx.h"
#include "DeviceStateItem.h"
CDeviceStateItem::CDeviceStateItem(CPaintManagerUI* pPaintMgr, CListHeaderUI* pHeader)
{
m_pPaintMgr = pPaintMgr;
m_pContainer = NULL;
m_pHeader = pHeader;
Initailize();
}
CDeviceStateItem::~CDeviceStateItem(void)
{
}
BOOL CDeviceStateItem::Initailize()
{
CDialogBuilder objBuilder;
m_pContainer = static_cast<CContainerUI*>(objBuilder.Create(_T("systemstateitem.xml"), 0, NULL, m_pPaintMgr, NULL));
ASSERT(NULL != m_pContainer);
if (NULL == m_pContainer)
{
RemoveAll();
return FALSE;
}
Add(m_pContainer);
return TRUE;
}
void CDeviceStateItem::SetText(int nCol, CString strText)
{
if(nCol >= m_pContainer->GetCount())
return ;
m_pContainer->GetItemAt(nCol)->SetText(strText);
}
CDuiString CDeviceStateItem::GetText(int nCol)
{
if(nCol >= m_pContainer->GetCount())
return _T("");
return m_pContainer->GetItemAt(nCol)->GetText();
}
void CDeviceStateItem::SetTooTip(int nCol, CString strTip)
{
if(nCol >= m_pContainer->GetCount())
return ;
m_pContainer->GetItemAt(nCol)->SetToolTip(strTip);
}
void CDeviceStateItem::SetPos(RECT rc)
{
CContainerUI::SetPos(rc);
if( m_pOwner == NULL ) return;
if (m_pHeader == NULL || m_pContainer == NULL)
{
return;
}
// 首先移动xml容器跟节点容器,使其与Header控件位置左右相等
RECT rtHeader = m_pHeader->GetPos();
RECT rt = m_pContainer->GetPos();
rt.left = rtHeader.left;
rt.right = rtHeader.right;
m_pContainer->SetPos(rt);
// 然后移动根节点容器的所有列与HeaderItem的左右相等
for (int nIndex = 0; NULL != m_pContainer && nIndex < m_pContainer->GetCount(); ++nIndex)
{
CListHeaderItemUI *pHeaderItem = static_cast<CListHeaderItemUI*>(m_pHeader->GetItemAt(nIndex));
CControlUI* pControl = m_pContainer->GetItemAt(nIndex);
if (pControl != NULL && pHeaderItem != NULL)
{
RECT rtHeader = pHeaderItem->GetPos();
RECT rt = pControl->GetPos();
rt.left = rtHeader.left;
rt.right = rtHeader.right;
pControl->SetPos(rt);
}
}
}