![]() |
OldSchoolHack GUI
|
Dieses Beispiel fasst nochmal die vorherigen Tutorials zum Erstellen von eigenen Formen zusammen und stellt eine neue Form vor. Die Form ist nochmal ausführlich kommentiert.
class SimpleForm : public Form //create a new form which inherits the Form class { private: Label *helloLabel; Button *countButton; Label *countLabel; void InitializeComponent() //this method initialize all controls { this->SetText("Simple Form"); //set form caption this->SetBounds(10, 10, 400, 300); //place form at (10/10) with 400x300px size helloLabel = new Label(); //create a new Label with parent = this helloLabel->SetName("helloLabel"); //set the name so you can use GetControlByName(...) helloLabel->SetText("Hello, I'm a Label. :)"); //set the text helloLabel->SetLocation(6, 6); //set the position relative to the form //no need to set the size because a label has autosize //to disable autosize use helloLabel->SetAutoSize(false); this->AddControl(helloLabel); //add helloLabel to the forms controllist countButton = new Button(); //create a new Button with parent = this countButton->SetName("countButton"); //set the name so you can use GetControlByName(...) countButton->SetText("press me"); //set the text v->SetLocation(5, 25); //set the position and use default button size //to set the size use SetSize or SetBounds or use autosize => SetAutoSize(true) countButton->GetClickEvent() += ClickEventHandler([this](Control *control) { //this method will be called every time you click the button countButton_Click(control); }); //add an onclickevent to the button using a lambdaexpression //if you don't like lambdas you can use this version: //countButton->GetClickEvent() += ClickEventHandler(std::bind(&SimpleForm::button_Click, this, std::placeholders::_1, std::placeholders::_2)); this->AddControl(countButton); countLabel = new Label(); //create a new Label with parent = this countLabel->SetName("countLabel"); //set the name so you can use GetControlByName(...) countLabel->SetText("Button pressed 0 times"); //set the text countLabel->SetLocation(6, 50); //set the position countLabel->SetTag(Misc::Any(0)); //set the tag. The tag can hold every datatype. We can use it to count the buttonclicks. this->AddControl(countLabel); } public: SimpleForm() { InitializeComponent(); //just call this method to initialize all controls } private: void countButton_Click(Control *sender) { //read old count value from tag Any tag = countLabel->GetTag(); int count = tag.CastTo<int>(); //and increase it ++count; //use the Strings::Format method to create a new string Misc::AnsiString test = Misc::String::Format("Button pressed %u times", count); //and set it as text countLabel->SetText(test); //set the new count as tag tag = count; countLabel->SetTag(tag); } };
1.7.5.1