-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatusSymbolRenderer.cs
More file actions
244 lines (205 loc) · 8.44 KB
/
Copy pathStatusSymbolRenderer.cs
File metadata and controls
244 lines (205 loc) · 8.44 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace c2flux
{
public enum StatusSymbolKind
{
Information,
Warning,
Error,
SystemDirectory
}
public static class StatusSymbolRenderer
{
public const int DefaultSymbolSize = 14;
public static Bitmap CreateBitmap(StatusSymbolKind symbolKind)
{
return CreateBitmap(symbolKind, DefaultSymbolSize);
}
public static Bitmap CreateBitmap(StatusSymbolKind symbolKind, int symbolSize)
{
int size = Math.Max(1, symbolSize);
Bitmap bitmap = new Bitmap(size, size, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics graphics = Graphics.FromImage(bitmap))
{
ConfigureGraphics(graphics);
graphics.Clear(Color.Transparent);
DrawSymbol(graphics, new RectangleF(0, 0, size, size), symbolKind);
}
return bitmap;
}
public static void DrawSymbol(Graphics graphics, RectangleF symbolBox, StatusSymbolKind symbolKind)
{
if (graphics == null)
return;
ConfigureGraphics(graphics);
switch (symbolKind)
{
case StatusSymbolKind.Information:
DrawCircleSymbol(
graphics,
symbolBox,
Color.FromArgb(0, 120, 212),
Color.FromArgb(0, 90, 158),
Color.White,
"i",
0F);
break;
case StatusSymbolKind.Warning:
DrawWarningTriangleSymbol(graphics, symbolBox);
break;
case StatusSymbolKind.Error:
DrawSquareSymbol(
graphics,
symbolBox,
Color.FromArgb(196, 43, 28),
Color.FromArgb(135, 24, 15),
Color.White,
"×",
0F);
break;
default:
DrawCircleSymbol(
graphics,
symbolBox,
Color.FromArgb(255, 140, 0),
Color.FromArgb(178, 92, 0),
Color.Black,
"!",
0F);
break;
}
}
public static void DrawTreeExpandGlyph(Graphics graphics, RectangleF glyphBox, bool expanded)
{
if (graphics == null)
return;
ConfigureGraphics(graphics);
RectangleF bounds = GetShapeBounds(glyphBox);
using (SolidBrush fillBrush = new SolidBrush(SystemColors.Window))
using (Pen borderPen = new Pen(SystemColors.ControlDark, 1F))
using (Pen linePen = new Pen(SystemColors.WindowText, 1F))
{
graphics.FillRectangle(fillBrush, bounds);
graphics.DrawRectangle(
borderPen,
bounds.Left,
bounds.Top,
bounds.Width,
bounds.Height);
float centerX = bounds.Left + bounds.Width / 2F;
float centerY = bounds.Top + bounds.Height / 2F;
graphics.DrawLine(
linePen,
bounds.Left + 2F,
centerY,
bounds.Right - 2F,
centerY);
if (!expanded)
{
graphics.DrawLine(
linePen,
centerX,
bounds.Top + 2F,
centerX,
bounds.Bottom - 2F);
}
}
}
private static void ConfigureGraphics(Graphics graphics)
{
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.PixelOffsetMode = PixelOffsetMode.Half;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
}
private static void DrawCircleSymbol(Graphics graphics, RectangleF symbolBox, Color fillColor, Color borderColor, Color textColor, string symbolText, float textOffsetY)
{
RectangleF bounds = GetShapeBounds(symbolBox);
using (GraphicsPath circlePath = new GraphicsPath())
using (SolidBrush fillBrush = new SolidBrush(fillColor))
using (Pen borderPen = new Pen(borderColor, 1F))
{
circlePath.AddEllipse(bounds);
graphics.FillPath(fillBrush, circlePath);
graphics.DrawPath(borderPen, circlePath);
}
DrawCenteredSymbolText(graphics, symbolBox, textColor, symbolText, textOffsetY);
}
private static void DrawWarningTriangleSymbol(Graphics graphics, RectangleF symbolBox)
{
RectangleF bounds = GetShapeBounds(symbolBox);
PointF topPoint = new PointF(
bounds.Left + bounds.Width / 2F,
bounds.Top);
PointF rightPoint = new PointF(
bounds.Right,
bounds.Bottom);
PointF leftPoint = new PointF(
bounds.Left,
bounds.Bottom);
using (GraphicsPath trianglePath = new GraphicsPath())
using (SolidBrush fillBrush = new SolidBrush(Color.FromArgb(255, 185, 0)))
using (Pen borderPen = new Pen(Color.FromArgb(180, 125, 0), 1F))
{
trianglePath.AddPolygon(new[] { topPoint, rightPoint, leftPoint });
graphics.FillPath(fillBrush, trianglePath);
graphics.DrawPath(borderPen, trianglePath);
}
DrawCenteredSymbolText(graphics, symbolBox, Color.Black, "!", 1F);
}
private static void DrawSquareSymbol(Graphics graphics, RectangleF symbolBox, Color fillColor, Color borderColor, Color textColor, string symbolText, float textOffsetY)
{
RectangleF bounds = GetShapeBounds(symbolBox);
using (SolidBrush fillBrush = new SolidBrush(fillColor))
using (Pen borderPen = new Pen(borderColor, 1F))
{
graphics.FillRectangle(fillBrush, bounds);
graphics.DrawRectangle(
borderPen,
bounds.Left,
bounds.Top,
bounds.Width,
bounds.Height);
}
DrawCenteredSymbolText(graphics, symbolBox, textColor, symbolText, textOffsetY);
}
private static RectangleF GetShapeBounds(RectangleF symbolBox)
{
return new RectangleF(
symbolBox.Left + 0.5F,
symbolBox.Top + 0.5F,
symbolBox.Width - 1F,
symbolBox.Height - 1F);
}
private static void DrawCenteredSymbolText(Graphics graphics, RectangleF symbolBox, Color textColor, string symbolText, float offsetY)
{
using (GraphicsPath textPath = new GraphicsPath())
using (FontFamily fontFamily = new FontFamily("Segoe UI"))
using (SolidBrush textBrush = new SolidBrush(textColor))
{
textPath.AddString(
symbolText,
fontFamily,
(int)FontStyle.Bold,
9F,
Point.Empty,
StringFormat.GenericTypographic);
RectangleF textBounds = textPath.GetBounds();
float targetCenterX = symbolBox.Left + symbolBox.Width / 2F;
float targetCenterY = symbolBox.Top + symbolBox.Height / 2F + offsetY;
float textCenterX = textBounds.Left + textBounds.Width / 2F;
float textCenterY = textBounds.Top + textBounds.Height / 2F;
using (Matrix transformMatrix = new Matrix())
{
transformMatrix.Translate(
targetCenterX - textCenterX,
targetCenterY - textCenterY);
textPath.Transform(transformMatrix);
}
graphics.FillPath(textBrush, textPath);
}
}
}
}