Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style ToolTip to make it dark enough in Dark mode #12420

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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 @@ -714,10 +714,12 @@ private unsafe void CreateHandle()

_window.CreateHandle(cp);

if (SystemInformation.HighContrast)
#pragma warning disable WFO5001
if (SystemInformation.HighContrast || Application.IsDarkModeEnabled)
{
PInvoke.SetWindowTheme(HWND, string.Empty, string.Empty);
}
#pragma warning restore WFO5001
}

// If in OwnerDraw mode, we don't want the default border.
Expand Down Expand Up @@ -770,15 +772,11 @@ private unsafe void CreateHandle()
// Set active status.
PInvokeCore.SendMessage(this, PInvoke.TTM_ACTIVATE, (WPARAM)(BOOL)_active);

if (BackColor != SystemColors.Info)
{
PInvokeCore.SendMessage(this, PInvoke.TTM_SETTIPBKCOLOR, (WPARAM)BackColor);
}
// Set background color.
PInvokeCore.SendMessage(this, PInvoke.TTM_SETTIPBKCOLOR, (WPARAM)_backColor);

if (ForeColor != SystemColors.InfoText)
{
PInvokeCore.SendMessage(this, PInvoke.TTM_SETTIPTEXTCOLOR, (WPARAM)ForeColor);
}
// Set text color.
PInvokeCore.SendMessage(this, PInvoke.TTM_SETTIPTEXTCOLOR, (WPARAM)_foreColor);

if (_toolTipIcon > 0 || !string.IsNullOrEmpty(_toolTipTitle))
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Windows.Forms;

namespace System;

#pragma warning disable WFO5001
/// <summary>
/// Scope for setting the default color mode (dark mode) for the application. Use in a <see langword="using"/> statement.
/// </summary>
public readonly ref struct ApplicationColorModeScope
{
private readonly SystemColorMode _originalColorMode;

public ApplicationColorModeScope(SystemColorMode colorMode)
{
// Prevent multiple ApplicationColorModeScopes from running simultaneously.
// Using Monitor to allow recursion on the same thread.
Monitor.Enter(typeof(ApplicationColorModeScope));
willibrandon marked this conversation as resolved.
Show resolved Hide resolved
_originalColorMode = Application.ColorMode;

if (_originalColorMode != colorMode)
{
Application.SetColorMode(colorMode);
}
}

public void Dispose()
{
try
willibrandon marked this conversation as resolved.
Show resolved Hide resolved
{
if (Application.ColorMode != _originalColorMode)
{
Application.SetColorMode(_originalColorMode);
}
}
finally
{
Monitor.Exit(typeof(ApplicationColorModeScope));
}
}
}
#pragma warning restore WFO5001
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,32 @@ public void ToolTip_ToString_Invoke_ReturnsExpected()
Assert.Equal("System.Windows.Forms.ToolTip InitialDelay: 500, ShowAlways: False", toolTip.ToString());
}

#pragma warning disable WFO5001
[WinFormsFact]
public void ToolTip_DarkMode_GetColors_ReturnsExpected()
{
if (SystemInformation.HighContrast)
{
// We don't run this test in HighContrast mode.
return;
}

using ApplicationColorModeScope colorModeScope = new(colorMode: SystemColorMode.Dark);
using SubToolTip toolTip = new();

toolTip.Handle.Should().NotBe(IntPtr.Zero); // A workaround to create the toolTip native window Handle

var backgroundColor = PInvokeCore.SendMessage(toolTip, PInvoke.TTM_GETTIPBKCOLOR);
Color backColor = ColorTranslator.FromWin32((int)backgroundColor);

var textColor = PInvokeCore.SendMessage(toolTip, PInvoke.TTM_GETTIPTEXTCOLOR);
Color foreColor = ColorTranslator.FromWin32((int)textColor);

backColor.ToArgb().Should().Be(SystemColors.Info.ToArgb());
foreColor.ToArgb().Should().Be(SystemColors.InfoText.ToArgb());
}
#pragma warning restore WFO5001

[WinFormsFact]
public void ToolTip_SetToolTipToControl_Invokes_SetToolTip_OfControl()
{
Expand Down