Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

What's new in Windows Forms for .NET 9

Feedback

In this article

This article describes what's new in Windows Forms for .NET 9.

Async forms

Important

This feature set is experimental, except forControl.InvokeAsync.

Modern apps require asynchronous communication models. As Windows Forms has grown on .NET, more components require marshaling to anasync method to run on the UI-thread. For example, controls likeWebView2, native Windows 10 and Windows 11 APIs, or modern asynchronous libraries likeSemantic Kernel. Another scenario would be where you're sharing MVVM ViewModels built aroundasync with Windows Forms from other UI stacks such as WPF, WinUI, or .NET MAUI.

The following a list of new methods added to support asynchronous scenarios:

This API is guarded behind a compiler error because it's experimental. To suppress the error and enable access to the API, add the followingPropertyGroup to your project file:

<PropertyGroup>    <NoWarn>$(NoWarn);WFO5002</NoWarn></PropertyGroup>

Tip

For more information about how to suppress this rule, seeCompiler Error WFO5002.

BinaryFormatter no longer supported

BinaryFormatter is considered unsafe because it's vulnerable to deserialization attacks, which can lead to denial of service (DoS), information disclosure, or remote code execution. It was implemented before deserialization vulnerabilities were well understood, and its design doesn't follow modern security best practices.

Starting with .NET 9, its implementation has been removed to prevent these security risks. WhenBinaryFormatter is used, thePlatformNotSupportedException exception is thrown.

Windows Forms usedBinaryFormatter in many scenarios, such as when serializing data for clipboard and drag-and-drop operations, and most importantly, the Windows Forms Designer. Internally, Windows Forms continues to use a safer subset ofBinaryFormatter to handle specific use cases with a known set of types.

Windows Forms for .NET 9 is shipping with analyzers that help you identify times you unknowingly participate in binary serialization.

For more information aboutBinaryFormatter, seeWindows Forms migration guide for BinaryFormatter.

Dark mode

Important

This feature set is experimental.

Preliminary support for dark mode has been added to Windows Forms, with the goal of finalizing support in .NET 10. When the color mode changes, theSystemColors are changed to match. The color mode for the app can be set to one of the following values:

  • SystemColorMode.Classic—(default) Light mode, the same as previous versions of Windows Forms.
  • SystemColorMode.System—Respect the light or dark mode set by Windows.
  • SystemColorMode.Dark—Use dark mode.

To apply a color mode, callApplication.SetColorMode(SystemColorMode) in the program startup code:

namespace MyExampleProject;static class Program{    /// <summary>    ///  The main entry point for the application.    /// </summary>    [STAThread]    static void Main()    {        // To customize application configuration such as set high DPI settings or default font,        // see https://aka.ms/applicationconfiguration.        ApplicationConfiguration.Initialize();        Application.SetColorMode(SystemColorMode.Dark);        Application.Run(new Form1());    }    }
Friend Module Program    <STAThread()>    Friend Sub Main(args As String())        Application.SetHighDpiMode(HighDpiMode.SystemAware)        Application.EnableVisualStyles()        Application.SetCompatibleTextRenderingDefault(False)        Application.SetColorMode(SystemColorMode.Dark)        Application.Run(New Form1)    End SubEnd Module

This API is guarded behind a compiler error because it's experimental. To suppress the error and enable access to the API, add the followingPropertyGroup to your project file:

<PropertyGroup>    <NoWarn>$(NoWarn);WFO5001</NoWarn></PropertyGroup>

Tip

For more information about how to suppress this rule, seeCompiler Error WFO5001.

FolderBrowserDialog enhancements

FolderBrowserDialog now supports selecting multiple folders, which are stored in theSelectedPaths array. To enable multiple folders, setMultiselect totrue.

System.Drawing new features and enhancements

TheSystem.Drawing library has had many improvements, including wrapping GDI+ effects, support forReadOnlySpan, and better interop code generation.

System.Drawing supports GDI+ effects

TheSystem.Drawing library now supports GDI+ bitmap effects, such as blur and tint. Effects have been a part of GDI+, but weren't exposed throughSystem.Drawing until now.

Effects are applied to aBitmap by calling theBitmap.ApplyEffect(Effect, Rectangle) method. Provide the effect and an optionalRectangle for the area to apply the effect on. UseRectangle.Empty to process the entire image.

TheSystem.Drawing.Imaging.Effects namespace contains the effects you can apply:

System.Drawing supports Span

Many methods that accepted arrays have been enhanced to also acceptReadOnlySpan. For example, methods such asGraphicsPath.AddLines(ReadOnlySpan<Point>),Graphics.DrawLines(Pen, ReadOnlySpan<Point>), andDrawPolygon(Pen, ReadOnlySpan<Point>), accept an array orReadOnlySpan.

Use CsWin32 for interop

All interop code has been replaced byCsWin32, a C# P/Invoke source generator.

ToolStrip

The following improvements have been added to theToolStrip andToolStripItem controls.

  • A new property was added toToolStrip,AllowClickThrough.

    When set totrue, the control can be interacted with while the form is unfocused.

Back when .NET Core 3.1 was released, allMenu-related controls, such asMainMenu andMenuItem, were removed.ToolStrip andToolStripMenuItem should be used instead. However,ToolStripItem, the base class forToolStripMenuItem, didn't have a replacement for theMenuItem.Select event. This event was raised when the mouse or keyboard is used to highlight the item.

.NET 9 has addedToolStripItem.SelectedChanged, which can be used to detect when a menu item is highlighted.

Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, seeour contributor guide.

Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?