mxProject.WindowsFormGenericHost.NETFramework 1.0.0

dotnet add package mxProject.WindowsFormGenericHost.NETFramework --version 1.0.0                
NuGet\Install-Package mxProject.WindowsFormGenericHost.NETFramework -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="mxProject.WindowsFormGenericHost.NETFramework" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add mxProject.WindowsFormGenericHost.NETFramework --version 1.0.0                
#r "nuget: mxProject.WindowsFormGenericHost.NETFramework, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install mxProject.WindowsFormGenericHost.NETFramework as a Cake Addin
#addin nuget:?package=mxProject.WindowsFormGenericHost.NETFramework&version=1.0.0

// Install mxProject.WindowsFormGenericHost.NETFramework as a Cake Tool
#tool nuget:?package=mxProject.WindowsFormGenericHost.NETFramework&version=1.0.0                

mxProject.WindowsFormGenericHost

Overview

This is an extension library for Generic Host.

  • Runs a Windows Forms application on Generic Host.
  • Runs a task tray resident application.

Requirement

  • .NET 6 or .NET Framework 4.7.2+

Usage

Entry point implementation example

Instead of calling Application.Run method in the entry point, rewrite the application to run on Generic Host.

using Microsoft.Extensions.Hosting;
using mxProject.WindowFormHosting;

static void Main(string[] args)
{
#if NET6_0
    ApplicationConfiguration.Initialize();
#else
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
#endif

    // Application.Run(new Form1());
    RunWindowsFormApp(args);
}

MyAppContext class shown in the following example implementation is a class that implements mxProject.WindowFormHosting.IWindowsFormAppContext interface. If you want to use a specific type of context, define a context class that implements this interface.

CASE1 : Specify the main form type

Use AddWindowsFormApp<TForm> method.

/// <summary>
/// Runs this application.
/// </summary>
static void RunWindowsFormApp(string[] args)
{
    void OnStart(IWindowsFormAppContext context) { }
    void OnExit(IWindowsFormAppContext context) { }

    IHostBuilder builder = Host.CreateDefaultBuilder(args)
        .AddWindowsFormApp<Form1>(onStart: OnStart, onExit: OnExit)
        ;

    builder.Build().Run();
}

CASE2 : Specify the main form type and the context type

Use AddWindowsFormApp<TContext, TForm> method.

/// <summary>
/// Runs this application.
/// </summary>
static void RunWindowsFormApp(string[] args)
{
    void OnStart(MyAppContext context) { }
    void OnExit(MyAppContext context) { }

    IHostBuilder builder = Host.CreateDefaultBuilder(args)
        .AddWindowsFormApp<MyAppContext, Form1>(onStart: OnStart, onExit: OnExit);

    builder.Build().Run();
}

CASE3 : Specify the application information

Use AddWindowsFormApp(IWindowsFormAppInfo) method or AddWindowsFormApp<TContext>(IWindowsFormAppInfo<TContext>) method.

/// <summary>
/// Runs this application.
/// </summary>
static void RunWindowsFormApp(string[] args)
{
    IHostBuilder builder = Host.CreateDefaultBuilder(args)
        .AddWindowsFormApp(new MyWindowsFormAppInfo());

    builder.Build().Run();
}

internal class MyWindowsFormAppInfo : IWindowsFormAppInfo<MyAppContext>
{
    internal MyWindowsFormAppInfo() {}

    // It is possible to determine the type of the main form at runtime.
    public Type StartupObjectType => typeof(Form1)

    public void OnStart(MyAppContext context) {}
    public void OnExit(MyAppContext context) {}
}

CASE4 : Run as a task tray resident application.

Use AddTaskTrayApp<TNotifyIcon> method.

/// <summary>
/// Run as a task tray resident application.
/// </summary>
static void RunWindowsFormApp(string[] args)
{
    void OnStart(IWindowsFormAppContext context) { }
    void OnExit(IWindowsFormAppContext context) { }

    IHostBuilder builder = Host.CreateDefaultBuilder(args)
        .AddTaskTrayApp<MyNotifyIconProvider>(onStart: OnStart, onExit: OnExit);

    builder.Build().Run();
}

internal class MyNotifyIconProvider : INotifyIconProvider
{
    public MyNotifyIconProvider(IWindowsFormAppContext context)
    {
        m_Context = context;
    }

    private readonly IWindowsFormAppContext m_Context;

    public NotifyIcon CreateNotifyIcon()
    {
        return new NotifyIcon
        {
            Icon = new System.Drawing.Icon("sampleapp.ico");,
            ContextMenuStrip = ContextMenu(),
            Text = "Sample Application",
            Visible = true
        };
    }

    private ContextMenuStrip ContextMenu()
    {
        var menu = new ContextMenuStrip();

        menu.Items.Add("Exit", null, (sender, e) =>
        {
            Application.Exit();
        });

        return menu;
    }
}

CASE5 : Run as a task tray resident application. Specify the context type.

Use AddTaskTrayApp<TContext, TNotifyIcon> method.

/// <summary>
/// Run as a task tray resident application.
/// </summary>
static void RunWindowsFormApp(string[] args)
{
    void OnStart(MyAppContext context) { }
    void OnExit(MyAppContext context) { }

    IHostBuilder builder = Host.CreateDefaultBuilder(args)
        .AddTaskTrayApp<MyAppContext, MyNotifyIconProvider>(onStart: OnStart, onExit: OnExit);

    builder.Build().Run();
}

internal class MyNotifyIconProvider : INotifyIconProvider
{
    // An instance of MyAppContext class is injected.
    public MyNotifyIconProvider(MyAppContext context)
    {
        m_Context = context;
    }

    private readonly MyAppContext m_Context;

    // Omitted below
}

Licence

MIT

Product Compatible and additional computed target framework versions.
.NET Framework net472 is compatible.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 211 12/18/2022

Initial release.