好久没动duilib 了==
duilib 自带的默认属性,思想是不错的,但都知道比较难用,一不小心就所有替换,还来个莫名其妙。
<?xml version="1.0" encoding="utf-8"?>
<Window size="800,572" sizebox="4,4,6,6" roundcorner="5,5" caption="0,0,0,90" mininfo="800,570">
<Default name="VScrollBar" value="..."/>
<Default name="HScrollBar" value="..."/>
<VerticalLayout bkcolor="#FFD1E8F5" bkcolor2="#FFC6E0F1" bordercolor="#FF768D9B" bordersize="1" borderround="5,5">
<TitleCfg height="24"/> <!--标题栏-->
<HorizontalLayout bkcolor="0xFF00FF00" hscrollbar="true" vscrollbar="true">
<Button name="my_btn" pos="40,40,0, 0" text="my_btn" />
<Button name="my_btn2" pos="40,80,0, 0" text="my_btn2" />
<Button name="my_btn3" pos="40,120,0, 0" text="my_btn3"/>
<Button name="my_btn4" pos="40,160,0, 0" text="my_btn4"/>
</HorizontalLayout>
</VerticalLayout>
</Window>
效果是这样的
但是我想要整体效果,这么多button,就用Default吧,在xml添加如下
<Default name="Button" value="float="true" height="25" width="60" bkcolor="#FFF0F0F0" borderround="5,5" normalimage="button_nor.bmp" hotimage="button_down.bmp" focusedimage="button_over.bmp" "/>
但是看到的效果是
影响到了 标题栏 button 出现异常,收到default 影响,
于是就想,为啥不搞的自由点?
哈哈,,,
自己定义个属性列表属性
<?xml version="1.0" encoding="utf-8"?>
<Window size="800,572" sizebox="4,4,6,6" roundcorner="5,5" caption="0,0,0,90" mininfo="800,570">
<Default name="VScrollBar" value="..."/>
<Default name="HScrollBar" value="..."/>
<AttributeList name="Button_default" value="..."/>
<VerticalLayout bkcolor="#FFD1E8F5" bkcolor2="#FFC6E0F1" bordercolor="#FF768D9B" bordersize="1" borderround="5,5">
<TitleCfg height="24"/>
<HorizontalLayout bkcolor="0xFF00FF00" hscrollbar="true" vscrollbar="true">
<Button name="my_btn" pos="40,40,0, 0" text="my_btn" AttributeList="Button_default"/>
<Button name="my_btn2" pos="40,80,0, 0" text="my_btn2" AttributeList="Button_default" />
<Button name="my_btn3" pos="40,120,0, 0" text="my_btn3" AttributeList="Button_default"/>
<Button name="my_btn4" pos="40,160,0, 0" text="my_btn4" AttributeList="Button_default"/>
</HorizontalLayout>
</VerticalLayout>
</Window>
对应的在每个 Button 添加属性 AttributeList=”Button_default” 就可以使用上面的 ,
剩下的搞定 代码就ok。
1、在解析Default的地方 添加一个 解析AttributeList的默认属性
else if (_tcsicmp(pstrClass, _T("AttributeList")) == 0) {
nAttributes = node.GetAttributeCount();
LPCTSTR pControlName = NULL;
LPCTSTR pControlValue = NULL;
bool shared = false;
for (int i = 0; i < nAttributes; i++) {
pstrName = node.GetAttributeName(i);
pstrValue = node.GetAttributeValue(i);
if (_tcsicmp(pstrName, _T("name")) == 0) {
pControlName = pstrValue;
}
else if (_tcsicmp(pstrName, _T("value")) == 0) {
pControlValue = pstrValue;
}
else if (_tcsicmp(pstrName, _T("shared")) == 0) {
shared = (_tcsicmp(pstrValue, _T("true")) == 0);
}
}
if (pControlName) {
pManager->AddDefaultAttributeList(pControlName, pControlValue, shared);
}
}
2、防止再次解析
CControlUI* CDialogBuilder::_Parse(CMarkupNode* pRoot, CControlUI* pParent, CPaintManagerUI* pManager)
{
IContainerUI* pContainer = NULL;
CControlUI* pReturn = NULL;
for( CMarkupNode node = pRoot->GetChild() ; node.IsValid(); node = node.GetSibling() ) {
LPCTSTR pstrClass = node.GetName();
if( _tcsicmp(pstrClass, _T("Image")) == 0 || _tcsicmp(pstrClass, _T("Font")) == 0 \
|| _tcsicmp(pstrClass, _T("Default")) == 0
|| _tcsicmp(pstrClass, _T("MultiLanguage")) == 0
|| _tcsicmp(pstrClass, _T("AttributeList")) == 0) continue;
3、在控件设置 属性的时候,添加获取解析 这个 AtrributeList就OK了
// Process attributes
if( node.HasAttributes() ) {
// Set ordinary attributes
int nAttributes = node.GetAttributeCount();
for( int i = 0; i < nAttributes; i++ ) {
const CDuiString attribute_name = node.GetAttributeName(i);
if (attribute_name == _T("AttributeList"))
{
LPCTSTR pDefaultAttributes = pManager->GetDefaultAttributeList(node.GetAttributeValue(i));
if (pDefaultAttributes)
{
pControl->SetAttributeList(pDefaultAttributes);
}
}
else
{
pControl->SetAttribute(attribute_name, node.GetAttributeValue(i));
}
}
}
看下效果