-
Notifications
You must be signed in to change notification settings - Fork 3
/
CustomAdorner.cs
160 lines (132 loc) · 5.23 KB
/
CustomAdorner.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.Windows;
using System.Windows.Media;
using System.Threading;
/// cool Adorner helper class provided by https://marlongrech.wordpress.com/2008/02/28/wpf-overlays-or-better-adorner/
namespace AppRTCDemo
{
/// <summary>
/// Adorner that disables all controls that fall under it
/// </summary>
public class CustomAdorner : Adorner
{
public UIElement el;
#region Properties
/// <summary>
/// Gets or sets the color to paint
/// </summary>
public Brush Color
{
get { return (Brush)GetValue(ColorProperty); }
set { SetValue(ColorProperty, value); }
}
/// <summary>
/// Gets or sets the color to paint
/// </summary>
public static readonly DependencyProperty ColorProperty =
DependencyProperty.Register("Color", typeof(Brush), typeof(CustomAdorner),
new PropertyMetadata((Brush)new BrushConverter().ConvertFromString("#7F4047F7")));
/// <summary>
/// Gets or sets the border
/// </summary>
public Pen Border
{
get { return (Pen)GetValue(BorderProperty); }
set { SetValue(BorderProperty, value); }
}
/// <summary>
/// Gets or sets the border
/// </summary>
public static readonly DependencyProperty BorderProperty =
DependencyProperty.Register("Border", typeof(Pen), typeof(CustomAdorner),
new UIPropertyMetadata(new Pen(Brushes.Gray, 1)));
//the start point where to start drawing
private static readonly Point startPoint =
new Point(0, 0);
/// <summary>
/// Gets or sets the text to display
/// </summary>
public string OverlayedText
{
get { return (string)GetValue(OverlayedTextProperty); }
set { SetValue(OverlayedTextProperty, value); }
}
/// <summary>
/// Gets or sets the text to display
/// </summary>
public static readonly DependencyProperty OverlayedTextProperty =
DependencyProperty.Register("OverlayedText", typeof(string), typeof(CustomAdorner), new UIPropertyMetadata(""));
/// <summary>
/// Gets or sets the foreground to use for the text
/// </summary>
public Brush ForeGround
{
get { return (Brush)GetValue(ForeGroundProperty); }
set { SetValue(ForeGroundProperty, value); }
}
/// <summary>
/// Gets or sets the foreground to use for the text
/// </summary>
public static readonly DependencyProperty ForeGroundProperty =
DependencyProperty.Register("ForeGround", typeof(Brush), typeof(CustomAdorner),
new UIPropertyMetadata(Brushes.Aquamarine));
/// <summary>
/// Gets or sets the font size for the text
/// </summary>
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
/// <summary>
/// Gets or sets the font size for the text
/// </summary>
public static readonly DependencyProperty FontSizeProperty =
DependencyProperty.Register("FontSize", typeof(double), typeof(CustomAdorner), new UIPropertyMetadata(10.0));
/// <summary>
/// Gets or sets the Typeface for the text
/// </summary>
public Typeface Typeface
{
get { return (Typeface)GetValue(TypefaceProperty); }
set { SetValue(TypefaceProperty, value); }
}
/// <summary>
/// Gets or sets the Typeface for the text
/// </summary>
public static readonly DependencyProperty TypefaceProperty =
DependencyProperty.Register("Typeface", typeof(Typeface), typeof(CustomAdorner),
new UIPropertyMetadata(new Typeface("Verdana")));
#endregion
/// <summary>
/// Constructor for the adorner
/// </summary>
/// <param name="adornerElement">The element to be adorned</param>
public CustomAdorner(UIElement adornerElement)
: base(adornerElement)
{
el = adornerElement;
}
/// <summary>
/// Called to draw on screen
/// </summary>
/// <param name="drawingContext">The drawind context in which we can draw</param>
protected override void OnRender(System.Windows.Media.DrawingContext drawingContext)
{
FormattedText text = new FormattedText(OverlayedText, Thread.CurrentThread.CurrentUICulture,
FlowDirection.LeftToRight, Typeface, FontSize, ForeGround);
// Find the center of the client area.
double xmid = el.RenderSize.Width / 2;
double ymid = el.RenderSize.Height;
Point center =
new Point(xmid, ymid - text.Height);
drawingContext.DrawText(text, center);
//drawingContext.DrawRectangle(Color, Border, new Rect(startPoint, DesiredSize));
base.OnRender(drawingContext);
}
}
}