-
Notifications
You must be signed in to change notification settings - Fork 957
/
SolidBrush.cs
112 lines (94 loc) · 3.16 KB
/
SolidBrush.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Drawing.Internal;
namespace System.Drawing;
public sealed unsafe class SolidBrush : Brush, ISystemColorTracker
{
// GDI+ doesn't understand system colors, so we need to cache the value here.
private Color _color = Color.Empty;
private bool _immutable;
public SolidBrush(Color color)
{
_color = color;
GpSolidFill* nativeBrush;
PInvoke.GdipCreateSolidFill((ARGB)_color, &nativeBrush).ThrowIfFailed();
SetNativeBrushInternal((GpBrush*)nativeBrush);
if (_color.IsSystemColor)
{
SystemColorTracker.Add(this);
}
}
internal SolidBrush(Color color, bool immutable) : this(color) => _immutable = immutable;
internal SolidBrush(GpSolidFill* nativeBrush)
{
Debug.Assert(nativeBrush is not null, "Initializing native brush with null.");
SetNativeBrushInternal((GpBrush*)nativeBrush);
}
public override object Clone()
{
GpBrush* clonedBrush;
PInvoke.GdipCloneBrush(NativeBrush, &clonedBrush).ThrowIfFailed();
GC.KeepAlive(this);
// Clones of immutable brushes are not immutable.
return new SolidBrush((GpSolidFill*)clonedBrush);
}
protected override void Dispose(bool disposing)
{
if (!disposing)
{
_immutable = false;
}
else if (_immutable)
{
throw new ArgumentException(SR.Format(SR.CantChangeImmutableObjects, "Brush"));
}
base.Dispose(disposing);
}
public Color Color
{
get
{
if (_color == Color.Empty)
{
ARGB color;
PInvoke.GdipGetSolidFillColor((GpSolidFill*)NativeBrush, (uint*)&color).ThrowIfFailed();
GC.KeepAlive(this);
_color = color;
}
// GDI+ doesn't understand system colors, so we can't use GdipGetSolidFillColor in the general case.
return _color;
}
set
{
if (_immutable)
{
throw new ArgumentException(SR.Format(SR.CantChangeImmutableObjects, "Brush"));
}
if (_color != value)
{
Color oldColor = _color;
InternalSetColor(value);
// NOTE: We never remove brushes from the active list, so if someone is
// changing their brush colors a lot, this could be a problem.
if (value.IsSystemColor && !oldColor.IsSystemColor)
{
SystemColorTracker.Add(this);
}
}
}
}
// Sets the color even if the brush is considered immutable.
private void InternalSetColor(Color value)
{
PInvoke.GdipSetSolidFillColor((GpSolidFill*)NativeBrush, (ARGB)value).ThrowIfFailed();
GC.KeepAlive(this);
_color = value;
}
void ISystemColorTracker.OnSystemColorChanged()
{
if (NativeBrush is not null)
{
InternalSetColor(_color);
}
}
}