«
DUILIB的List控件让表单数据列与表头宽度一致的方法

时间:2022-7-3    作者:范文泉    分类: 编程


DUILIB的List控件如下

    <List bkcolor="#FF5F646A" bordersize="1" bordercolor="#FF62676D" inset="0,0,0,0" itemshowhtml="true" vscrollbar="true" hscrollbar="true" itemalign="left" itemfont="1"  itemaltbk="true" itemhotbkcolor="#F05F646A" itemselectedbkcolor="#F85F646A">
                    <ListHeader height="38" bordersize="0,0,0,1" bordercolor="#FF62676D">
                        <ListHeaderItem text="创建时间" width="130" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="文件名称" width="140" align="left" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="创建人" width="70" align="center" textcolor="#FFFFffFF" />                   
                        <ListHeaderItem text="状态" width="85" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="打款结果" width="85" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="申请金额" width="75" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="打款金额" width="75" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="打款人数" width="65" align="center" textcolor="#FFFFffFF" />
                        <ListHeaderItem text="操作" width="145" align="center" textcolor="#FFFFffFF" />
                    </ListHeader>
                    <ListContainerElement height="42" bordersize="0,0,0,1" bordercolor="#FF62676D" font="1">                     
                        <Label text="2020-07-01 17:00" align="center" textcolor="#FFFFffFF"/>  
                        <Label text="7.1汇总.xls" align="left" textcolor="#FFFFffFF"/>
                        <Label text="管理员" align="center" textcolor="#FFFFffFF"/>            
                        <Label text="打款完毕" align="center" textcolor="#FFFFffFF"/>
                        <Label text="部分失败" align="center" textcolor="#FFFFffFF"/>
                        <Label text="1052.25" align="center" textcolor="#FFFFffFF"/>
                        <Label text="1052.25" align="center" textcolor="#FFFFffFF" />
                        <Label text="55" align="center" textcolor="#FFFFffFF"/>
                        <HorizontalLayout inset="25,0,25,0" >
                            <Label text="查看" textcolor="#FFFFffFF" />
                            <Label text="失败重打" textcolor="#FFFFffFF" />
                        </HorizontalLayout>
                    </ListContainerElement>
</List>

这样出来的表头的宽度与BODY的列宽是不一致,并且不能拖动,因此我们重写SePos就可以了。代码如下
void CListContainerElementUI::SetPos(RECT rc, bool bNeedInvalidate)
{
__super::SetPos(rc, bNeedInvalidate);

    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);
        }
    }
}

出来的效果图