GacUI發(fā)布了一個(gè)新的Demo。這個(gè)Demo是關(guān)于多選框和單選框的。跟Windows一樣,直接創(chuàng)建出來(lái)的單選框其實(shí)是不會(huì)互斥的,除非你把他們放進(jìn)同一個(gè)group里面。界面是左右各一個(gè)group box,使用table來(lái)保證兩邊的尺寸都一樣大。每一個(gè)group box里面放三個(gè)按鈕,而且每一個(gè)group box的最小尺寸都取決于兩邊所有6按鈕中最長(zhǎng)的那個(gè)按鈕。每一邊的三個(gè)按鈕使用stack來(lái)排列成像一個(gè)列表一樣。左邊是多選框,右邊是單選框。現(xiàn)在先上圖:
第一張是剛打開(kāi)的時(shí)候,窗口的尺寸自動(dòng)變化到能顯示所有內(nèi)容的最小的尺寸。盡管因?yàn)槲淖值年P(guān)系,左邊的按鈕比右邊的短,但是table可以控制兩個(gè)group box一樣大,并且共享最小尺寸。

然后改變窗口的尺寸,按鈕始終靠左上角,兩個(gè)group box則保持一樣大。

大家已經(jīng)看了前面的三個(gè)demo,所以有些東西其實(shí)已經(jīng)不需要重復(fù)解釋了。先上代碼:
#include "..\..\Public\Source\GacUIIncludes.h"
#include <Windows.h>
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int CmdShow)
{
return SetupWindowsDirect2DRenderer();
}
class CheckAndRadioWindow : public GuiWindow
{
private:
GuiCellComposition* CreateButtons(const WString& groupName, const WString& buttonName, bool checkBox, GuiSelectableButton::GroupController* groupController)
{
GuiCellComposition* cell=new GuiCellComposition;
GuiControl* groupBox=g::NewGroupBox();
groupBox->GetBoundsComposition()->SetMinSizeLimitation(GuiGraphicsComposition::LimitToElementAndChildren);
groupBox->GetContainerComposition()->SetMinSizeLimitation(GuiGraphicsComposition::LimitToElementAndChildren);
// all child controls should at least 10 pixels away from the group box
groupBox->GetContainerComposition()->SetInternalMargin(Margin(10, 10, 10, 10));
// dock the group box to fill the cell
groupBox->GetBoundsComposition()->SetAlignmentToParent(Margin(0, 0, 0, 0));
groupBox->SetText(groupName);
// add the button to the cell
cell->AddChild(groupBox->GetBoundsComposition());
// create a stack to layout the 3 buttons from top to bottom shown like a list
GuiStackComposition* stack=new GuiStackComposition;
stack->SetMinSizeLimitation(GuiGraphicsComposition::LimitToElementAndChildren);
stack->SetDirection(GuiStackComposition::Vertical);
stack->SetAlignmentToParent(Margin(0, 0, 0, 0));
stack->SetPadding(6);
groupBox->GetContainerComposition()->AddChild(stack);
// create buttons
for(int i=0;i<3;i++)
{
GuiSelectableButton* button=checkBox?g::NewCheckBox():g::NewRadioButton();
button->SetText(buttonName+itow(i+1));
button->GetBoundsComposition()->SetAlignmentToParent(Margin(0, 0, 0, 0));
if(groupController)
{
button->SetGroupController(groupController);
}
GuiStackItemComposition* stackItem=new GuiStackItemComposition;
stack->AddChild(stackItem);
stackItem->AddChild(button->GetBoundsComposition());
}
return cell;
}
public:
CheckAndRadioWindow()
:GuiWindow(GetCurrentTheme()->CreateWindowStyle())
{
this->SetText(L"Controls.Button.CheckAndRadio");
// limit the size that the window should always show the whole content without cliping it
this->GetContainerComposition()->SetMinSizeLimitation(GuiGraphicsComposition::LimitToElementAndChildren);
// create a table to layout the 2 group boxes
GuiTableComposition* table=new GuiTableComposition;
// make the table to have 2 rows
table->SetRowsAndColumns(1, 2);
table->SetRowOption(0, GuiCellOption::MinSizeOption());
table->SetColumnOption(0, GuiCellOption::PercentageOption(0.5));
table->SetColumnOption(1, GuiCellOption::PercentageOption(0.5));
// dock the table to fill the window
table->SetAlignmentToParent(Margin(4, 4, 4, 4));
table->SetCellPadding(6);
// add the table to the window;
this->GetContainerComposition()->AddChild(table);
// add group box for check boxes
{
GuiCellComposition* cell=CreateButtons(L"Check Boxes", L"This is a check box ", true, 0);
table->AddChild(cell);
// this cell is the left cell
cell->SetSite(0, 0, 1, 1);
}
// add group box for radio buttons
{
// create a group controller to group those radio buttons together
// so that select a radio button will unselect the previous one automatically
GuiSelectableButton::GroupController* groupController=new GuiSelectableButton::MutexGroupController;
this->AddComponent(groupController);
GuiCellComposition* cell=CreateButtons(L"Radio buttons", L"This is a radio button ", false, groupController);
table->AddChild(cell);
// this cell is the right cell
cell->SetSite(0, 1, 1, 1);
}
// call this to calculate the size immediately if any indirect content in the table changes
// so that the window can calcaulte its correct size before calling the MoveToScreenCenter()
this->ForceCalculateSizeImmediately();
// move to the screen center
this->MoveToScreenCenter();
}
~CheckAndRadioWindow()
{
}
};
void GuiMain()
{
GuiWindow* window=new CheckAndRadioWindow();
GetApplication()->Run(window);
delete window;
}
需要關(guān)心的就是第二次調(diào)用CreateButtons函數(shù),用來(lái)構(gòu)造單選按鈕的時(shí)候,穿進(jìn)去的最后一個(gè)參數(shù)。GuiSelectableButton::GroupController類(lèi)是一個(gè)虛類(lèi),用來(lái)控制選中狀況。而預(yù)定義的MutexGroupController則可以控制連接到的所有GuiSelectionButton并保證他們互斥。如果需要更加復(fù)雜的情況,譬如說(shuō)“最多只能選中N個(gè)按鈕”這樣的,則自己集成一個(gè)group controller就可以了。在創(chuàng)建了一個(gè)group controller,要調(diào)用GuiWindow::AddComponent保持他的生命周期,然后使用GuiSelectableButton::SetGroupController來(lái)幫頂一個(gè)按鈕和一個(gè)group controller。
這個(gè)demo就介紹到這里了,下一個(gè)將是關(guān)于tab控件和文本框的demo。
posted on 2012-04-27 06:02
陳梓瀚(vczh) 閱讀(2255)
評(píng)論(25) 編輯 收藏 引用 所屬分類(lèi):
GacUI