-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathOutlinedTextController.cs
More file actions
159 lines (134 loc) · 6.42 KB
/
OutlinedTextController.cs
File metadata and controls
159 lines (134 loc) · 6.42 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
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
namespace LLC_MOD_Toolbox
{
public class OutlinedTextControl : FrameworkElement
{
// 定义依赖属性
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static readonly DependencyProperty StrokeProperty =
DependencyProperty.Register("Stroke", typeof(Brush), typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyProperty.Register("StrokeThickness", typeof(double), typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(2.0, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FillProperty =
DependencyProperty.Register("Fill", typeof(Brush), typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(Brushes.White, FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(12.0, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(SystemFonts.MessageFontFamily, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FontWeightProperty =
TextElement.FontWeightProperty.AddOwner(typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(FontWeights.Normal, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty FontStyleProperty =
TextElement.FontStyleProperty.AddOwner(typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(FontStyles.Normal, FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsRender));
public static readonly DependencyProperty TextAlignmentProperty =
TextBlock.TextAlignmentProperty.AddOwner(
typeof(OutlinedTextControl),
new FrameworkPropertyMetadata(
TextAlignment.Left,
FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure
)
);
public TextAlignment TextAlignment
{
get { return (TextAlignment)GetValue(TextAlignmentProperty); }
set { SetValue(TextAlignmentProperty, value); }
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (string.IsNullOrEmpty(Text))
return;
var formattedText = CreateFormattedText();
double textWidth = formattedText.Width;
double originX = 0;
// 计算水平绘制原点
switch (TextAlignment)
{
case TextAlignment.Right:
originX = RenderSize.Width - textWidth;
break;
case TextAlignment.Center:
originX = (RenderSize.Width - textWidth) / 2;
break;
case TextAlignment.Left:
default:
originX = 0;
break;
}
// 生成几何图形并绘制
var geometry = formattedText.BuildGeometry(new Point(originX, 0));
drawingContext.DrawGeometry(Stroke, new Pen(Stroke, StrokeThickness), geometry); // 描边
drawingContext.DrawGeometry(Fill, null, geometry); // 填充
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public Brush Stroke
{
get { return (Brush)GetValue(StrokeProperty); }
set { SetValue(StrokeProperty, value); }
}
public double StrokeThickness
{
get { return (double)GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
}
public Brush Fill
{
get { return (Brush)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
public FontFamily FontFamily
{
get { return (FontFamily)GetValue(FontFamilyProperty); }
set { SetValue(FontFamilyProperty, value); }
}
public FontWeight FontWeight
{
get { return (FontWeight)GetValue(FontWeightProperty); }
set { SetValue(FontWeightProperty, value); }
}
public FontStyle FontStyle
{
get { return (FontStyle)GetValue(FontStyleProperty); }
set { SetValue(FontStyleProperty, value); }
}
protected override Size MeasureOverride(Size availableSize)
{
if (string.IsNullOrEmpty(Text))
return new Size(0, 0);
var formattedText = CreateFormattedText();
return new Size(formattedText.Width, formattedText.Height);
}
private FormattedText CreateFormattedText()
{
return new FormattedText(
Text,
CultureInfo.CurrentUICulture,
FlowDirection.LeftToRight,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretches.Normal),
FontSize,
Brushes.Black, // 此处的颜色并不影响渲染,因为使用Geometry绘制
VisualTreeHelper.GetDpi(this).PixelsPerDip);
}
}
}