@@ -24,8 +24,9 @@ public partial class ErrorProvider : Component, IExtenderProvider, ISupportIniti
24
24
{
25
25
private readonly Dictionary < Control , ControlItem > _items = [ ] ;
26
26
private readonly Dictionary < Control , ErrorWindow > _windows = [ ] ;
27
- private Icon _icon = DefaultIcon ;
27
+ private Icon ? _icon ;
28
28
private IconRegion ? _region ;
29
+ private int _currentDpi ;
29
30
private int _itemIdCounter ;
30
31
private int _blinkRate ;
31
32
private ErrorBlinkStyle _blinkStyle ;
@@ -80,7 +81,6 @@ public ErrorProvider(IContainer container)
80
81
: this ( )
81
82
{
82
83
ArgumentNullException . ThrowIfNull ( container ) ;
83
-
84
84
container . Add ( this ) ;
85
85
}
86
86
@@ -308,7 +308,7 @@ public object? DataSource
308
308
{
309
309
if ( _parentControl is not null && _parentControl . BindingContext is not null && value is not null && ! string . IsNullOrEmpty ( _dataMember ) )
310
310
{
311
- // Let's check if the datamember exists in the new data source
311
+ // Let's check if the data member exists in the new data source
312
312
try
313
313
{
314
314
_errorManager = _parentControl . BindingContext [ value , _dataMember ] ;
@@ -546,10 +546,8 @@ private static Icon DefaultIcon
546
546
if ( t_defaultIcon is null )
547
547
{
548
548
// Error provider uses small Icon.
549
- int width = PInvokeCore . GetSystemMetrics ( SYSTEM_METRICS_INDEX . SM_CXSMICON ) ;
550
- int height = PInvokeCore . GetSystemMetrics ( SYSTEM_METRICS_INDEX . SM_CYSMICON ) ;
551
- using Icon defaultIcon = new ( typeof ( ErrorProvider ) , "Error" ) ;
552
- t_defaultIcon = new Icon ( defaultIcon , width , height ) ;
549
+ Icon defaultIcon = new ( typeof ( ErrorProvider ) , "Error" ) ;
550
+ t_defaultIcon = ScaleHelper . ScaleSmallIconToDpi ( defaultIcon , ScaleHelper . InitialSystemDpi ) ;
553
551
}
554
552
}
555
553
@@ -567,10 +565,7 @@ private static Icon DefaultIcon
567
565
[ SRDescription ( nameof ( SR . ErrorProviderIconDescr ) ) ]
568
566
public Icon Icon
569
567
{
570
- get
571
- {
572
- return _icon ;
573
- }
568
+ get => _icon ??= DefaultIcon ;
574
569
set
575
570
{
576
571
_icon = value . OrThrowIfNull ( ) ;
@@ -583,10 +578,21 @@ public Icon Icon
583
578
}
584
579
}
585
580
581
+ /// <summary>
582
+ /// Gets or sets the DPI at which the current error is displayed.
583
+ /// If currentDpi is not set, it defaults to _parentControl.DeviceDpi
584
+ /// or the system DPI.
585
+ /// </summary>
586
+ private int CurrentDpi
587
+ {
588
+ get => _currentDpi != 0 ? _currentDpi : _parentControl ? . DeviceDpi ?? ScaleHelper . InitialSystemDpi ;
589
+ set => _currentDpi = value ;
590
+ }
591
+
586
592
/// <summary>
587
593
/// Create the icon region on demand.
588
594
/// </summary>
589
- internal IconRegion Region => _region ??= new IconRegion ( Icon ) ;
595
+ internal IconRegion Region => _region ??= new IconRegion ( Icon , CurrentDpi ) ;
590
596
591
597
/// <summary>
592
598
/// Begin bulk member initialization - deferring binding to data source until EndInit is reached
@@ -761,7 +767,7 @@ internal ErrorWindow EnsureErrorWindow(Control parent)
761
767
[ SRDescription ( nameof ( SR . ErrorProviderIconPaddingDescr ) ) ]
762
768
public int GetIconPadding ( Control control ) => EnsureControlItem ( control ) . IconPadding ;
763
769
764
- private void ResetIcon ( ) => Icon = DefaultIcon ;
770
+ private void ResetIcon ( ) => _icon = null ;
765
771
766
772
[ EditorBrowsable ( EditorBrowsableState . Advanced ) ]
767
773
protected virtual void OnRightToLeftChanged ( EventArgs e )
@@ -814,5 +820,5 @@ public void SetIconPadding(Control control, int padding)
814
820
EnsureControlItem ( control ) . IconPadding = padding ;
815
821
}
816
822
817
- private bool ShouldSerializeIcon ( ) => Icon != DefaultIcon ;
823
+ private bool ShouldSerializeIcon ( ) => _icon is not null && _icon != DefaultIcon ;
818
824
}
0 commit comments