3.9k views
in GUI Development by
How can I create and show GUI components dynamically as well as hide them again? What is needed in order to  create panels, menus or entire screens?

1 Answer

0 votes
by

Assuming you have a 'message box' GUI component developed and you wish to show it, then following code would create and make it visible:

  // create a new instance of your message panel
  var YourUnit::YourMessageBox box = new YourUnit::YourMessageBox;
  var Core::Root root = GetRoot();

  // ... initialize it (e.g. define position on the screen, 
  // or content of the message box, etc.) 
  box.Bounds.origin = <500,200>;  
  box.Message       = "There is a new message for you";

  // ... make it visible and route keyboard events to it
  root.Add( box, 0 );
  root.Focus = box;

In the code above note the usage of the 'root' object. The message panel is added directly to this root object. The root object represents the screen itself. It is the 'root' of the GUI tree. You can of course embed GUI components in other GUI components creating GUI applications with complex hierarchies. When adding a GUI component to other GUI component, the child components behave like parts of the parent component. When the parent is moved, the children are also moved. When the parent became invisible, the children are also hidden ... and so far.

The following code (executed by the message box itself) hides the box again:

  GetRoot().Remove( this ); // 'this' == the message box itself

When you want to hide the message box 'from the outside' (within code not belonging to the message box), you need to know the affected message box instance. This can be simply done by storing the message box instance in a variable you can evaluate later when the message box should disappear:

  // when creating the message box instance - remember it in a variable
  // the variable can be defined in the class where this code is implemented.
  MessageBox = box;

  ...

  // when discarding the box ...
  GetRoot().Remove( MessageBox );

  // release the reference to the instance, so it can be freed
  MessageBox = null;

These aspects are addressed in the chapter 'Compose the component programmatically'. Additionally the example 'Lightweight3D' contains three different 'screens' which change accordingly to the user interaction. Maybe it helps you to understand these aspects. You can also take a look at the methods Add() and Remove() implemented in the class Core::Group.

Sometimes you may need to create GUI components which capture temporarily all user inputs. If you are interesting in it, take a look at the methods BeginModal(), EndModal() and GetModalGroup() of the class Core::Root.

Embedded Wizard Website | Privacy Policy | Imprint

...