This is an extension library for Generic Host.
- Runs a Windows Forms application on Generic Host.
- Runs a task tray resident application.
- .NET 6 or .NET Framework 4.7.2+
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.
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();
}
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();
}
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) {}
}
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;
}
}
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
}