Skip to content

An open source library based on Microsoft WebView2 that can buid powerful WinForm applications with HTML, CSS and JavaScript easily.

License

Notifications You must be signed in to change notification settings

NetDimension/WinFormedge

 
 

Repository files navigation

The WinFormedge Project

This is a .NET library based on Microsoft WebView2 that can buid powerful WinForm applications with HTML, CSS and JavaScript easily.

WinFormedge

GitHub Nuget Nuget Build

点击[此处]切换到中文仓库页面。

✨ About

WinFormedge is an open-source .NET library built on Microsoft WebView2, enabling developers to create modern, visually appealing WinForms applications using HTML, CSS, and JavaScript. With WinFormedge, you can seamlessly integrate web technologies into your WinForm projects, allowing for rich and interactive user interfaces.

The inspiration behind WinFormedge comes from another project I maintain: NanUI. Like WinFormedge, NanUI lets developers build WinForms applications using web technologies. But unlike WinFormedge, it relies on the Chromium Embedded Framework (CEF).

PREVIEW

🖥️ Requirements

Development Environment:

  • Visual Studio 2022
  • .NET 8.0 or higher

Deployment Environment:

  • Windows 10 1903 or higher

This is a Windows ONLY library. It is not compatible with other operating systems.

The minimum supported operating system is Windows 10 version 1903 (May 2019 Update). And some fetures such as SystemBackdrop and SystemColorMode are only available on Windows 11.

🧩 Changelog

The changelog for the WinFormedge project is available in the CHANGELOG.md file. It contains a detailed list of changes, bug fixes, and new features added to the project. The changelog is updated regularly to reflect the latest changes in the project.

🔧 Installation

The WinFormedge library has now set up automated NuGet publishing on GitHub, so you can find the latest release of WinFormedge on NuGet. Simply search for and install the WinFormedge package using the NuGet Package Manager or any other NuGet management tool.

PM> Install-Package WinFormedge

🪄 Getting Started

First of all, you should create a WinForm Application by using default project template.

1. Replace initialization code by using WinFormedge application initialization procedure.

You should use FormedgeApp instead of Application class to initialize your WinForm application in the program.cs file. The FormedgeApp class is a builder for creating a WinFormedge application. It provides methods for configuring the application and running it.

using WinFormedge;

namespace MinimalExampleApp;

internal static class Program
{
    [STAThread]
    static void Main()
    {
        ApplicationConfiguration.Initialize();

        var app = FormedgeApp.CreateBuilder()
            .UseDevTools()
            .UseWinFormedgeApp<MyFormedgeApp>().Build();

        app.Run();
    }
}

When the FormedgeApp class is created, it will automatically initialize the WebView2 environment and run the message loop.

2. Create a AppStartup class.

The AppStartup class is the entry point of your WinFormedge application. It provides methods for configuring the application. You can override the OnApplicationLaunched method to perform any initialization tasks before the application starts.

And you must override the OnApplicationStartup method to create the main window of your application. If the OnApplicationStartup method returns values created by StartupOptions class, the FormedgeApp class will create the main window of your application. Otherwise if the OnApplicationStartup method returns null the application will be closed.

using WinFormedge;

namespace MinimalExampleApp;

internal class MyFormedgeApp : AppStartup
{
    protected override bool OnApplicationLaunched(string[] args)
    {

        return true;
    }
    protected override AppCreationAction? OnApplicationStartup(StartupOptions options)
    {
        return options.UseMainWindow(new MyWindow());
    }
}

You can do some staffs like User Login, User Settings, etc. in the OnApplicationStartup method to determine using which window to start the application. Andalso you can end the application by returning null if conditions are not met.

3. Create a MainWindow class.

The MyWindow class is the main window of your application. It inherits from the Formedge class. You can use the Formedge class to create a window with a WebView2 control.

using WinFormedge;
using Microsoft.Web.WebView2.Core;

namespace MinimalExampleApp;
internal class MyWindow : Formedge
{
    public MyWindow()
    {
        Load += MyWindow_Load;
        DOMContentLoaded += MyWindow_DOMContentLoaded;

        Url = "https://cn.bing.com";
    }

    private void MyWindow_Load(object? sender, EventArgs e)
    {
        // Window and WebView2 are ready to use here.
    }

    private void MyWindow_DOMContentLoaded(object? sender, CoreWebView2DOMContentLoadedEventArgs e)
    {
        // The DOM content is loaded and ready to use here.
        ExecuteScriptAsync(""""
(()=>{
const headerEl = document.querySelector("#hdr");
headerEl.style.appRegion="drag";
})();
        """");
    }

    protected override WindowSettings ConfigureHostWindowSettings(HostWindowBuilder opts)
    {
        // Configure the host window settings here.
        var win = opts.UseDefaultWindow();

        win.ExtendsContentIntoTitleBar = true;
        win.MinimumSize = new Size(960, 480);
        win.Size = new Size(1280, 800);
        win.SystemBackdropType = SystemBackdropType.MicaAlt;
        win.AllowFullScreen = true;

        return win;
    }
}

Codes above creates a Formedge window. By using Url property, you can set the initial URL of the window.

Default window properties can be set in the constructor. For special style properties of the window, you need to override the ConfigureHostWindowSettings method of the Formedge class and use its HostWindowBuilder parameter to determine which window style you will adopt, as well as configure the special styles that this window possesses. For example, in the sample code, by using the UseDefaultWindow method of the HostWindowBuilder parameter, you can instruct Formedge to create a default window and set its ExtendsContentIntoTitleBar property to achieve a borderless effect.

The Load event is raised when the window and WebView2 are ready to use. You can use this event to perform any initialization tasks that require the WebView2 control to be ready.

The DOMContentLoaded event is raised when the DOM content is loaded and ready to use. You can use this event to perform any tasks that require the DOM content to be loaded. As you can see in the example, you can use the ExecuteScriptAsync method to execute JavaScript code in the WebView2 control. The JavaScript code in the example sets the appRegion property of the header element to drag, which allows the user to drag the window by clicking and dragging the element on these rectangles.

4. Run the application.

You can run the application by pressing F5 in Visual Studio. The application will create a window with a WebView2 control that displays the Bing homepage.

PREVIEW

You can drag the window by clicking and dragging the header element of the page and resize the window by dragging the edges of the window.

📚 Documentation

The documentation for the WinFormedge project is not available yet. However, you can find the source code and examples in the src and examples folders. The source code is well-documented and provides examples of how to use the WinFormedge library right now.

🛠️ Contributing

Contributions are welcome! If you have any ideas, suggestions, or bug reports, please feel free to open an issue or submit a pull request.

📄 License

This project is licensed under the MIT License. See the LICENSE file for details.

🧭 Acknowledgements

About

An open source library based on Microsoft WebView2 that can buid powerful WinForm applications with HTML, CSS and JavaScript easily.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 85.4%
  • HTML 7.1%
  • JavaScript 5.9%
  • CSS 1.6%