DarkNet-WPF
1.0.0
This package was renamed to DarkNet for version 2 because there are no longer separate packages for WPF and Windows Forms. The API also changed to be more organized.
dotnet add package DarkNet-WPF --version 1.0.0
NuGet\Install-Package DarkNet-WPF -Version 1.0.0
<PackageReference Include="DarkNet-WPF" Version="1.0.0" />
paket add DarkNet-WPF --version 1.0.0
#r "nuget: DarkNet-WPF, 1.0.0"
// Install DarkNet-WPF as a Cake Addin #addin nuget:?package=DarkNet-WPF&version=1.0.0 // Install DarkNet-WPF as a Cake Tool #tool nuget:?package=DarkNet-WPF&version=1.0.0
This is version 1 of the DarkNet library. For version 2 and beyond, use the DarkNet package. It was renamed because there are no longer separate packages for WPF and Windows Forms.
This is a .NET library that you can use to enable Windows 10's dark mode for your application's title bars and system context menus, similar to the dark title bar in Command Prompt.
Requirements
- .NET Framework 4.7.2 or later, or .NET Core 3.1 or later
- Windows 10 version 1809 (October 2018 Update) or later for dark mode to work
- You can run your app on earlier Windows versions, but the title bar won't turn dark
- WPF or Windows Forms
Installation
There are two packages available, one for WPF and one for Windows Forms. You can install the one that corresponds to the GUI technology that your program uses.
WPF
dotnet add package DarkNet-WPF
Windows Forms
dotnet add package DarkNet-Forms
Usage
You must do both of the following steps.
On application startup
Before showing any windows in your application, you must call
darknet.wpf.DarkNet.SetDarkModeAllowedForProcess(true);
A good place to call this is in an event handler for your Application's Startup
event.
By default, WPF apps have an App.xaml
file that specifies the first Window
to show in the StartupUri
attribute:
<Application StartupUri="MainWindow.xaml">
This does not allow you to run custom code before showing the window, so remove StartupUri
and add Startup
, which specifies an event handler method.
<Application Startup="App_OnStartup">
In App.xaml.cs
, create a method to handle this event and call the aforementioned DarkNet method.
public partial class App: Application {
private void App_OnStartup(object sender, StartupEventArgs e) {
DarkNet.SetDarkModeAllowedForProcess(true);
// Incomplete example; see below for complete example including showing your first window.
}
}
Before showing a new window
Before showing each window in your application, you have to enable dark mode for that window.
darknet.wpf.DarkNet.SetDarkModeAllowedForWindow(window, true);
You must do this when the SourceInitialized
event is fired for the window you are about to show. If you do it before this (such as right before calling the constructor), the Win32 HWND will not exist yet, so the native DLL calls will fail. If you call it too late (such as after the window is shown), the DLL calls will have no effect on Windows.
Add an event handler for the SourceInitialized
event and call the aforementioned DarkNet method.
window.SourceInitialized += (_, _) => { DarkNet.SetDarkModeAllowedForWindow(window, true); };
You must also perform this step for all subsequent windows you show in your application, not just the first window.
Complete example
<Application x:Class="my_namespace.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="App_OnStartup">
</Application>
public partial class App: Application {
private void App_OnStartup(object sender, StartupEventArgs e) {
DarkNet.SetDarkModeAllowedForProcess(true);
Window window = new MainWindow();
window.SourceInitialized += (_, _) => { DarkNet.SetDarkModeAllowedForWindow(window, true); };
window.Show();
}
}
Demo
Download and run darknet-demo-wpf.exe
from the latest release.
You can also clone this repository and build the darknet-demo-wpf
project yourself using Visual Studio Community 2019.
Limitations
- This library currently requires a fairly recent version of Windows and the .NET runtime. This could be relaxed in a future version to allow inclusion in applications that run in a wider range of environments.
- This library currently does not expose whether the active Windows app mode is set to Dark or Light. This may be possible to add in a future version to allow you to implement a "follow Windows app mode" strategy in your application.
- This library only changes the theme of the title bar/window chrome/non-client area, as well as the system context menu (the menu that appears when you right click on the title bar, or left click on the title bar icon, or hit
Alt
+Space
). It does not change the theme of the client area of your window. It is up to you to make that look different when dark mode is enabled. - This library currently does not help you persist a user choice for the mode they want your application to use. You can expose an option and persist that yourself, then pass the desired value to the methods in this library (e.g. call
DarkNet.SetDarkModeAllowedForProcess(false)
for light mode, or just don't call it at all).
Acknowledgements
- Milan Burda for explaining how to add this in Electron (electron/electron #23479: [Windows] Title bar does not respect dark mode)
- dyasta for an explanation on Stack Overflow
- Richard Yu for implementing this in a C++ library
- Berrysoft for implementing this in Mintty, the Cygwin Terminal emulator (further discussion)
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
-
.NETCoreApp 3.1
- No dependencies.
-
.NETFramework 4.7.2
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|