Skip to content
Open
Changes from 1 commit
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 @@ -26,6 +26,7 @@ public partial class ProgressBar : Control
private int _marqueeAnimationSpeed = 100;

private static readonly Color s_defaultForeColor = SystemColors.Highlight;
private static readonly Color s_defaultDarkModeBackColor = SystemColors.ControlText;

private ProgressBarStyle _style = ProgressBarStyle.Blocks;

Expand Down Expand Up @@ -74,26 +75,6 @@ protected override CreateParams CreateParams
protected override void OnCreateControl()
{
base.OnCreateControl();

// If SystemColorMode is enabled, we need to disable the Visual Styles
// so Windows allows setting Fore- and Background color.
// There are more ideal ways imaginable, but this does the trick for now.

if (Application.IsDarkModeEnabled)
{
if (!ShouldSerializeBackColor())
{
BackColor = SystemColors.ControlDarkDark;
}

if (!ShouldSerializeForeColor())
{
ForeColor = SystemColors.Highlight;
}

// Disables Visual Styles for the ProgressBar.
PInvoke.SetWindowTheme(HWND, " ", " ");
}
}

[Browsable(false)]
Expand Down Expand Up @@ -133,6 +114,8 @@ public ProgressBarStyle Style
if (IsHandleCreated)
{
RecreateHandle();
// Re-apply theming after handle recreation
ApplyTheming();
Comment thread
LeafShi1 marked this conversation as resolved.
}

if (_style == ProgressBarStyle.Marquee)
Expand Down Expand Up @@ -349,7 +332,8 @@ protected override void OnBackColorChanged(EventArgs e)
base.OnBackColorChanged(e);
if (IsHandleCreated)
{
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, 0, BackColor.ToWin32());
ApplyTheming();
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, 0, GetEffectiveBackColor().ToWin32());
}
}

Expand All @@ -358,7 +342,8 @@ protected override void OnForeColorChanged(EventArgs e)
base.OnForeColorChanged(e);
if (IsHandleCreated)
{
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, 0, ForeColor.ToWin32());
ApplyTheming();
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, 0, GetEffectiveForeColor().ToWin32());
}
}

Expand Down Expand Up @@ -603,13 +588,17 @@ public void Increment(int value)
protected override void OnHandleCreated(EventArgs e)
{
base.OnHandleCreated(e);

ApplyTheming();

if (IsHandleCreated)
{
PInvokeCore.SendMessage(this, PInvoke.PBM_SETRANGE32, (WPARAM)_minimum, (LPARAM)_maximum);
PInvokeCore.SendMessage(this, PInvoke.PBM_SETSTEP, (WPARAM)_step);
PInvokeCore.SendMessage(this, PInvoke.PBM_SETPOS, (WPARAM)_value);
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, (WPARAM)0, (LPARAM)BackColor);
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, (WPARAM)0, (LPARAM)ForeColor);

PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, 0, GetEffectiveBackColor().ToWin32());
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, 0, GetEffectiveForeColor().ToWin32());
}

StartMarquee();
Expand Down Expand Up @@ -703,8 +692,46 @@ private void UserPreferenceChangedHandler(object o, UserPreferenceChangedEventAr
{
if (IsHandleCreated)
{
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, 0, ForeColor.ToWin32());
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, 0, BackColor.ToWin32());
ApplyTheming();
Comment thread
LeafShi1 marked this conversation as resolved.
Outdated

PInvokeCore.SendMessage(this, PInvoke.PBM_SETBARCOLOR, 0, GetEffectiveForeColor().ToWin32());
PInvokeCore.SendMessage(this, PInvoke.PBM_SETBKCOLOR, 0, GetEffectiveBackColor().ToWin32());
}
}

private Color GetEffectiveBackColor()
{
if (ShouldSerializeBackColor())
{
return BackColor;
}

return Application.IsDarkModeEnabled ? s_defaultDarkModeBackColor : SystemColors.Control;
}

private Color GetEffectiveForeColor()
{
if (ShouldSerializeForeColor())
{
return ForeColor;
}

return s_defaultForeColor;
}

private void ApplyTheming()
{
if (!IsHandleCreated)
{
return;
}

if (Application.IsDarkModeEnabled || ShouldSerializeBackColor() || ShouldSerializeForeColor())
{
// In dark mode on newer Windows builds, style switching can produce mixed rendering
// across Blocks/Continuous/Marquee. Disable visual styles and drive colors via PBM_SET*COLOR
// for consistent appearance.
PInvoke.SetWindowTheme(HWND, " ", " ");
}
Comment thread
LeafShi1 marked this conversation as resolved.
}

Expand Down
Loading