Windows Forms (WinForms) for .NET

repository·main·Indexed 26 days ago

https://github.com/dotnet/winforms

A UI framework for building Windows desktop applications, providing a .NET wrapper over User32 and GDI+. This repository contains implementations for the .NET platform, including documentation on the out-of-process designer, project templates, accessibility mapping between AccessibleRole and ControlType, and guidance for migrating control libraries from .NET Framework to .NET.

Tokens
37.1K
Snippets
64
Records
175
Agent score
84%

What's inside dotnet-winforms

  1. Overview of Windows Forms (WinForms)

    main

    Windows Forms (WinForms) is a UI framework designed for building Windows desktop applications. It acts as a .NET wrapper over Windows user interface libraries, specifically User32 and GDI+.

    Key features include:

    • A productive visual designer in Visual Studio supporting drag-and-drop controls.
    • A focus on being a Rapid Application Tool for Windows-based apps.
    • Support for stable, monolithic Line of Business (LOB) applications.
    • Modernized support for HighDPI and Per-Monitor V2 scenarios.
  2. Accessibility behavior for MonthCalendar control

    main
    The MonthCalendar control provides UI Automation (UIA) support for its accessible objects and children. This ensures that the control and its constituent parts are accessible to users of tools like Inspect, Narrator, and Accessibility Insights. This documentation outlines the expected properties, accessibility tree structure, and actions for MonthCalendarAccessibleObject to ensure compliance with accessibility standards.
  3. Understand WinForms Designer Code-Behind Modernization

    main

    Starting with Visual Studio 2022 v17.5 Preview 3, the WinForms out-of-process designer has transitioned from using the COM-based CodeModel interface to using Roslyn for design-time serialization.

    Key Changes:

    • Performance: Roslyn enables multithreaded serialization/deserialization, reducing delays during designer load/unload.
    • Code Style: The generated InitializeComponent method now respects .editorconfig settings (e.g., removing this. or Me. qualifiers).
    • Implicit Usings: If <ImplicitUsings>enable</ImplicitUsings> is set in your project file, the designer will omit fully qualified namespace names in the generated code.
    • Code Churn: When upgrading to v17.5+, you may see a one-time change in your Form1.Designer.cs or Form1.Designer.vb files in source control. This is expected as the designer regenerates the code to match modern standards.
  4. Use System.Private.Windows.Core.GdiPlus for GDI+ interop

    main
    When performing GDI+ interop, functionality should primarily be implemented within System.Private.Windows.Core.GdiPlus. This namespace is designed to provide targeted GDI+ functionality (such as initialization and handle management) to allow other frameworks like WPF to access specific GDI+ features without requiring the full GDI+ implementation.
  5. Understand Shared WinForms and WPF Infrastructure

    main

    WinForms and WPF share certain low-level infrastructure components, primarily for clipboard and OLE (Object Linking and Embedding) operations. The most critical shared component is System.Private.Windows.Core.

    Key Implications:

    • Cross-Stack Impact: Changes made to System.Private.Windows.Core or other shared assemblies in the dotnet/winforms repository will affect both WinForms and WPF applications.
    • Runtime Behavior: At runtime, applications targeting the Windows Desktop shared framework (Microsoft.WindowsDesktop.App) share a single instance of System.Private.Windows.Core.dll, ensuring consistent clipboard and OLE behavior across both UI stacks.
  6. Expected accessibility behavior for ListView

    main
    The ListView control provides UI Automation support to ensure accessibility. The structure of the accessibility tree (visible via tools like Inspect) depends on the current View property of the ListView. The tree must contain all visible items based on the selected view mode, which includes: Details, LargeIcon, List, SmallIcon, and Tile views.
  7. Windows Desktop SDK (WPF) integration files

    main

    The sdk\dotnet-wpf folder contains props and targets used by the Microsoft.NET.Sdk.WindowsDesktop project. When this project is built, it copies files from the WinForms transport NuGet package into a Microsoft.NET.Sdk.WindowsDesktop bundle.

    Key files:

    • Microsoft.NET.Sdk.WindowsDesktop.WindowsForms.props: Contains Windows Forms specific configurations, such as default using imports.
    • Microsoft.NET.Sdk.WindowsDesktop.WindowsForms.targets: Contains Windows Forms specific build targets.
    • System.Windows.Forms.Analyzers.props: Contains properties required by WinForms source generators.
  8. Windows Desktop SDK integration files

    main

    The sdk\dotnet-windowsdesktop folder contains props and targets used to ingest WinForms assemblies into the Windows Desktop SDK. This process bundles assemblies into either the Microsoft.WindowsDesktop.App.Ref pack or the Microsoft.WindowsDesktop.App.Runtime pack.

    Key files:

    • System.Windows.Forms.FileClassification.props: Contains the manifest (list of assemblies) for the WindowsForms SDK.
  9. Debug Windows Forms by pointing your project to experimental binaries

    main

    If you do not want to modify your local SDK, you can add direct references to your experimental binaries within your project file (.csproj). This allows you to debug your changes without affecting the global system installation.

    Add an <ItemGroup> containing <Reference> elements pointing to the specific DLLs in your local build artifacts directory.

    <ItemGroup>
        <Reference Include="[Drive]:[Path-to-repo]\winforms\artifacts\bin\System.Windows.Forms\Debug\net9.0\System.Drawing.Common.dll" />
        <Reference Include="[Drive]:[Path-to-repo]\winforms\artifacts\bin\System.Windows.Forms\Debug\net9.0\System.Private.Windows.Core.dll" />
        <Reference Include="[Drive]:[Path-to-repo]\winforms\artifacts\bin\System.Windows.Forms\Debug\net9.0\System.Windows.Forms.dll" />
        <Reference Include="[Drive]:[Path-to-repo]\winforms\artifacts\bin\System.Windows.Forms\Primitives\Debug\net9.0\System.Windows.Forms.Primitives.dll" />
        <!-- Optionally you may need designer -->
        <Reference Include="[Drive]:[Path-to-repo]\winforms\artifacts\bin\System.Windows.Forms.Design\Debug\net9.0\System.Windows.Forms.Design.dll" />
    </ItemGroup>
  10. Use adaptive containers for .NET 11 VisualStyles

    main

    When using VisualStylesMode.Net11, controls may change their preferred size due to modern chrome, DPI, or text-scale changes. To handle these changes automatically, use TableLayoutPanel or FlowLayoutPanel instead of fixed-size containers. These containers automatically respond to localization, font changes, and AutoSize content updates.

    Note on Exceptions: RichTextBox and multiline TextBox do not support intrinsic single-line or IntegralHeight behavior. Their bounds will not resize automatically when the visual style changes; instead, their client area may shrink. For these controls, you should programmatically manage size and alignment within your workflow.