Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Samples/Islands/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,38 @@ DispatcherQueueController on a dedicated rendering thread. (main.cpp)
* Press Ctrl+Shift+B, or select **Build** \> **Build Solution**.
* Press Ctrl+F5 to launch the app without attaching a debugger.
* Press F5 to launch the app under a debugger.

# WinForms Island App (cs-winforms-unpackaged)

The cs-winforms-unpackaged directory has a sample shows how to add a WinAppSDK island with Xaml content to a WinForms app.
It was first created with the C# "Windows Forms App" template in Visual Studio, which yields a boilerplate WinForms app.

This sample is an "unpackaged" app, so it will run like a WinForms/Win32 app does when built from the default templates.

This sample uses Windows App SDK as a "framework package". This means that the Windows App SDK runtime must be installed for it to run.

## Prerequisites

* See [System requirements for Windows app development](https://docs.microsoft.com/windows/apps/windows-app-sdk/system-requirements).
* Make sure that your development environment is set up correctly—see [Install tools for developing apps for Windows 10 and Windows 11](https://docs.microsoft.com/windows/apps/windows-app-sdk/set-up-your-development-environment).

## Building and running the WinForms Island sample

* Open the solution file (`.sln`) in Visual Studio.
* Press Ctrl+Shift+B, or select **Build** \> **Build Solution**.
* Press Ctrl+F5 to launch the app (without attaching a debugger)
> Note: If the Windows App SDK runtime isn't installed on the machine, the user will see a message box directing them to a download link.
* Press F5 to launch the app under a debugger.
* To run from the command line or File Explorer, navigate to `bin/<arch>/<config>/net6.0-windows10.0.17763.0` directory and run WinFormsWithIslandApp.exe.
* To deploy to another machine, copy the `bin/<arch>/<config>/net6.0-windows10.0.17763.0` directory to that machine and run WinFormsWithIslandApp.exe. The sample
runs on Windows version 17763 and later.

In Visual Studio, you should also be able to see the DesktopWindowXamlSourceControl in the designer:

![alt text](img/designer.png)

When you launch the app, it should look like this:

![alt text](img/screenshot.png)


Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Windows.Foundation;

namespace WinFormsWithIsland
{
/// <summary>
/// This control hosts a DesktopWindowXamlSource in a WinForms application.
/// The DesktopWindowXamlSource is backed by a DesktopChildSiteBridge HWND that is a child of the WinForms Control.
/// The HWND tree looks like this:
/// * Top-level WinForms HWND
/// * DesktopWindowXamlSourceControl HWND
/// * DesktopChildSiteBridge HWND
/// * Other WinForms control HWNDs
/// * Other WinForms control HWNDs
/// </summary>
public partial class DesktopWindowXamlSourceControl : System.Windows.Forms.Control
{
public DesktopWindowXamlSourceControl()
{
InitializeComponent();
}

protected override void OnGotFocus(EventArgs e)
{
if (_desktopWindowXamlSource != null)
{
// The DesktopWindowXamlSourceControl is getting focus, let's redirect focus to the Xaml content by
// calling DesktopWindowXamlSource.NavigateFocus.
bool isShiftPressed = ((Form.ModifierKeys & Keys.Shift) != 0);
var reason = isShiftPressed ? Microsoft.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.Last : Microsoft.UI.Xaml.Hosting.XamlSourceFocusNavigationReason.First;
var request = new XamlSourceFocusNavigationRequest(reason);
_desktopWindowXamlSource.NavigateFocus(request);
}
}

private void OnDesktopWindowXamlSourceTakeFocusRequested(object sender, DesktopWindowXamlSourceTakeFocusRequestedEventArgs e)
{
// The DesktopWindowXamlSource is requesting that the host take focus.
// This typically happens when the user is tabbing through the controls in the Xaml content and reaches the first or last control.
if (e.Request.Reason == XamlSourceFocusNavigationReason.First)
{
FocusNextFocusableWinFormsControl(this.Parent!, this, true /*forward*/);
}
else if (e.Request.Reason == XamlSourceFocusNavigationReason.Last)
{
FocusNextFocusableWinFormsControl(this.Parent!, this, false /*forward*/);
}
}

private void InitializeDesktopWindowXamlSource()
{
_desktopWindowXamlSource = new Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource();

_desktopWindowXamlSource.Initialize(new WindowId((ulong)this.Handle));

_desktopWindowXamlSource.TakeFocusRequested +=
new TypedEventHandler<DesktopWindowXamlSource, DesktopWindowXamlSourceTakeFocusRequestedEventArgs>(
OnDesktopWindowXamlSourceTakeFocusRequested);

_desktopWindowXamlSource.SiteBridge.MoveAndResize(new Windows.Graphics.RectInt32(0, 0, this.Width, this.Height));

_frame = new Frame();
_desktopWindowXamlSource.Content = _frame;

_desktopWindowXamlSource.SystemBackdrop = new DesktopAcrylicBackdrop();

if (_content != null)
{
_frame.Content = _content;
}
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
if (_desktopWindowXamlSource != null)
{
// Resize the DesktopChildSiteBridge HWND to match the size of the WinForms control, it's parent.
_desktopWindowXamlSource.SiteBridge.MoveAndResize(new Windows.Graphics.RectInt32(0, 0, this.Width, this.Height));
}
}

protected override void OnPaint(PaintEventArgs pe)
{
if (this.DesignMode)
{
base.OnPaint(pe);
pe.Graphics.FillRectangle(Brushes.LightGray, this.ClientRectangle);
pe.Graphics.DrawString("DesktopWindowXamlSourceControl", this.Font, Brushes.Black, 10, 10);
}
else if (_desktopWindowXamlSource == null)
{
InitializeDesktopWindowXamlSource();
}
}

/// <summary>
/// Sets the content of the DesktopWindowXamlSource.
/// </summary>
public FrameworkElement? Content
{
get
{
return _content;
}
set
{
_content = value;
if (_frame != null)
{
_frame.Content = _content;
}
}
}

/// <summary>
/// Call to move focus to the next focusable control in the parent.
/// </summary>
/// <param name="parent">Control or Form that contains the controls.</param>
/// <param name="start">Control we're starting with.</param>
/// <param name="forward">If true, focus is moving forward. If not, backward.</param>
private static void FocusNextFocusableWinFormsControl(System.Windows.Forms.Control parent, System.Windows.Forms.Control start, bool forward)
{
// GetNextControl can return controls that aren't tab stops, so keep going until we find one that is.
System.Windows.Forms.Control? next = start;
do
{
next = parent.GetNextControl(next, forward);
}
while (next != null && next.TabStop == false);

if (next == null)
{
// Oops, we ran out of controls. Get the first control in the parent.
next = parent.GetNextControl(null, forward);
}

if (next != null)
{
next.Focus();
}
}

private FrameworkElement? _content;
private Frame? _frame;
private DesktopWindowXamlSource? _desktopWindowXamlSource;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
Loading