Skip to content

Commit 3d08357

Browse files
committed
Add helper method "ScaleSmallIconToDpi"
1 parent 118d537 commit 3d08357

File tree

4 files changed

+15
-11
lines changed

4 files changed

+15
-11
lines changed

src/System.Windows.Forms.Primitives/src/System/Windows/Forms/Internals/ScaleHelper.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,16 @@ internal static HighDpiMode GetThreadHighDpiMode()
421421
return HighDpiMode.DpiUnaware;
422422
}
423423

424+
/// <summary>
425+
/// Get X, Y metrics at DPI, IF icon is not already that size, create and return a new one.
426+
/// </summary>
427+
internal static Icon ScaleSmallIconToDpi(Icon icon, int dpi)
428+
{
429+
return new(icon,
430+
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)dpi),
431+
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)dpi));
432+
}
433+
424434
/// <summary>
425435
/// Sets the requested DPI mode. If the current OS does not support the requested mode,
426436
/// </summary>

src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider/ErrorProvider.ErrorWindow.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,9 +456,8 @@ protected override void WmDpiChangedBeforeParent(ref Message m)
456456

457457
double factor = ((double)currentDpi) / _parent._deviceDpi;
458458
using Icon icon = _provider.Icon;
459-
_provider.Icon = new Icon(icon, (int)(icon.Width * factor), (int)(icon.Height * factor));
460-
_provider.DisposeRegion();
461459
_provider._currentDpi = currentDpi;
460+
_provider.Icon = new Icon(icon, (int)(icon.Width * factor), (int)(icon.Height * factor));
462461
}
463462
}
464463
}

src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider/ErrorProvider.IconRegion.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ internal class IconRegion : IHandle<HICON>
1515
private Region? _region;
1616
private readonly Icon _icon;
1717

18-
public IconRegion(int currentDpi, Icon icon)
18+
public IconRegion(Icon icon, int currentDpi)
1919
{
20-
_icon = new(icon,
21-
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)currentDpi),
22-
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)currentDpi));
20+
_icon = ScaleHelper.ScaleSmallIconToDpi(icon, currentDpi);
2321
}
2422

2523
/// <summary>

src/System.Windows.Forms/src/System/Windows/Forms/ErrorProvider/ErrorProvider.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,7 @@ private static Icon DefaultIcon
549549
{
550550
// Error provider uses small Icon.
551551
using Icon defaultIcon = new(typeof(ErrorProvider), "Error");
552-
int currentDpi = (int)PInvoke.GetDpiForSystem();
553-
t_defaultIcon = new(defaultIcon,
554-
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)currentDpi),
555-
PInvoke.GetCurrentSystemMetrics(SYSTEM_METRICS_INDEX.SM_CXSMICON, (uint)currentDpi));
552+
t_defaultIcon = ScaleHelper.ScaleSmallIconToDpi(defaultIcon, (int)PInvoke.GetDpiForSystem());
556553
}
557554
}
558555

@@ -589,7 +586,7 @@ public Icon Icon
589586
/// <summary>
590587
/// Create the icon region on demand.
591588
/// </summary>
592-
internal IconRegion Region => _region ??= new IconRegion(_currentDpi, Icon);
589+
internal IconRegion Region => _region ??= new IconRegion(Icon, _currentDpi);
593590

594591
/// <summary>
595592
/// Begin bulk member initialization - deferring binding to data source until EndInit is reached

0 commit comments

Comments
 (0)