Skip to content
Draft
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 @@ -173,6 +173,18 @@ protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e)
}
}

protected override void OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs e)
{
if (FillWhenSelected)
{
RenderItemInternalFilled(e);
}
else
{
base.OnRenderToolStripStatusLabelBackground(e);
}
}

protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
base.OnRenderMenuItemBackground(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,14 @@ protected override void OnRenderOverflowButtonBackground(ToolStripItemRenderEven
/// </summary>
protected override void OnRenderLabelBackground(ToolStripItemRenderEventArgs e)
{
// If system is in high contrast mode or dark mode and a specific renderer-override is defined, use that.
// For ToolStripSystemRenderer in a contrast theme, the RendererOverride property will be ToolStripHighContrastRenderer.
if (RendererOverride is not null)
{
base.OnRenderLabelBackground(e);
return;
}

RenderLabelInternal(e);
}

Expand Down Expand Up @@ -545,6 +553,14 @@ protected override void OnRenderSeparator(ToolStripSeparatorRenderEventArgs e)

protected override void OnRenderToolStripStatusLabelBackground(ToolStripItemRenderEventArgs e)
{
// If system is in high contrast mode or dark mode and a specific renderer-override is defined, use that.
// For ToolStripSystemRenderer in a contrast theme, the RendererOverride property will be ToolStripHighContrastRenderer.
if (RendererOverride is not null)
{
base.OnRenderToolStripStatusLabelBackground(e);
return;
}

RenderLabelInternal(e);
ToolStripStatusLabel? item = e.Item as ToolStripStatusLabel;
if (item is not null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.Drawing;
using System.Windows.Forms.Metafiles;

namespace System.Windows.Forms.Tests;

public partial class ToolStripLabelTests
{
[WinFormsFact]
public void ToolStripLabel_Selected_RendersBackgroundCorrectly_HighContrast()
{
using Form form = new();
using ToolStrip toolStrip = new();
toolStrip.Renderer = new ToolStripSystemHighContrastRenderer();
using ToolStripLabel toolStripLabel = new()
{
IsLink = true,
Text = "Test Link"
};
toolStrip.Items.Add(toolStripLabel);
form.Controls.Add(toolStrip);
toolStripLabel.Select();

using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = toolStripLabel.Bounds;
using PaintEventArgs e = new(emf, bounds);
toolStripLabel.TestAccessor().Dynamic.OnPaint(e);

// In high contrast mode, selected items should have a highlight background
emf.Validate(
state,
Validate.Polygon16(
bounds: null,
points: null,
State.Brush(SystemColors.Highlight, BRUSH_STYLE.BS_SOLID)),
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_POLYPOLYGON16), 2));
}

[WinFormsFact]
public void ToolStripLabel_IsLink_Selected_RendersBackgroundCorrectly_HighContrast()
{
using Form form = new();
using ToolStrip toolStrip = new();
toolStrip.Renderer = new ToolStripSystemHighContrastRenderer();
using ToolStripLabel toolStripLabel = new()
{
IsLink = true,
Text = "Link Text"
};
toolStrip.Items.Add(toolStripLabel);
form.Controls.Add(toolStrip);
toolStripLabel.Select();

using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = toolStripLabel.Bounds;
using PaintEventArgs e = new(emf, bounds);
toolStripLabel.TestAccessor().Dynamic.OnPaint(e);

// When IsLink is true and item is selected in high contrast mode,
// the background should be filled with highlight color for proper visibility
emf.Validate(
state,
Validate.Polygon16(
bounds: null,
points: null,
State.Brush(SystemColors.Highlight, BRUSH_STYLE.BS_SOLID)),
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_POLYPOLYGON16), 2));
}

private class ToolStripSystemHighContrastRenderer : ToolStripSystemRenderer
{
internal override ToolStripRenderer RendererOverride => HighContrastRenderer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.Drawing;
using System.Windows.Forms.Metafiles;

namespace System.Windows.Forms.Tests;

public partial class ToolStripStatusLabelTests
{
[WinFormsFact]
public void ToolStripStatusLabel_Selected_RendersBackgroundCorrectly_HighContrast()
{
using Form form = new();
using StatusStrip statusStrip = new();
statusStrip.Renderer = new ToolStripSystemHighContrastRenderer();
using ToolStripStatusLabel toolStripStatusLabel = new()
{
IsLink = true,
Text = "Test Link"
};
statusStrip.Items.Add(toolStripStatusLabel);
form.Controls.Add(statusStrip);
toolStripStatusLabel.Select();

using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = toolStripStatusLabel.Bounds;
using PaintEventArgs e = new(emf, bounds);
toolStripStatusLabel.TestAccessor().Dynamic.OnPaint(e);

// In high contrast mode, selected items should have a highlight background
emf.Validate(
state,
Validate.Polygon16(
bounds: null,
points: null,
State.Brush(SystemColors.Highlight, BRUSH_STYLE.BS_SOLID)),
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_POLYPOLYGON16), 2));
}

[WinFormsFact]
public void ToolStripStatusLabel_IsLink_Selected_RendersBackgroundCorrectly_HighContrast()
{
using Form form = new();
using StatusStrip statusStrip = new();
statusStrip.Renderer = new ToolStripSystemHighContrastRenderer();
using ToolStripStatusLabel toolStripStatusLabel = new()
{
IsLink = true,
Text = "Link Text"
};
statusStrip.Items.Add(toolStripStatusLabel);
form.Controls.Add(statusStrip);
toolStripStatusLabel.Select();

using EmfScope emf = new();
DeviceContextState state = new(emf);
Rectangle bounds = toolStripStatusLabel.Bounds;
using PaintEventArgs e = new(emf, bounds);
toolStripStatusLabel.TestAccessor().Dynamic.OnPaint(e);

// When IsLink is true and item is selected in high contrast mode,
// the background should be filled with highlight color for proper visibility
emf.Validate(
state,
Validate.Polygon16(
bounds: null,
points: null,
State.Brush(SystemColors.Highlight, BRUSH_STYLE.BS_SOLID)),
Validate.Repeat(Validate.SkipType(ENHANCED_METAFILE_RECORD_TYPE.EMR_POLYPOLYGON16), 2));
}

private class ToolStripSystemHighContrastRenderer : ToolStripSystemRenderer
{
internal override ToolStripRenderer RendererOverride => HighContrastRenderer;
}
}
Loading