Skip to content

Commit 7add703

Browse files
authored
Fix title bar small icon scaling (#13787)
We weren't actually scaling the icon for the main form. In addition, we didn't have an embedded icon with multiple resolutions. This appropriately sets the right sized icon to eliminate or reduce blurring.
1 parent cc55eec commit 7add703

File tree

2 files changed

+18
-9
lines changed
  • src/System.Windows.Forms

2 files changed

+18
-9
lines changed
39.4 KB
Binary file not shown.

src/System.Windows.Forms/System/Windows/Forms/Form.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4546,6 +4546,7 @@ protected virtual void OnDpiChanged(DpiChangedEventArgs e)
45464546
_processingDpiChanged = true;
45474547
}
45484548

4549+
UpdateWindowIcon(redrawFrame: true, dpi: e.DeviceDpiNew);
45494550
ScaleContainerForDpi(e.DeviceDpiNew, e.DeviceDpiOld, e.SuggestedRectangle);
45504551
}
45514552
finally
@@ -6417,12 +6418,17 @@ protected override void OnStyleChanged(EventArgs e)
64176418
/// <summary>
64186419
/// Updates the window icon.
64196420
/// </summary>
6420-
private unsafe void UpdateWindowIcon(bool redrawFrame)
6421+
private unsafe void UpdateWindowIcon(bool redrawFrame, int dpi = 0)
64216422
{
64226423
if (IsHandleCreated)
64236424
{
64246425
Icon? icon;
64256426

6427+
if (dpi == 0)
6428+
{
6429+
dpi = DeviceDpi;
6430+
}
6431+
64266432
// Preserve Win32 behavior by keeping the icon we set NULL if
64276433
// the user hasn't specified an icon and we are a dialog frame.
64286434
if ((FormBorderStyle == FormBorderStyle.FixedDialog && _formState[s_formStateIconSet] == 0) || !ShowIcon)
@@ -6436,20 +6442,23 @@ private unsafe void UpdateWindowIcon(bool redrawFrame)
64366442

64376443
if (icon is not null)
64386444
{
6439-
if (_smallIcon is null)
6445+
Icon? oldSmallIcon = _smallIcon;
6446+
6447+
try
6448+
{
6449+
_smallIcon = ScaleHelper.ScaleSmallIconToDpi(icon, dpi);
6450+
}
6451+
catch
64406452
{
6441-
try
6442-
{
6443-
_smallIcon = new Icon(icon, SystemInformation.SmallIconSize);
6444-
}
6445-
catch
6446-
{
6447-
}
64486453
}
64496454

64506455
if (_smallIcon is not null)
64516456
{
64526457
PInvokeCore.SendMessage(this, PInvokeCore.WM_SETICON, (WPARAM)PInvoke.ICON_SMALL, (LPARAM)_smallIcon.Handle);
6458+
if (oldSmallIcon is not null && oldSmallIcon.Handle != _smallIcon.Handle)
6459+
{
6460+
oldSmallIcon.Dispose();
6461+
}
64536462
}
64546463

64556464
PInvokeCore.SendMessage(this, PInvokeCore.WM_SETICON, (WPARAM)PInvoke.ICON_BIG, (LPARAM)icon.Handle);

0 commit comments

Comments
 (0)