转载请说明原出处,谢谢~~:http://blog.csdn.net/zhuhongshu/article/details/42503147
最近项目里需要用到包含表头列表,而窗体大小改变后,每个列表项的宽度不会自动改变,这样窗体变宽后列表就出现了一大片空白,非常难看。所以给列表头增加了属性,可以控制让每个列表项的宽度根据百分比来计算。这样再配合我之前写的对List控件的增强代码,就能让列表项的每列按照百分比控制宽度。
分别要给CListHeaderUI类和CListHeaderItemUI类增加函数和属性。
给CListHeaderUI增加scaleheader属性,只有scaleheader为真时才开启百分比功能,否则还是根据width属性来控制宽度
<Attribute name="scaleheader" default="false" type="BOOL" comment="每个表头的宽度是否按照百分比来设置"/>
给CListHeaderItemUI增加scale属性,设置本子表头的宽度百分比
<Attribute name="scale" default="0" type="INT" comment="设置子表头所占总表头的百分比宽度,如40(代表占40%的宽度)"/>
给CListHeaderItemUI增加属性的代码很简单,我就不贴了。给CListHeaderUI控件增加SetPos成员函数,写入如下代码,加入百分比功能。
void CListHeaderUI::SetPos(RECT rc)
{
CControlUI::SetPos(rc);
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;
if( m_items.GetSize() == 0) {
return;
}
// Determine the width of elements that are sizeable
SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
int nAdjustables = 0;
int cxFixed = 0;
int nEstimateNum = 0;
for( int it1 = 0; it1 < m_items.GetSize(); it1++ ) {
CControlUI* pControl = static_cast<CControlUI*>(m_items[it1]);
if( !pControl->IsVisible() ) continue;
if( pControl->IsFloat() ) continue;
SIZE sz = pControl->EstimateSize(szAvailable);
if( sz.cx == 0 ) {
nAdjustables++;
}
else {
if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
}
cxFixed += sz.cx + pControl->GetPadding().left + pControl->GetPadding().right;
nEstimateNum++;
}
cxFixed += (nEstimateNum - 1) * m_iChildPadding;
int cxExpand = 0;
int cxNeeded = 0;
if( nAdjustables > 0 ) cxExpand = MAX(0, (szAvailable.cx - cxFixed) / nAdjustables);
// Position the elements
SIZE szRemaining = szAvailable;
int iPosX = rc.left;
int iAdjustable = 0;
int cxFixedRemaining = cxFixed;
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();
szRemaining.cx -= rcPadding.left;
SIZE sz = {0,0};
if (m_bIsScaleHeader)
{
CListHeaderItemUI* pHeaderItem = static_cast<CListHeaderItemUI*>(pControl);
sz.cx = int(GetWidth() * (float)pHeaderItem->GetScale() / 100);
}
else
{
sz = pControl->EstimateSize(szRemaining);
}
if( sz.cx == 0 ) {
iAdjustable++;
sz.cx = cxExpand;
// Distribute remaining to last element (usually round-off left-overs)
if( iAdjustable == nAdjustables ) {
sz.cx = MAX(0, szRemaining.cx - rcPadding.right - cxFixedRemaining);
}
if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
}
else {
if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();
cxFixedRemaining -= sz.cx;
}
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 = { iPosX + rcPadding.left, rc.top + rcPadding.top, iPosX + sz.cx + rcPadding.left + rcPadding.right, rc.top + rcPadding.top + sz.cy};
pControl->SetPos(rcCtrl);
iPosX += sz.cx + m_iChildPadding + rcPadding.left + rcPadding.right;
cxNeeded += sz.cx + rcPadding.left + rcPadding.right;
szRemaining.cx -= sz.cx + m_iChildPadding + rcPadding.right;
}
cxNeeded += (nEstimateNum - 1) * m_iChildPadding;
}
总结:
完整的代码可以在我的库里下载到:点击打开链接
Redrain 2015.1.7