forked from koush/WindowlessControls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowlessRectangle.cs
executable file
·368 lines (325 loc) · 11.6 KB
/
WindowlessRectangle.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Runtime.InteropServices;
namespace WindowlessControls
{
public class NativeObject : IDisposable
{
protected NativeObject(IntPtr handle)
{
myHandle = handle;
}
IntPtr myHandle;
public IntPtr Handle
{
get { return myHandle; }
}
[DllImport("coredll")]
static extern bool DeleteObject(IntPtr hObject);
[DllImport("coredll")]
internal static extern IntPtr SelectObject(IntPtr hdc, IntPtr hObject);
internal IntPtr Select(IntPtr hdc)
{
return SelectObject(hdc, myHandle);
}
#region IDisposable Members
public void Dispose()
{
DeleteObject(myHandle);
}
#endregion
}
public class NativePen : NativeObject
{
[DllImport("coredll")]
static extern IntPtr CreatePen(int fnPenStyle, int nWidth, int crColor);
//#define PS_SOLID 0
//#define PS_DASH 1
//#define PS_NULL 5
public NativePen(Color color, int width)
: base(CreatePen(0, width, color.ToArgb()))
{
}
}
public class NativeBrush : NativeObject
{
[DllImport("coredll")]
static extern IntPtr CreateSolidBrush(int crColor);
public NativeBrush(Color color)
: base(CreateSolidBrush(color.ToArgb()))
{
}
}
public enum GradientDirection
{
Horizontal = 0,
Vertical = 1
}
public class WindowlessGradientRectangle : WindowlessPaintControl
{
DependencyPropertyStorage<Color> myTopLeftColor;
DependencyPropertyStorage<Color> myBottomRightColor;
DependencyPropertyStorage<GradientDirection> myGradientDirection;
public WindowlessGradientRectangle()
{
myTopLeftColor = new DependencyPropertyStorage<Color>(this, Color.Transparent, new DependencyPropertyChangedEvent(TopLeftColorChanged));
myBottomRightColor = new DependencyPropertyStorage<Color>(this, Color.Transparent, new DependencyPropertyChangedEvent(BottomRightColorChanged));
myGradientDirection = new DependencyPropertyStorage<GradientDirection>(this, GradientDirection.Vertical, new DependencyPropertyChangedEvent(GradientDirectionChanged));
}
public GradientDirection GradientDirection
{
get
{
return myGradientDirection.Value;
}
set
{
myGradientDirection.Value = value;
}
}
public SmartColor TopLeftColor
{
get
{
return myTopLeftColor.Value;
}
set
{
myTopLeftColor.Value = value;
}
}
public SmartColor BottomRightColor
{
get
{
return myBottomRightColor.Value;
}
set
{
myBottomRightColor.Value = value;
}
}
void GradientDirectionChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
void TopLeftColorChanged(object sender, DependencyPropertyEventArgs e)
{
myVertices[0].Red = (ushort)(myTopLeftColor.Value.R << 8);
myVertices[0].Green = (ushort)(myTopLeftColor.Value.G << 8);
myVertices[0].Blue = (ushort)(myTopLeftColor.Value.B << 8);
myVertices[0].Alpha = (ushort)(myTopLeftColor.Value.A << 8);
WindowlessControlHost.WindowlessInvalidate(this);
}
void BottomRightColorChanged(object sender, DependencyPropertyEventArgs e)
{
myVertices[1].Red = (ushort)(myBottomRightColor.Value.R << 8);
myVertices[1].Green = (ushort)(myBottomRightColor.Value.G << 8);
myVertices[1].Blue = (ushort)(myBottomRightColor.Value.B << 8);
myVertices[1].Alpha = (ushort)(myBottomRightColor.Value.A << 8);
WindowlessControlHost.WindowlessInvalidate(this);
}
struct TRIVERTEX
{
public int X;
public int Y;
public ushort Red;
public ushort Green;
public ushort Blue;
public ushort Alpha;
};
static readonly int[] myRectIndices = new int[2] { 0, 1 };
TRIVERTEX[] myVertices = new TRIVERTEX[2];
[DllImport("coredll")]
extern static bool GradientFill(IntPtr hdc, TRIVERTEX[] pVertex, uint nVertex, int[] pMesh, int nCount, int ulMode);
protected override void OnPaint(WindowlessPaintEventArgs e)
{
myVertices[0].X = e.Origin.X;
myVertices[0].Y = e.Origin.Y;
myVertices[1].X = e.Origin.X + ClientWidth;
myVertices[1].Y = e.Origin.Y + ClientHeight;
IntPtr hdc = e.Graphics.GetHdc();
GradientFill(hdc, myVertices, 2, myRectIndices, 1, (int)myGradientDirection.Value);
e.Graphics.ReleaseHdc(hdc);
}
}
public class WindowlessRoundRectangle : WindowlessPaintControl
{
DependencyPropertyStorage<NativePen> myPen;
DependencyPropertyStorage<NativeBrush> myBrush;
DependencyPropertyStorage<int> myEllipseWidth;
DependencyPropertyStorage<int> myEllipseHeight;
static NativePen myDefaultPen = new NativePen(Color.White, 1);
static NativeBrush myDefaultBrush = new NativeBrush(Color.White);
public WindowlessRoundRectangle()
{
myPen = new DependencyPropertyStorage<NativePen>(this, myDefaultPen, new DependencyPropertyChangedEvent(PenChanged));
myBrush = new DependencyPropertyStorage<NativeBrush>(this, myDefaultBrush, new DependencyPropertyChangedEvent(BrushChanged));
myEllipseWidth = new DependencyPropertyStorage<int>(this, 0, new DependencyPropertyChangedEvent(EllipseWidthChanged));
myEllipseHeight = new DependencyPropertyStorage<int>(this, 0, new DependencyPropertyChangedEvent(EllipseHeightChanged));
}
public NativePen Pen
{
get
{
return myPen;
}
set
{
myPen.Value = value;
}
}
public NativeBrush Brush
{
get
{
return myBrush;
}
set
{
myBrush.Value = value;
}
}
public int EllipseWidth
{
get
{
return myEllipseWidth;
}
set
{
myEllipseWidth.Value = value;
}
}
public int EllipseHeight
{
get
{
return myEllipseHeight;
}
set
{
myEllipseHeight.Value = value;
}
}
void PenChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
void BrushChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
void EllipseWidthChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
void EllipseHeightChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
[DllImport("coredll")]
static extern bool RoundRect(
IntPtr hdc,
int nLeftRect,
int nTopRect,
int nRightRect,
int nBottomRect,
int nWidth,
int nHeight
);
protected override void OnPaint(WindowlessPaintEventArgs e)
{
IntPtr hdc = e.Graphics.GetHdc();
IntPtr oldPen = myPen.Value.Select(hdc);
IntPtr oldBrush = myBrush.Value.Select(hdc);
RoundRect(hdc, e.Origin.X, e.Origin.Y, e.Origin.X + ClientWidth, e.Origin.Y + ClientHeight, myEllipseWidth, myEllipseHeight);
NativeObject.SelectObject(hdc, oldBrush);
NativeObject.SelectObject(hdc, oldPen);
e.Graphics.ReleaseHdc(hdc);
}
}
public class WindowlessRectangle : WindowlessPaintControl
{
DependencyPropertyStorage<Color> myColor;
DependencyPropertyStorage<bool> myFilled;
DependencyPropertyStorage<double> myRectangleWidth;
SolidBrush myBrush = new SolidBrush(Color.Transparent);
Pen myPen = new Pen(Color.Transparent);
public Color Color
{
get { return myColor; }
set
{
myColor.Value = value;
}
}
public double RectangleWidth
{
get
{
return myRectangleWidth;
}
set
{
myRectangleWidth.Value = value;
}
}
public bool Filled
{
get
{
return myFilled;
}
set
{
myFilled.Value = value;
}
}
public WindowlessRectangle()
{
myColor = new DependencyPropertyStorage<Color>(this, Color.Transparent, new DependencyPropertyChangedEvent(ColorChanged));
myFilled = new DependencyPropertyStorage<bool>(this, true, new DependencyPropertyChangedEvent(FilledChanged));
myRectangleWidth = new DependencyPropertyStorage<double>(this, 1.0, new DependencyPropertyChangedEvent(RectangleWidthChanged));
}
public WindowlessRectangle(int maxWidth, int maxHeight, Color color)
: this()
{
HorizontalAlignment = HorizontalAlignment.Stretch;
VerticalAlignment = VerticalAlignment.Stretch;
MaxWidth = maxWidth;
MaxHeight = maxHeight;
myColor.Value = color;
myBrush = new SolidBrush(color);
myPen = new Pen(color, (float)myRectangleWidth.Value);
}
void ColorChanged(object sender, DependencyPropertyEventArgs e)
{
myPen.Color = Color;
myBrush.Color = Color;
WindowlessControlHost.WindowlessInvalidate(this);
}
void FilledChanged(object sender, DependencyPropertyEventArgs e)
{
WindowlessControlHost.WindowlessInvalidate(this);
}
void RectangleWidthChanged(object sender, DependencyPropertyEventArgs e)
{
myPen.Width = (float)myRectangleWidth.Value;
if (!myFilled.Value)
WindowlessControlHost.WindowlessInvalidate(this);
}
protected override void OnPaint(WindowlessPaintEventArgs e)
{
if (myColor != Color.Transparent)
{
if (myFilled.Value)
e.Graphics.FillRectangle(myBrush, e.Origin.X, e.Origin.Y, ClientWidth, ClientHeight);
else
e.Graphics.DrawRectangle(myPen, e.Origin.X, e.Origin.Y, ClientWidth - 1, ClientHeight - 1);
}
}
}
}