Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,20 @@ namespace System.Windows.Forms;
/// </summary>
public static class ButtonRenderer
{
private const string DarkModeThemeIdentifier = $"{Control.DarkModeIdentifier}_{Control.ExplorerThemeIdentifier}::{PInvoke.WC_BUTTON}";

// Make this per-thread, so that different threads can safely use these methods.
[ThreadStatic]
private static VisualStyleRenderer? t_visualStyleRenderer;
private static readonly VisualStyleElement s_buttonElement = VisualStyleElement.Button.PushButton.Normal;
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private static readonly VisualStyleElement s_darkButtonElement =
VisualStyleElement.CreateElement(DarkModeThemeIdentifier, 1, 1);
private static readonly VisualStyleElement s_lightButtonElement = VisualStyleElement.Button.PushButton.Normal;
private static readonly VisualStyleElement s_buttonElement =
Application.IsDarkModeEnabled
? s_darkButtonElement
: s_lightButtonElement;
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

/// <summary>
/// Gets or sets a value indicating whether the renderer uses the application state to determine rendering style.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ internal unsafe void DrawBackground(HDC dc, Rectangle bounds, HWND hwnd = defaul

if (!hwnd.IsNull)
{
// in DarkMode we alwayes use OpenThemeData with defult HWNND if the HWND is not null,
// UxTheme has another Method for drawing if Dpi greater than 96,
// Especially when per-monitor aware we need to remove the subclassing. .
uint dpi = PInvoke.GetDpiForWindow(hwnd);
if (dpi > 96 && Class == $"{Control.DarkModeIdentifier}_" +
$"{Control.ExplorerThemeIdentifier}::{PInvoke.WC_BUTTON}")
{
Class = Class[19..];
}

using var htheme = OpenThemeData(hwnd, Class);
_lastHResult = PInvoke.DrawThemeBackground(htheme, dc, Part, State, bounds, null);
}
Expand Down