duilib 快速入门
duilib入门基础一 容器之 BOX、VBOX、HBOX布局及其子控件align排列方式
duilib入门基础二 HBOX容器内 子控件margin定位规则
duilib入门基础三 VBOX容器内 子控件margin定位规则
duilib入门基础四 BOX容器内 子控件margin定位规则
duilib入门基础 六 见见世面,duilib 基本控件 Button 能实现的界面一览
duilib入门基础 七 见见世面,duilib 基本控件 复选框 单选框
duilib入门基础 八 见见世面,duilib 基本控件 滑动条
duilib入门基础 九 见见世面,duilib 基本控件 组合框
duilib入门基础 十 见见世面,duilib 基本控件 选项框 OptionBox
动态添加BOX布局
Box* GlobalManager::CreateBoxWithCache(const std::wstring& strXmlPath, CreateControlCallback callback)
{
Box* box = nullptr;
auto it = m_builderMap.find(strXmlPath);
if (it == m_builderMap.end()) {
WindowBuilder* builder = new WindowBuilder();
box = builder->Create(strXmlPath.c_str(), callback);
if (box) {
m_builderMap[strXmlPath].reset(builder);
}
else {
ASSERT(FALSE);
}
}
else {
box = it->second->Create(callback);
}
return box;
}
void GlobalManager::FillBoxWithCache(Box* pUserDefinedBox, const std::wstring& strXmlPath, CreateControlCallback callback)
{
Box* box = nullptr;
auto it = m_builderMap.find(strXmlPath);
if (it == m_builderMap.end()) {
WindowBuilder* winBuilder = new WindowBuilder();
box = winBuilder->Create(strXmlPath.c_str(), callback, pUserDefinedBox->GetWindow(), nullptr, pUserDefinedBox);
if (box) {
m_builderMap[strXmlPath].reset(winBuilder);
}
else {
ASSERT(FALSE);
}
}
else {
box = it->second->Create(callback, pUserDefinedBox->GetWindow(), nullptr, pUserDefinedBox);
}
return;
}
1 列表项中,动态添加一布局项
列表子项XML布局
<?xml version="1.0" encoding="utf-8"?>
<Window>
<ListContainerElement class="xnw_listitem" width="stretch" height="180" bkcolor="xnw_client" >
<VBox>
<HBox name="storePushUrl" margin="0,0,0,0" childmargin="12" height="18">
<Control name="statusImg" width="auto" height="auto"/>
<Label name="live_status" text="正在直播..." font="7" normaltextcolor="xnw_yellow_text" />
<Label text="9:00--11:00" font="1" normaltextcolor="xnw_time_text" name="time" />
</HBox>
</VBox>
</ListContainerElement>
</Window>
使用 CreateBoxWithCache 或 FillBoxWithCache
示例:
ui::ListContainerElement* pItem = GetItemAt(0);
ui::ListContainerElement* pNewItem = NULL;
for (int nIndex = 1; nIndex < nCount; nIndex++){
pNewItem = new ui::ListContainerElement;
ui::GlobalManager::FillBoxWithCache(pNewItem, L"myClass/myclass_list_item.xml");
std::string itemName = MyString::n2str(nIndex);
pNewItem->SetUTF8Name(itemName); //设置item标识名字
SetItemDetailedInfo(pNewItem);
myClass_list->Add(pNewItem);
}
AddListItem(int nIndex){
ui::ListContainerElement* pItem = dynamic_cast<ui::ListContainerElement*>(ui::GlobalManager::CreateBoxWithCache(L"myClass/myclass_list_item.xml"));
myClass_list->Add(pItem);
std::string itemName = MyString::n2str(nIndex);
pItem->SetUTF8Name(itemName); //设置item标识名字
SetItemDetailedInfo(pItem);
}
//添加列表中
ui::ListContainerElement* pItem = dynamic_cast<ui::ListContainerElement*>(ui::GlobalManager::CreateBoxWithCache(L"myClass/myclass_list_item.xml"));
myClass_list->Add(pItem);
示例2 添加HBox布局
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<Window>
<HBox width="175" height="auto" padding="10,6,10,6" margin="0,0,10,10" bkimage="file='selected_user_bk.png' corner='2,2,2,2'">
<Label name="show_name" width="stretch" valign="center" font="1" normaltextcolor="dark2color" mouse="false"/>
<Button class="btn_wnd_close" name="delete" valign="center" margin="8"/>
</HBox>
</Window>
ui::HBox* InvokeChatForm::CreateSelectedListItem(const std::string& accid)
{
ui::HBox* container_element = (ui::HBox*)GlobalManager::CreateBoxWithCache(L"invokechat/user_photo.xml");
container_element->SetUTF8Name(accid);
container_element->SetUTF8DataID(accid);
Label* show_name_label = (Label*)container_element->FindSubControl(L"show_name");
show_name_label->SetText(UserService::GetInstance()->GetUserName(accid));
Button* btn_delete = (Button*)container_element->FindSubControl(L"delete");
btn_delete->AttachClick(nbase::Bind(&InvokeChatForm::OnBtnDeleteClick, this, accid, std::placeholders::_1));
return container_element;
}
ui::HBox* selected_listitem = CreateSelectedListItem(id);
selected_user_list_->Add(selected_listitem);
selected_user_list_->EndDown();