Fork me on GitHub

.net6给winform带来的新功能

  首先简化了Program文件,引入了全局命名空间,但顶级语句由于Main函数的特性[STAThread]没有引用进来。

namespace WinFormsDemo
{
    internal static class Program
    {
        /// <summary>
        ///  The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            ApplicationConfiguration.Initialize();
            Application.Run(new frmMain());
        }
    }
}

ApplicationConfiguration.Initialize,其实是进行了一个封装,代码如下:

using System.Drawing;
using System.Runtime.CompilerServices;
using System.Windows.Forms;

namespace WinFormsDemo
{
    /// <summary>
    ///  Bootstrap the application configuration.
    /// </summary>
    [CompilerGenerated]
    internal static partial class ApplicationConfiguration
    {
        /// <summary>
        ///  Bootstrap the application as follows:
        ///  <code>
        ///  Application.EnableVisualStyles();
        ///  Application.SetCompatibleTextRenderingDefault(false);
        ///  Application.SetHighDpiMode(HighDpiMode.SystemAware);
        /// </code>
        /// </summary>
        public static void Initialize()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
        }
    }
}

再就是引入了全局字体设置,可以在Main引入,也可以在项目文件中配置:

[STAThread]
static void Main()
{
    ApplicationConfiguration.Initialize();
    Application.SetDefaultFont(new Font("汉仪篆书繁", 12));
    Application.Run(new frmMain());
}

或(但项目文件中配置发现不如代码中引入,有点变形,这里还需要完善)

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows</TargetFramework>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
    <ImplicitUsings>enable</ImplicitUsings>
    <ApplicationDefaultFont>汉仪篆书繁, 12pt</ApplicationDefaultFont>
  </PropertyGroup>
</Project>

效果如下:

 再有就是更好的支持高DPI,还有一些新的PAI和修改过的API,具体参见:https://docs.microsoft.com/zh-cn/dotnet/desktop/winforms/whats-new/net60?view=netdesktop-6.0

 

  想要更快更方便的了解相关知识,可以关注微信公众号 
 

 

 

 
posted @ 2022-05-11 20:29  桂素伟  阅读(581)  评论(0编辑  收藏  举报