Difference between revisions of "DarkMode.cs"

From DanIT
Jump to navigation Jump to search
m
m
Line 1: Line 1:
 
== Class ==
 
== Class ==
A C# Class for .NET Framework 2.0 - 4.8.1 (Windows 98 - Windows 11) to enable Dark Mode on Windows Forms.<br>
+
A C# Class file for .NET Framework 2.0 - 4.8.1 (Windows 98 - Windows 11) to enable Dark Mode on Windows Forms.<br>
  
 
By default only .NET 9.0 (no Framework) and higher supports Dark Mode natively, which is an optional download for Windows 10/11 only.<br>
 
By default only .NET 9.0 (no Framework) and higher supports Dark Mode natively, which is an optional download for Windows 10/11 only.<br>
  
[[File:DarkMode_Example.gif|frameless|491px|Example: Default Light Mode / Dark Mode]]<br>
+
''Example:'' Default Light Mode / Dark Mode enabled<br>
 +
[[File:DarkMode_Example.png|frameless|982px|Example: Default Light Mode / Dark Mode]]<br>
  
 
== Script ==
 
== Script ==
 
'''Download:''' https://github.com/d-v-d-k/DarkMode.cs<br>
 
'''Download:''' https://github.com/d-v-d-k/DarkMode.cs<br>
 +
'''View:''' https://github.com/d-v-d-k/DarkMode.cs/blob/main/DarkMode.cs
  
 
== Usage ==
 
== Usage ==
Line 39: Line 41:
 
DarkMode.Enable(form);
 
DarkMode.Enable(form);
 
form.Show();
 
form.Show();
 +
</pre>
 +
 +
''Example:'' When a Button gets clicked
 +
<pre>
 +
private void buttonDarkMode_Click(object sender, EventArgs e)
 +
{
 +
    DarkMode.Enable(this);
 +
}
 
</pre>
 
</pre>
 
=== Inherit===
 
=== Inherit===
Line 44: Line 54:
  
 
Inherit and apply the current Windows User Dark/Light Mode preference to this Form.<br>
 
Inherit and apply the current Windows User Dark/Light Mode preference to this Form.<br>
Only works in Windows 10/11, otherwise Inherit will automatically default to standard Light Mode (change with <code>DarkMode.UserDefault</code>).<br>
+
Can only Inherit on Windows Operating Systems that support Dark Mode (Window 10/11), otherwise Inherit will automatically default to standard Light Mode (change this with <code>DarkMode.UserDefault</code>).
  
 
=== Disable===
 
=== Disable===

Revision as of 07:29, 28 April 2026

Class

A C# Class file for .NET Framework 2.0 - 4.8.1 (Windows 98 - Windows 11) to enable Dark Mode on Windows Forms.

By default only .NET 9.0 (no Framework) and higher supports Dark Mode natively, which is an optional download for Windows 10/11 only.

Example: Default Light Mode / Dark Mode enabled
Example: Default Light Mode / Dark Mode

Script

Download: https://github.com/d-v-d-k/DarkMode.cs
View: https://github.com/d-v-d-k/DarkMode.cs/blob/main/DarkMode.cs

Usage

Add DarkMode.cs to the project solution.

DarkMode Solution.png









Enable

DarkMode.Enable(form);

Enable Dark Mode style on this Form.

Example: On each Form Initialize Event

public Form1()
{
    InitializeComponent();
    DarkMode.Enable(this);
}

Example: When creating a Form instance

Form1 form = new Form1();
DarkMode.Enable(form);
form.Show();

Example: When a Button gets clicked

private void buttonDarkMode_Click(object sender, EventArgs e)
{
    DarkMode.Enable(this);
}

Inherit

DarkMode.Inherit(form);

Inherit and apply the current Windows User Dark/Light Mode preference to this Form.
Can only Inherit on Windows Operating Systems that support Dark Mode (Window 10/11), otherwise Inherit will automatically default to standard Light Mode (change this with DarkMode.UserDefault).

Disable

DarkMode.Disable(form);

Revert this Form back to default Light Mode style.