Windows

Window Management

.NET

Glue42 Windows

In order for windows created from an external WPF/WinForms applications to become Glue42 Windows, these windows must first be registered via the .NET Window Management API.

Window Types

Two types of windows are supported for external applications - flat and tab.

Flat window:

Flat Window

Tab window:

Tab Window

Window Options

You can set several window configuration options during window registration. To create an instance with window options, use:

var glueWindowOptions = new GlueWindowOptions();

If the application is started by Glue42 Desktop, you can get any window startup options (regarding the application bounds, layout, etc.) by using:

var glueWindowOptions = glue.GlueWindows.GetStartupOptions() ?? new GlueWindowOptions();

Window Type

To set the type of the window:

// Create a "flat" window.
glueWindowOptions.WithType(GlueWindowType.Flat);

// Create a "tab" window.
glueWindowOptions.WithType(GlueWindowType.Tab);

Window Title

To set the title of the window:

glueWindowOptions.WithTitle("My Window");

Registering Windows

To register a window as a Glue42 Window, use the RegisterWindow() method of the .NET Window Management API by passing the window object as a first parameter and a window options object as a second:

await glue.GlueWindows.RegisterWindow(this, glueWindowOptions);

The RegisterWindow() method returns a window instance of type IGlueWindow.

WPF Windows

Note that it is mandatory for an external WPF application to have an app.manifest file with the following section:

<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware>
    </windowsSettings>
</application>

You can register a WPF window right after its creation. You can do this either in the window code or from an external component. To register a WPF window, use:

// Register the window by using `this` window as a first parameter.
await glue.GlueWindows.RegisterWindow(this, glueWindowOptions);

Full example:

// using Tick42;
// using Tick42.Windows;

// Get and set window options.
var glueWindowOptions = glue.GlueWindows.GetStartupOptions() ?? new GlueWindowOptions();

glueWindowOptions.WithType(GlueWindowType.Tab);

glueWindowOptions.WithTitle("My Window");

// Register the window.
await glue.GlueWindows.RegisterWindow(this, glueWindowOptions);

WPF windows are automatically unregistered when they are closed. If you want to manually unregister a window at a different point in time, use:

glueWindow.Unregister();

See our .NET WPF window registration example on GitHub.

WinForms Windows

You can register a WinForms window by using its handle:

// Get and set window options.
var glueWindowOptions = glue.GlueWindows.GetStartupOptions() ?? new GlueWindowOptions();

glueWindowOptions.WithType(GlueWindowType.Flat);

glueWindowOptions.WithTitle("My Window");

// Register the window by using its handle as a first parameter.
IGlueWindow glueWindow = await glue.GlueWindows.RegisterWindow(this.Handle, glueWindowOptions);

WinForms windows do not support automatic unregistration. You should explicitly call Unregister() when the window is closed:

glueWindow.Unregister();

Window Operations

Once an application window is registered, the Window Management API will accept full control over the window positioning, sizing and visibility. The application should not use native methods (for example, WPF/WinForms calls) to control the window as it will interfere with the Glue42 window management.

You can perform operations on the current window and on any other registered window.

Current Window

To get a reference to the current window, use:

var myWindow = await glue.GlueWindows.RegisterWindow(this, glueWindowOptions);

Title

To set the window title, use:

myWindow.Title = "New title";

Size and Position

To change the bounds of the window, use:

// Set the window bounds - left, top, width, height.
var newWindowBounds = new GlueWindowBounds(10, 10, 200, 200);

myWindow.Bounds = newWindowBounds; 

Visibility

To hide the window, use:

myWindow.IsVisible = false;

Handling Other Windows

The .NET Window Management API allows you to find all Glue42 Windows and manipulate them. To find all Glue42 Windows, use the GetGDWindows() method of the .NET Window Management API:

await glue.GlueWindows.GetGDWindows();

The GetGDWindows() method returns a window instance of type IGDWindow.

Finding Windows

To get a collection of all Glue42 windows, use:

var allWindows = await glue.GlueWindows.GetGDWindows();

To await your newly registered window to be published to the window collection, use:

var myWindow = await glue.GlueWindows.RegisterWindow(this, glueWindowOptions);

await glue.GlueWindows.AwaitWindow(window => window.Id == myWindow.Id);

// The collection will now contain `myWindow`.
var allWindows = await glue.GlueWindows.GetGDWindows();

To find a window by ID, use:

var targetWindowId = "29476_0";
var allWindows = await glue.GlueWindows.GetGDWindows();
var targetWindow = allWindows.FirstOrDefault(window => window.Descriptor.Id == targetWindowId);

To find a window by name, use:

var targetWindowName = "target-window";
var allWindows = await glue.GlueWindows.GetGDWindows();
var targetWindow = windows.FirstOrDefault(window => window.Descriptor.Name == targetWindowName);

Updating Windows

To manipulate any Glue42 Window, use the Update() method of the window instance.

Window Title

var allWindows = await glue.GlueWindows.GetGDWindows();
var newTitle = "New Title";
var targetWindow = allWindows.FirstOrDefault((window) => 
{
    return window.Descriptor.Name == targetWindow.Descriptor.Name;
});

await targetWindow.Update((window) =>
{
    window.SetTitle((titleOptions) => titleOptions.Title = newTitle);
});

Hiding a Window

await targetWindow.Update((window) =>
{
    window.Hide();
});

Showing a Window

await targetWindow.Update((window) =>
{
    window.Show();
});

Closing a Window

await targetWindow.Update((window) =>
{
    window.Close();
});

WPF Example

This is a minimalistic WPF example that registers its main window as a Glue42 window. You can clone the repo with our Glue42 .NET examples on GitHub (wpf-sw subfolder example).

App.xaml - initialize Glue42:

public partial class App : Application
{
    public static Glue42 Glue;

    protected override void OnStartup(StartupEventArgs e)
    {
        base.OnStartup(e);    

        Glue = new Glue42();
        Glue.Initialize("MyDemo");        
    }

MainWindow.xaml.cs - register the window:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var glueWindowOptions = App.Glue.GlueWindows.GetStartupOptions() ?? new GlueWindowOptions();
        glueWindowOptions.WithType(GlueWindowType.Flat);
        glueWindowOptions.WithTitle("Example Window");

        // Register the window. 
        App.Glue.GlueWindows.RegisterWindow(this, glueWindowOptions)
            .ContinueWith(r =>
            {
                IGlueWindow glueWindow = r.Result;
            });
    }
}