Skip to content

Commit

Permalink
When system-wide dark mode is detected, sets the UseImmersiveDarkMode…
Browse files Browse the repository at this point in the history
… to make the title bar darj.
  • Loading branch information
FelisDiligens committed Mar 9, 2023
1 parent 38b8af5 commit c108657
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
16 changes: 16 additions & 0 deletions Fo76ini/Forms/FormMain/FormMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
Expand Down Expand Up @@ -119,6 +120,21 @@ public FormMain()
Utils.PreventChangeOnMouseWheelForAllElements(this);

this.userControlSideNav.SelectedTabIndex = 0;

// Set dark mode
if (Theming.DetectSystemTheme() == ThemeType.Dark)
{
int t = 1; // true
IntPtr hWnd = this.Handle;
try
{
Utils.DwmSetWindowAttribute(hWnd, Utils.DwmWindowAttribute.UseImmersiveDarkMode, ref t, Marshal.SizeOf(t));
}
catch (System.EntryPointNotFoundException ex)
{
Console.WriteLine("Couldn't call DwmSetWindowAttribute: " + ex.Message);
}
}
}

private void FormMain_Load(object sender, EventArgs e)
Expand Down
17 changes: 17 additions & 0 deletions Fo76ini/Forms/FormMods/FormMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.Threading;
using System.Windows.Forms;
using static Fo76ini.Utilities.Archive2;
using System.Runtime.InteropServices;

namespace Fo76ini
{
Expand Down Expand Up @@ -68,6 +69,22 @@ public FormMods()

this.FormClosing += this.FormMods_FormClosing;
this.KeyDown += this.FormMods_KeyDown;

// Set dark mode
if (Theming.DetectSystemTheme() == ThemeType.Dark)
{
int t = 1; // true
IntPtr hWnd = this.Handle;
try
{
Utils.DwmSetWindowAttribute(hWnd, Utils.DwmWindowAttribute.UseImmersiveDarkMode, ref t, Marshal.SizeOf(t));
}
catch (System.EntryPointNotFoundException ex)
{
Console.WriteLine("Couldn't call DwmSetWindowAttribute: " + ex.Message);
}
}

}

private void OnProfileChanged(object sender, ProfileEventArgs e)
Expand Down
35 changes: 34 additions & 1 deletion Fo76ini/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,46 @@ public static bool IsDirectoryEmpty(string path)
return !Directory.EnumerateFileSystemEntries(path).Any();
}

/* https://www.pinvoke.net/default.aspx/Enums/DWMWINDOWATTRIBUTE.html */
public enum DwmWindowAttribute : uint
{
NCRenderingEnabled = 1,
NCRenderingPolicy,
TransitionsForceDisabled,
AllowNCPaint,
CaptionButtonBounds,
NonClientRtlLayout,
ForceIconicRepresentation,
Flip3DPolicy,
ExtendedFrameBounds,
HasIconicBitmap,
DisallowPeek,
ExcludedFromPeek,
Cloak,
Cloaked,
FreezeRepresentation,
PassiveUpdateMode,
UseHostBackdropBrush,
UseImmersiveDarkMode = 20,
WindowCornerPreference = 33,
BorderColor,
CaptionColor,
TextColor,
VisibleFrameBorderThickness,
SystemBackdropType,
Last
}

/* https://pinvoke.net/default.aspx/Enums/DwmSetWindowAttribute.html?diff=y */
[DllImport("dwmapi.dll", PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute attr, ref int attrValue, int attrSize);

/*
* EnumDisplaySettings
* https://www.reddit.com/r/csharp/comments/31yw0t/how_can_i_get_the_main_monitors_refresh_rate/
* https://stackoverflow.com/questions/744541/how-to-list-available-video-modes-using-c/744609#744609
* https://docs.microsoft.com/de-de/windows/win32/api/winuser/nf-winuser-enumdisplaysettingsa?redirectedfrom=MSDN
*/

[DllImport("user32.dll")]
private static extern bool EnumDisplaySettings(
string deviceName, int modeNum, ref DEVMODE devMode);
Expand Down

0 comments on commit c108657

Please sign in to comment.