duilib 实现列表头任意拖动

时间:2022-05-17 10:15:23

1、表头(xml)

<List name="List_records" padding="5,10,5,5" bkcolor="#FFFFFFFF" inset="0,0,0,0" bordersize="1" bordercolor="#FFD7D7D7"
itemshowhtml="true" vscrollbar="true" hscrollbar="true" itemalign="left" itemaltbk="true"
itemshowrowline="true" itemshowcolumnline="true" itemlinecolor="#FFD7D7D7">
<ListHeader height="32" bkcolor="#FFE6ECF7" bordersize="1" bordercolor="#FFD7D7D7">
<ListHeaderItem text="采集人" name="es_header_name_dbclick" width="80" minwidth="80" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="手机号码" name="es_header_username_dbclick" width="100" minwidth="100" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="证件号码" name="es_header_username_dbclick" width="160" minwidth="160" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="采集时间" name="es_header_username_dbclick" width="150" minwidth="150" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="采集人2" name="es_header_username_dbclick" width="80" minwidth="80" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="现住地" name="es_header_username_dbclick" width="180" minwidth="150" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="现住地2" name="es_header_username_dbclick" width="150" minwidth="150" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="是否同步" name="es_header_username_dbclick" width="60" minwidth="60" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<ListHeaderItem text="" name="es_header_username_dbclick" width="40" minwidth="40" align="hcentervcenter" />
<Control width="1" bkcolor="#FFD7D7D7"/>
</ListHeader>
</List>

2、item(xml)

<Window>
<ListContainerElement name="ListContainerElement_statisQuery" height="32" >

<Text name="Text_susp_name" text="" padding="1,0,0,0" width="80"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_susp_mobile" text="" padding="1,0,0,0" width="80"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_susp_certNumber" text="" padding="1,0,0,0" width="130"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_susp_acquTime" text="" padding="1,0,0,0" width="130"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_gather_name" text="" padding="1,0,0,0" width="80"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_susp_address" text="" padding="1,0,0,0" width="150"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_susp_homeplace" text="" padding="1,0,0,0" width="150"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Text name="Text_is_up_server" text="" padding="1,0,0,0" width="60"
relativepos="0,0,100,0" mouse="false" textcolor="#FF333333" font="1" align="hleftvcenter" endellipsis="true" />
<Control width="1" bkcolor="#FFD7D7D7"/>
<Button name="btn_record_lookup" width="40" height="26" padding="0, 3, 0, 0" text="查看" font="7" align="hcentervcenter"/>
<Control width="1" bkcolor="#FFD7D7D7"/>

</ListContainerElement>
</Window>

ListContainerElement 中的第一级子控件与表头各列一一对应(标注:一定是第一级控件)。item中的width没有用处,以表头各列的宽度为准。

(3)代码中,向列表中添加节点

CDialogBuilder builder;
CListContainerElementUI* pListItem = (CListContainerElementUI*)(builder.Create(_T("xxxItem.xml"), (UINT)0));
if (pListItem != NULL)

{

  CTextUI* pText = static_cast<CTextUI*>(pListItem->FindSubControl(L"Text_susp_name"));
  pText->SetText(L"mark");

  .......

  pList->Add(pListItem);

}

(4)duilib的CList可能要修改

__super::SetPos(rc);
if (GetOwner()->GetListInfo()->nColumns > 0)
{

rc = m_rcItem;

// Adjust for inset
rc.left += m_rcInset.left;
rc.top += m_rcInset.top;
rc.right -= m_rcInset.right;
rc.bottom -= m_rcInset.bottom;

TListInfoUI *plistinfo = GetOwner()->GetListInfo();

// Determine the width of elements that are sizeable
SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };

for (int it2 = 0; it2 < m_items.GetSize(); it2++)
{
CControlUI* pControl = static_cast<CControlUI*>(m_items[it2]);
if (!pControl->IsVisible())
continue;
if (pControl->IsFloat())
{
SetFloatPos(it2);
continue;
}
RECT rcPadding = pControl->GetPadding();
SIZE sz = pControl->EstimateSize(szAvailable);

if (sz.cx < pControl->GetMinWidth())
sz.cx = pControl->GetMinWidth();
if (sz.cx > pControl->GetMaxWidth())
sz.cx = pControl->GetMaxWidth();

sz.cy = pControl->GetFixedHeight();
if (sz.cy == 0)
sz.cy = rc.bottom - rc.top - rcPadding.top - rcPadding.bottom;
if (sz.cy < 0)
sz.cy = 0;
if (sz.cy < pControl->GetMinHeight())
sz.cy = pControl->GetMinHeight();
if (sz.cy > pControl->GetMaxHeight())
sz.cy = pControl->GetMaxHeight();


RECT rcCtrl = { plistinfo->rcColumn[it2].left + rcPadding.left,
rc.top + rcPadding.top,
plistinfo->rcColumn[it2].right + rcPadding.left,
rc.top + sz.cy + rcPadding.top + rcPadding.bottom };
pControl->SetPos(rcCtrl);
}

}