duilib 之 动态添加BOX布局

 

 

动态添加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布局



    
      
        
            
                 
                 

        
    

 

使用 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::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::GlobalManager::CreateBoxWithCache(L"myClass/myclass_list_item.xml"));
				myClass_list->Add(pItem);

 

示例2 添加HBox布局

 



	
		

 

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

 

你可能感兴趣的:(duilib界面)