集成winform

This commit is contained in:
陈国伟 2021-10-25 17:37:34 +08:00
parent 33197ec838
commit 299e9d706e
23 changed files with 517 additions and 9 deletions

View File

@ -4,8 +4,7 @@
<Nullable>enable</Nullable>
<Description>github加速神器</Description>
<Copyright>https://github.com/dotnetcore/FastGithub</Copyright>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<TargetFramework>net6.0</TargetFramework>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<DisableImplicitNamespaceImports>true</DisableImplicitNamespaceImports>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
</PropertyGroup>

View File

@ -1,5 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />

View File

@ -1,5 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />

View File

@ -1,5 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Portable.BouncyCastle" Version="1.8.10" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

View File

@ -0,0 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
</Project>

View File

@ -2,7 +2,7 @@
using System.Reflection;
using System.Text.RegularExpressions;
namespace FastGithub
namespace FastGithub.Upgrade
{
/// <summary>
/// 表示产品版本

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<UseWindowsForms>true</UseWindowsForms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Extensions.Options" Version="5.0.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,47 @@
namespace FastGithub.Windows.Forms
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(11F, 24F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "FastGithub";
this.ResumeLayout(false);
}
#endregion
}
}

View File

@ -0,0 +1,20 @@
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;
namespace FastGithub.Windows.Forms
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<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>

View File

@ -0,0 +1,25 @@
using System.Windows.Forms;
namespace FastGithub.Windows.Hosting
{
/// <summary>
/// 表示Application选项
/// </summary>
public class ApplicationOptions
{
/// <summary>
/// 获取或设置是否启用VisualStyles
/// </summary>
public bool EnableVisualStyles { get; set; } = true;
/// <summary>
/// 获取或设置高Dpi的模式
/// </summary>
public HighDpiMode HighDpiMode { get; set; } = HighDpiMode.SystemAware;
/// <summary>
/// 获取或设置是否兼容TextRendering
/// </summary>
public bool CompatibleTextRenderingDefault { get; set; } = false;
}
}

View File

@ -0,0 +1,51 @@
using System;
using System.Threading;
namespace FastGithub.Windows.Hosting
{
/// <summary>
/// WinForm调度器
/// </summary>
public interface IWinFormDispatcher
{
/// <summary>
/// 获取或设置同步上下文
/// </summary>
SynchronizationContext? SynchronizationContext { get; set; }
/// <summary>
/// 尝试在同步上下文投递执行委托
/// </summary>
/// <param name="action"></param>
/// <returns></returns>
bool TryInvoke(Action action)
{
var context = this.SynchronizationContext;
if (context == null || action == null)
{
return false;
}
context.Post(state => ((Action)state!)(), action);
return false;
}
/// <summary>
/// 在同步上下文投递执行委托
/// </summary>
/// <param name="action"></param>
void Invoke(Action action)
{
var context = this.SynchronizationContext;
if (context == null)
{
throw new InvalidOperationException($"{nameof(SynchronizationContext)} is null");
}
if (action != null)
{
context.Post(state => ((Action)state!)(), action);
}
}
}
}

View File

@ -0,0 +1,15 @@
using System.Threading;
namespace FastGithub.Windows.Hosting
{
/// <summary>
/// WinForm调度器
/// </summary>
sealed class WinFormDispatcher : IWinFormDispatcher
{
/// <summary>
/// 获取或设置同步上下文
/// </summary>
public SynchronizationContext? SynchronizationContext { get; set; }
}
}

View File

@ -0,0 +1,63 @@
using FastGithub.Windows.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Windows.Forms;
namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// IHostBuilder的WinForm扩展
/// </summary>
public static class WinFormHostBuilderExtensions
{
/// <summary>
/// 指定WinForm的主窗体
/// </summary>
/// <remarks>
/// * 该方法需要在services.AddHostedService()之前调用
/// </remarks>
/// <typeparam name="TMainForm"></typeparam>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IHostBuilder UseWinForm<TMainForm>(this IHostBuilder hostBuilder) where TMainForm : Form
{
return hostBuilder.ConfigureServices((context, services) =>
{
services
.AddSingleton<TMainForm>()
.AddSingleton<IWinFormDispatcher, WinFormDispatcher>()
.AddHostedService<WinFormHostedService<TMainForm>>();
});
}
/// <summary>
/// 使用WinForm生命周期
/// </summary>
/// <remarks>
/// * 关闭主窗体或调用Appliaction.Exit()之后生命结束
/// </remarks>
/// <param name="hostBuilder"></param>
/// <returns></returns>
public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder)
{
return hostBuilder.UseWinFormHostLifetime(c => { });
}
/// <summary>
/// 使用WinForm生命周期
/// </summary>
/// <remarks>
/// * 关闭主窗体或调用Appliaction.Exit()之后生命结束
/// </remarks>
/// <param name="hostBuilder"></param>
/// <param name="configureOptions">Applicaiton选项</param>
public static IHostBuilder UseWinFormHostLifetime(this IHostBuilder hostBuilder, Action<ApplicationOptions> configureOptions)
{
return hostBuilder.ConfigureServices((context, services) =>
{
services.Configure(configureOptions);
services.AddSingleton<IHostLifetime, WinFormHostLifetime>();
});
}
}
}

View File

@ -0,0 +1,55 @@
using FastGithub.Windows.Hosting;
using Microsoft.Extensions.Options;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// WinForm生命周期
/// </summary>
sealed class WinFormHostLifetime : IHostLifetime, IDisposable
{
private readonly IHostApplicationLifetime applicationLifetime;
private readonly IOptions<ApplicationOptions> applicationOptions;
public WinFormHostLifetime(IHostApplicationLifetime applicationLifetime, IOptions<ApplicationOptions> applicationOptions)
{
this.applicationLifetime = applicationLifetime;
this.applicationOptions = applicationOptions;
}
public Task WaitForStartAsync(CancellationToken cancellationToken)
{
var option = this.applicationOptions.Value;
if (option.EnableVisualStyles == true)
{
Application.EnableVisualStyles();
}
Application.SetHighDpiMode(option.HighDpiMode);
Application.SetCompatibleTextRenderingDefault(option.CompatibleTextRenderingDefault);
Application.ApplicationExit += OnApplicationExit;
return Task.CompletedTask;
}
private void OnApplicationExit(object? sender, System.EventArgs e)
{
Application.ApplicationExit -= OnApplicationExit;
this.applicationLifetime.StopApplication();
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public void Dispose()
{
Application.ApplicationExit -= OnApplicationExit;
}
}
}

View File

@ -0,0 +1,88 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FastGithub.Windows.Hosting
{
/// <summary>
/// WinForm后台任务和WinForm线程
/// </summary>
/// <typeparam name="TMainForm"></typeparam>
sealed class WinFormHostedService<TMainForm> : IHostedService where TMainForm : Form
{
private readonly Thread staThread;
private readonly IServiceProvider serviceProvider;
private readonly TaskCompletionSource taskCompletionSource = new();
/// <summary>
/// WinForm后台任务
/// </summary>
/// <param name="serviceProvider"></param>
public WinFormHostedService(IServiceProvider serviceProvider)
{
this.serviceProvider = serviceProvider;
this.staThread = new Thread(StaRunMainFrom);
this.staThread.TrySetApartmentState(ApartmentState.STA);
}
/// <summary>
/// 启动
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task StartAsync(CancellationToken cancellationToken)
{
this.staThread.Start();
return this.taskCompletionSource.Task;
}
/// <summary>
/// STA线程
/// </summary>
private void StaRunMainFrom()
{
try
{
var mainForm = this.CreateMainForm();
this.taskCompletionSource.TrySetResult();
Application.Run(mainForm);
}
catch (Exception ex)
{
this.taskCompletionSource.TrySetException(ex);
}
}
/// <summary>
/// 实例化MainForm与初始化调度器
/// </summary>
/// <exception cref="InvalidOperationException"></exception>
private TMainForm CreateMainForm()
{
// 在STA线程实例化TMainForm保证该线程拥有SynchronizationContext
var mainForm = this.serviceProvider.GetRequiredService<TMainForm>();
if (SynchronizationContext.Current is null)
{
throw new InvalidOperationException($"不允许在其它线程上实例化{typeof(TMainForm)}");
}
var dispatcher = this.serviceProvider.GetRequiredService<IWinFormDispatcher>();
dispatcher.SynchronizationContext = SynchronizationContext.Current;
return mainForm;
}
/// <summary>
/// 停止
/// </summary>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task StopAsync(CancellationToken cancellationToken)
{
Application.Exit();
return Task.CompletedTask;
}
}
}

View File

@ -0,0 +1,22 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
namespace FastGithub
{
/// <summary>
/// 服务注册扩展
/// </summary>
public static class ServiceCollectionExtensions
{
/// <summary>
/// 添加配置服务
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddWinForm(this IServiceCollection services)
{
return services;
}
}
}

View File

@ -15,6 +15,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.HttpServer", "Fa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.PacketIntercept", "FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj", "{701FF90C-E651-4E0B-AE7F-84D1F17DD178}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FastGithub.Windows", "FastGithub.Windows\FastGithub.Windows.csproj", "{4F9914B7-2519-46F9-96F1-42400B6BFD6E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FastGithub.Upgrade", "FastGithub.Upgrade\FastGithub.Upgrade.csproj", "{D644B53B-91E4-41DD-818B-FE3A77ED8D10}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -45,6 +49,14 @@ Global
{701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Debug|Any CPU.Build.0 = Debug|Any CPU
{701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Release|Any CPU.ActiveCfg = Release|Any CPU
{701FF90C-E651-4E0B-AE7F-84D1F17DD178}.Release|Any CPU.Build.0 = Release|Any CPU
{4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4F9914B7-2519-46F9-96F1-42400B6BFD6E}.Release|Any CPU.Build.0 = Release|Any CPU
{D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D644B53B-91E4-41DD-818B-FE3A77ED8D10}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -1,4 +1,5 @@
using FastGithub.Configuration;
using FastGithub.Upgrade;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

View File

@ -1,6 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0;net6.0-windows</TargetFrameworks>
<AssemblyName>fastgithub</AssemblyName>
<OutputType>Exe</OutputType>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
@ -9,12 +10,26 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
<UseWindowsForms>true</UseWindowsForms>
<OutputType>WinExe</OutputType>
</PropertyGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-windows'">
<ProjectReference Include="..\FastGithub.Windows\FastGithub.Windows.csproj" />
</ItemGroup>
<ItemGroup>
<FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="PInvoke.AdvApi32" Version="0.7.104" />
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
<ProjectReference Include="..\FastGithub.DomainResolve\FastGithub.DomainResolve.csproj" />
<ProjectReference Include="..\FastGithub.HttpServer\FastGithub.HttpServer.csproj" />
<ProjectReference Include="..\FastGithub.PacketIntercept\FastGithub.PacketIntercept.csproj" />
<ProjectReference Include="..\FastGithub.Upgrade\FastGithub.Upgrade.csproj" />
</ItemGroup>
<ItemGroup>

View File

@ -27,7 +27,11 @@ namespace FastGithub
{
return Host
.CreateDefaultBuilder(args)
#if WINDOWS
.UseWindowsService()
.UseWinForm<Windows.Forms.MainForm>()
.UseWinFormHostLifetime()
#endif
.UseDefaultServiceProvider(c =>
{
c.ValidateOnBuild = false;