WPF自定义空心文字
首先创建一个自定义控件,继承自FrameworkElement,“Generic.xaml”中可以不添加样式。
要自定义空心文字,要用到绘制格式化文本FormattedText类。FormattedText对象提供的文本格式设置功能比WPF提供的已有文本控件提供的相应功能更为强大。调用FormattedText构造函数,可以传入相应的参数,得到我们想要的文本样式。使用 MaxTextWidth 属性可以将文本约束为特定宽度。 文本将自动换行,以避免超过指定宽度。 使用 MaxTextHeight 属性可以将文本约束为特定高度。 超过指定高度的文本将显示一个省略号“…”。
接下来重写OnRender方法,在方法体中调用DrawingContext对象的DrawGeometry方法即可完成文本的绘制工作。
public class OutlinedText : FrameworkElement, IAddChild
{
/// <summary>
/// 静态构造函数
/// </summary>
static OutlinedText()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(OutlinedText), new FrameworkPropertyMetadata(typeof(OutlinedText)));
} #region Private Fields /// <summary>
/// 文字几何形状
/// </summary>
private Geometry m_TextGeometry; #endregion #region Private Methods /// <summary>
/// 当依赖项属性改变文字无效时,创建新的空心文字对象来显示。
/// </summary>
/// <param name="d"></param>
/// <param name="e"></param>
private static void OnOutlineTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (Convert.ToString(e.NewValue) != Convert.ToString(e.OldValue))
{
((OutlinedText)d).CreateText();
}
} #endregion #region FrameworkElement Overrides /// <summary>
/// 重写绘制文字的方法。
/// </summary>
/// <param name="drawingContext">空心文字控件的绘制上下文。</param>
protected override void OnRender(DrawingContext drawingContext)
{
//CreateText();
// 基于设置的属性绘制空心文字控件。
drawingContext.DrawGeometry(Fill, new Pen(Stroke, StrokeThickness), m_TextGeometry);
} /// <summary>
/// 基于格式化文字创建文字的几何轮廓。
/// </summary>
public void CreateText()
{
FontStyle fontStyle = FontStyles.Normal;
FontWeight fontWeight = FontWeights.Medium;
if (Bold == true)
fontWeight = FontWeights.Bold;
if (Italic == true)
fontStyle = FontStyles.Italic;
// 基于设置的属性集创建格式化的文字。
FormattedText formattedText = new FormattedText(
Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight,
new Typeface(Font, fontStyle, fontWeight, FontStretches.Normal),
FontSize, Brushes.Black);
formattedText.MaxTextWidth = this.MaxTextWidth;
formattedText.MaxTextHeight = this.MaxTextHeight;
// 创建表示文字的几何对象。
m_TextGeometry = formattedText.BuildGeometry(new Point(, ));
// 基于格式化文字的大小设置空心文字的大小。
this.MinWidth = formattedText.Width;
this.MinHeight = formattedText.Height;
} #endregion #region DependencyProperties /// <summary>
/// 指定将文本约束为特定宽度
/// </summary>
public double MaxTextWidth
{
get { return (double)GetValue(MaxTextWidthProperty); }
set { SetValue(MaxTextWidthProperty, value); }
}
/// <summary>
/// 指定将文本约束为特定宽度依赖属性
/// </summary>
public static readonly DependencyProperty MaxTextWidthProperty =
DependencyProperty.Register("MaxTextWidth", typeof(double), typeof(OutlinedText),
new FrameworkPropertyMetadata(1000.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定将文本约束为特定高度
/// </summary>
public double MaxTextHeight
{
get { return (double)GetValue(MaxTextHeightProperty); }
set { SetValue(MaxTextHeightProperty, value); }
}
/// <summary>
/// 指定将文本约束为特定高度依赖属性
/// </summary>
public static readonly DependencyProperty MaxTextHeightProperty =
DependencyProperty.Register("MaxTextHeight", typeof(double), typeof(OutlinedText),
new FrameworkPropertyMetadata(1000.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定字体是否加粗。
/// </summary>
public bool Bold
{
get { return (bool)GetValue(BoldProperty); }
set { SetValue(BoldProperty, value); }
}
/// <summary>
/// 指定字体是否加粗依赖属性。
/// </summary>
public static readonly DependencyProperty BoldProperty = DependencyProperty.Register(
"Bold", typeof(bool), typeof(OutlinedText),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定填充字体的画刷颜色。
/// </summary>
///
public Brush Fill
{
get { return (Brush)GetValue(FillProperty); }
set { SetValue(FillProperty, value); }
}
/// <summary>
/// 指定填充字体的画刷颜色依赖属性。
/// </summary>
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
"Fill", typeof(Brush), typeof(OutlinedText),
new FrameworkPropertyMetadata(new SolidColorBrush(Colors.LightSteelBlue), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定文字显示的字体。
/// </summary>
public FontFamily Font
{
get { return (FontFamily)GetValue(FontProperty); }
set { SetValue(FontProperty, value); }
}
/// <summary>
/// 指定文字显示的字体依赖属性。
/// </summary>
public static readonly DependencyProperty FontProperty = DependencyProperty.Register(
"Font", typeof(FontFamily), typeof(OutlinedText),
new FrameworkPropertyMetadata(new FontFamily("Arial"), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定字体大小。
/// </summary>
public double FontSize
{
get { return (double)GetValue(FontSizeProperty); }
set { SetValue(FontSizeProperty, value); }
}
/// <summary>
/// 指定字体大小依赖属性。
/// </summary>
public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register(
"FontSize", typeof(double), typeof(OutlinedText),
new FrameworkPropertyMetadata((double)48.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定字体是否显示斜体字体样式。
/// </summary>
public bool Italic
{
get { return (bool)GetValue(ItalicProperty); }
set { SetValue(ItalicProperty, value); }
}
/// <summary>
/// 指定字体是否显示斜体字体样式依赖属性。
/// </summary>
public static readonly DependencyProperty ItalicProperty = DependencyProperty.Register(
"Italic", typeof(bool), typeof(OutlinedText),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定绘制空心字体边框画刷的颜色。
/// </summary>
public Brush Stroke
{
get { return (Brush)GetValue(StrokeProperty); }
set { SetValue(StrokeProperty, value); }
}
/// <summary>
/// 指定绘制空心字体边框画刷的颜色依赖属性。
/// </summary>
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
"Stroke", typeof(Brush), typeof(OutlinedText),
new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Teal), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定空心字体边框大小。
/// </summary>
public ushort StrokeThickness
{
get { return (ushort)GetValue(StrokeThicknessProperty); }
set { SetValue(StrokeThicknessProperty, value); }
}
/// <summary>
/// 指定空心字体边框大小依赖属性。
/// </summary>
public static readonly DependencyProperty StrokeThicknessProperty =
DependencyProperty.Register("StrokeThickness",
typeof(ushort), typeof(OutlinedText),
new FrameworkPropertyMetadata((ushort), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null)); /// <summary>
/// 指定要显示的文字字符串。
/// </summary>
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
/// <summary>
/// 指定要显示的文字字符串依赖属性。
/// </summary>
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(OutlinedText),
new FrameworkPropertyMetadata("",
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnOutlineTextInvalidated),
null)); #endregion #region Public Methods /// <summary>
/// 添加子对象。
/// </summary>
/// <param name="value">要添加的子对象。</param>
public void AddChild(Object value)
{ } /// <summary>
/// 将节点的文字内容添加到对象。
/// </summary>
/// <param name="value">要添加到对象的文字。</param>
public void AddText(string value)
{
Text = value;
} #endregion
}

WPF自定义空心文字的更多相关文章
- [WPF] 如何实现文字描边
1. 前言 WPF 的 TextBlock 提供了大部分常用的文字修饰方法,在日常使用中基本够用.如果需要更丰富的表现方式,WPF 也提供了其它用起来复杂一些的工具去实现这些需求.例如这篇文章介绍的文 ...
- WPF 自定义 MessageBox (相对完善版)
WPF 自定义 MessageBox (相对完善版) 基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当 ...
- WPF 自定义 MessageBox (相对完善版 v1.0.0.6)
基于WPF的自定义 MessageBox. 众所周知WPF界面美观.大多数WPF元素都可以简单的修改其样式,从而达到程序的风格统一.可是当你不得不弹出一个消息框通知用户消息时(虽然很不建议在程序中频繁 ...
- WPF自定义TextBox及ScrollViewer
原文:WPF自定义TextBox及ScrollViewer 寒假过完,在家真心什么都做不了,可能年龄大了,再想以前那样能专心坐下来已经不行了.回来第一件事就是改了项目的一个bug,最近又新增了一个新的 ...
- WPF自定义LED风格数字显示控件
原文:WPF自定义LED风格数字显示控件 版权声明:本文为博主原创文章,转载请注明作者和出处 https://blog.csdn.net/ZZZWWWPPP11199988899/article/de ...
- WPF 自定义柱状图 BarChart
WPF 自定义柱状图 当前的Telerik控件.DevExpress控件在图表控件方面做得不错,但是有时项目中需要特定的样式,不是只通过修改图表的模板和样式就能实现的. 或者说,通过修改当前的第三方控 ...
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- wpf 自定义圆形按钮
wpf 自定义圆形按钮 效果图 默认样式 获取焦点样式 点击样式 下面是实现代码: 一个是自定义控件类,一个是控件类皮肤 using System; using System.Collections. ...
- WPF自定义窗口基类
WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...
随机推荐
- ajax传参到实体类对应字段
之前看公司的代码,从页面ajax传参数到接口类,接口类用一个实体接收.我一直以为c#会自动识别,赋值到同名的字段. 我曾和朋友讨论过,双方辩论.朋友认为c#没有这种功能,没有这样高级匹配的机制.而我因 ...
- 一行代码调用实现带字段选取+条件判断+排序+分页功能的增强ORM框架
问题:3行代码 PDF.NET是一个开源的数据开发框架,它的特点是简单.轻量.快速,易上手,而且是一个注释完善的国产开发框架,受到不少朋友的欢迎,也在我们公司的项目中多次使用.但是,PDF.NET比起 ...
- Sharepoint2013:在页面上显示错误信息
在sharepoint2013中我们需要修改以下三处的web.config,以显示错误信息 1, C:\inetpub\wwwroot\wss\VirtualDirectories\端口号\web.c ...
- 分享一组很赞的 jQuery 特效【附源码下载】
作为最优秀的 JavaScript 库之一,jQuery 不仅使用简单灵活,同时还有许多成熟的插件可供选择,它可以帮助你在项目中加入漂亮的效果.这篇文章挑选了8个优秀的 jQuery 实例教程,这些 ...
- JBox - 模态窗口,工具提示和消息 jQuery 插件
jBox 是一个强大而灵活的 jQuery 插件,可以帮助实现模态窗口,工具提示,通知和更多的功能.你可以使用 jQuery 选择器轻松地添加工具提示效果到元素上,您可以以同样的方式设置模态窗口.该库 ...
- Dynatable – 基于 HTML5 & jQuery 的交互表格插件
Dynatable 一款有趣的,语义化,交互式的表格插件,使用 jQuery,HTML5 和 JSON 实现.Dynatable 的目的是提供一种简单的.可扩展的 API,能够轻松的浏览和操作大规模的 ...
- 汉王云名片识别(SM)组件开发详解
大家好,最近在DeviceOne平台上做了一个汉王云名片识别的功能组件.下面把我开发过程给大家做一个分享,希望可以帮助到大家. 下面我把我的思路给大家讲解一下. 1.找到我要集成的sdk,也就是汉 ...
- javascript 对象初探(二)--- 返回对象的函数
除了使用new操作符调用构造函数以外,我们也可以抛开new操作符,只用一般函数来创建对象,这样就能执行某些预备工作,并已对象为返回值的函数.. function her(){ return { nam ...
- SAP 使用较频繁的日期时间处理函数总结
在ABAP实际开发中,经常需要用到一些日期时间处理函数,个人感觉经常使用到的函数进行一下汇总 1. 根据工厂日历 计划交货日期 和 收货处理时间 来计算 销售计划中计划完工日期,其他类似日期计算等 ...
- GPS坐标互转:WGS-84(GPS)、GCJ-02(Google地图)、BD-09(百度地图)(转载)
WGS-84:是国际标准,GPS坐标(Google Earth使用.或者GPS模块)GCJ-02:中国坐标偏移标准,Google Map.高德.腾讯使用BD-09:百度坐标偏移标准,Baidu Map ...