Skip to content
dzamkov edited this page Jan 31, 2011 · 1 revision

After you set up a project and add OpenTKGUI as a reference, the quickest way to start using OpenTKGUI is to make a HostWindow. HostWindow's display and handle user input for a single control (which may have many child controls). The code for creating a HostWindow is as follows:

public static class MyOpenTKProgram
{
    // Program main entry point
    public static void Main()
    {
        HostWindow window = new HostWindow(delegate
        {
            // Create and return a control here
        }, "OpenTKGUI Host Window");
        window.Run();
    }
}

Now we need to create a control to display. One of the simplest and useful controls is Button, which does exactly what you'd expect. However, note that the control created for HostWindow will be stretched across the entire client area of the window. A button of that size will surely be unpleasant. That's why we need to wrap it in a Container, which is a control that can hold, resize and modify other controls. AlignContainer keeps a control at a specific size and aligns it in the area the container is given. By putting a button in a (rather large) AlignContainer, you can give it any size you want and center it. So, your first full GUI program should look like this:

public static class MyOpenTKProgram
{
    // Program main entry point
    public static void Main()
    {
        HostWindow window = new HostWindow(delegate
        {
            // Create a button
            Button button = new Button("I am a button");

            // Put it in an align container
            AlignContainer align = new AlignContainer(button, new Point(120, 30), Align.Center, Align.Center);
            return align;
        }, "OpenTKGUI Host Window");
        window.Run();
    }
}

Which, when run, should look like this

A button, in the middle of a white window

Note that you can avoid creating AlignContainer manually by just calling button.WithCenterAlign(new Point(120.0, 30.0));. Methods starting with With automatically wrap the control in a container.

Clone this wiki locally