# Delphi

Source: https://docs.interop.io/desktop/capabilities/windows/window-management/delphi/index.html

## io.Connect Windows

> ℹ️ *See the [Delphi 10](https://github.com/InteropIO/native-examples/tree/main/glue-com/GlueDelphi) and [Delphi 7](https://github.com/InteropIO/native-examples/tree/main/glue-com/GlueDelphi7) examples on GitHub.*

In order for form windows of Delphi apps to become io.Connect Windows, they must be registered as io.Connect Windows after the io.Connect COM library has been initialized.

## Registering Delphi Forms

To enable registration as an io.Connect Window, the form class must implement the [`IGlueWindowEventHandler`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler) interface:

```delphi
TMainForm = class(TForm, IGlueWindowEventHandler)
...
private
  G42: IGlue42;
  // The io.Connect Window handle.
  glueWin: IGlueWindow;
protected
  function HandleWindowReady(const glueWindow: IGlueWindow): HResult; stdcall;
  function HandleChannelData(const glueWindow: IGlueWindow; const channelUpdate: IGlueContextUpdate): HResult; stdcall;
  function HandleChannelChanged(const glueWindow: IGlueWindow; const channel: IGlueContext; prevChannel: GlueContext): HResult; stdcall;
  function HandleWindowDestroyed(const glueWindow: IGlueWindow) : HResult; stdcall;
  function HandleWindowEvent(const GlueWindow: IGlueWindow; eventType: GlueWindowEventType; eventData: GlueValue): HResult; stdcall;
  ...
```

You can register the form as an io.Connect Window using the [`RegisterGlueWindow`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-iglue42-registergluewindow) method immediately after initializing io.Connect:

```delphi
procedure TMainForm.InitializeGlue;
  ...
    G42.Start(inst);
    // Register the io.Connect Window.
    G42.RegisterGlueWindow(Self.Handle, Self);
  ...
```

Unregister the io.Connect Window when the form is closed:

```delphi
procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  if Assigned(glueWin) then
  begin
    glueWin.Unregister;
  end;
  ...
end;
```

## Events

You must provide implementations for the methods of the [`IGlueWindowEventHandler`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler) interface. The following example demonstrates a minimal implementation of the event handlers:

```delphi
//  Handles the event when the io.Connect Window registration has completed.
function TMainForm.HandleWindowReady(const glueWindow: IGlueWindow): HResult;
begin
  glueWin := glueWindow;
  Result := S_OK;
end;

// Handles the event when the user changes the Channel via the Channel Selector in the window title bar.
function TMainForm.HandleChannelChanged(const glueWindow: IGlueWindow; const channel: IGlueContext; prevChannel: GlueContext): HResult;
begin
  Result := S_OK;
end;

// Handles the event when the data in the currently selected Channel has changed.
function TMainForm.HandleChannelData(const glueWindow: IGlueWindow; const channelUpdate: IGlueContextUpdate): HResult;
begin
  Result := S_OK;
end;

// Handles the event when the io.Connect Window is destroyed.
function TMainForm.HandleWindowDestroyed(const glueWindow: IGlueWindow): HResult;
begin
  Result := S_OK;
end;

// Handles additional window events.
function TMainForm.HandleWindowEvent(const GlueWindow: IGlueWindow; eventType: GlueWindowEventType; eventData: GlueValue): HResult;
begin
  Result := S_OK;
end;
```

> ℹ️ *For more information about using io.Connect Channels, see the [Channels](https://docs.interop.io/desktop/capabilities/data-sharing/channels/delphi/index.md) section.*

## Window Operations

Once an io.Connect Window has been registered, you can use the [`HandleWindowReady`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindoweventhandler-handlewindowready) event to perform operations on it.

### Title

To get the current window title, use the [`GetTitle`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-gettitle) method:

```delphi
var
  winTitle: WideString;
...
  winTitle := glueWin.GetTitle();
```

To change the window title, use the [`SetTitle`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-settitle) method:

```delphi
glueWin.SetTitle('New Window Title');
```

### Visibility

To check whether the window is visible, use [`IsVisible`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-isvisible). To hide or show a window, use [`SetVisible`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-setvisible) and pass a `WordBool` value as an argument:

```delphi
if glueWin.IsVisible() then
  glueWin.SetVisible(False)
else
  glueWin.SetVisible(True);
```

### Activation

You can activate the window by using the [`Activate`](https://docs.interop.io/desktop/getting-started/how-to/interop-enable-your-apps/delphi/index.md#interfaces-igluewindow-activate) method:

```delphi
glueWin.Activate();
```
