WPF自定义空心文字

WPF自定义空心文字
原文:WPF自定义空心文字

  首先创建一个自定义控件,继承自FrameworkElement,“Generic.xaml”中可以不添加样式。

  要自定义空心文字,要用到绘制格式化文本FormattedText类。FormattedText对象提供的文本格式设置功能比WPF提供的已有文本控件提供的相应功能更为强大。调用FormattedText构造函数,可以传入相应的参数,得到我们想要的文本样式。使用 MaxTextWidth 属性可以将文本约束为特定宽度。 文本将自动换行,以避免超过指定宽度。 使用 MaxTextHeight 属性可以将文本约束为特定高度。 超过指定高度的文本将显示一个省略号“…”。

  接下来重写OnRender方法,在方法体中调用DrawingContext对象的DrawGeometry方法即可完成文本的绘制工作。

 

  1     public class OutlinedText : FrameworkElement, IAddChild
  2     {
  3         /// <summary>
  4         /// 静态构造函数
  5         /// </summary>
  6         static OutlinedText()
  7         {
  8             DefaultStyleKeyProperty.OverrideMetadata(typeof(OutlinedText), new FrameworkPropertyMetadata(typeof(OutlinedText)));
  9         }
 10 
 11 
 12         #region Private Fields
 13 
 14         /// <summary>
 15         /// 文字几何形状
 16         /// </summary>
 17         private Geometry m_TextGeometry;
 18 
 19         #endregion
 20 
 21 
 22         #region Private Methods
 23 
 24         /// <summary>     
 25         /// 当依赖项属性改变文字无效时,创建新的空心文字对象来显示。
 26         /// </summary>     
 27         /// <param name="d"></param>     
 28         /// <param name="e"></param>     
 29         private static void OnOutlineTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
 30         {
 31             if (Convert.ToString(e.NewValue) != Convert.ToString(e.OldValue))
 32             {
 33                 ((OutlinedText)d).CreateText();
 34             }
 35         }
 36 
 37         #endregion
 38 
 39 
 40         #region FrameworkElement Overrides
 41 
 42         /// <summary>     
 43         /// 重写绘制文字的方法。   
 44         /// </summary>     
 45         /// <param name="drawingContext">空心文字控件的绘制上下文。</param>     
 46         protected override void OnRender(DrawingContext drawingContext)
 47         {
 48             //CreateText();
 49             // 基于设置的属性绘制空心文字控件。         
 50             drawingContext.DrawGeometry(Fill, new Pen(Stroke, StrokeThickness), m_TextGeometry);
 51         }
 52 
 53         /// <summary>     
 54         /// 基于格式化文字创建文字的几何轮廓。    
 55         /// </summary>     
 56         public void CreateText()
 57         {
 58             FontStyle fontStyle = FontStyles.Normal;
 59             FontWeight fontWeight = FontWeights.Medium;
 60             if (Bold == true)
 61                 fontWeight = FontWeights.Bold;
 62             if (Italic == true)
 63                 fontStyle = FontStyles.Italic;
 64             // 基于设置的属性集创建格式化的文字。        
 65             FormattedText formattedText = new FormattedText(
 66                 Text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight,
 67                 new Typeface(Font, fontStyle, fontWeight, FontStretches.Normal),
 68                 FontSize, Brushes.Black);
 69             formattedText.MaxTextWidth = this.MaxTextWidth;
 70             formattedText.MaxTextHeight = this.MaxTextHeight;
 71             // 创建表示文字的几何对象。        
 72             m_TextGeometry = formattedText.BuildGeometry(new Point(0, 0));
 73             // 基于格式化文字的大小设置空心文字的大小。         
 74             this.MinWidth = formattedText.Width;
 75             this.MinHeight = formattedText.Height;
 76         }
 77 
 78         #endregion
 79 
 80 
 81         #region DependencyProperties
 82 
 83         /// <summary>
 84         /// 指定将文本约束为特定宽度
 85         /// </summary>
 86         public double MaxTextWidth
 87         {
 88             get { return (double)GetValue(MaxTextWidthProperty); }
 89             set { SetValue(MaxTextWidthProperty, value); }
 90         }
 91         /// <summary>
 92         /// 指定将文本约束为特定宽度依赖属性
 93         /// </summary>
 94         public static readonly DependencyProperty MaxTextWidthProperty =
 95             DependencyProperty.Register("MaxTextWidth", typeof(double), typeof(OutlinedText),
 96                 new FrameworkPropertyMetadata(1000.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
 97 
 98         /// <summary>
 99         /// 指定将文本约束为特定高度
100         /// </summary>
101         public double MaxTextHeight
102         {
103             get { return (double)GetValue(MaxTextHeightProperty); }
104             set { SetValue(MaxTextHeightProperty, value); }
105         }
106         /// <summary>
107         /// 指定将文本约束为特定高度依赖属性
108         /// </summary>
109         public static readonly DependencyProperty MaxTextHeightProperty =
110             DependencyProperty.Register("MaxTextHeight", typeof(double), typeof(OutlinedText),
111                  new FrameworkPropertyMetadata(1000.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
112 
113         /// <summary>     
114         /// 指定字体是否加粗。   
115         /// </summary>     
116         public bool Bold
117         {
118             get { return (bool)GetValue(BoldProperty); }
119             set { SetValue(BoldProperty, value); }
120         }
121         /// <summary>     
122         /// 指定字体是否加粗依赖属性。    
123         /// </summary>     
124         public static readonly DependencyProperty BoldProperty = DependencyProperty.Register(
125             "Bold", typeof(bool), typeof(OutlinedText),
126             new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
127 
128         /// <summary>     
129         /// 指定填充字体的画刷颜色。    
130         /// </summary>     
131         /// 
132         public Brush Fill
133         {
134             get { return (Brush)GetValue(FillProperty); }
135             set { SetValue(FillProperty, value); }
136         }
137         /// <summary>     
138         /// 指定填充字体的画刷颜色依赖属性。    
139         /// </summary>     
140         public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
141             "Fill", typeof(Brush), typeof(OutlinedText),
142             new FrameworkPropertyMetadata(new SolidColorBrush(Colors.LightSteelBlue), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
143 
144         /// <summary>     
145         /// 指定文字显示的字体。   
146         /// </summary>    
147         public FontFamily Font
148         {
149             get { return (FontFamily)GetValue(FontProperty); }
150             set { SetValue(FontProperty, value); }
151         }
152         /// <summary>     
153         /// 指定文字显示的字体依赖属性。     
154         /// </summary>     
155         public static readonly DependencyProperty FontProperty = DependencyProperty.Register(
156             "Font", typeof(FontFamily), typeof(OutlinedText),
157             new FrameworkPropertyMetadata(new FontFamily("Arial"), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
158 
159         /// <summary>     
160         /// 指定字体大小。
161         /// </summary>     
162         public double FontSize
163         {
164             get { return (double)GetValue(FontSizeProperty); }
165             set { SetValue(FontSizeProperty, value); }
166         }
167         /// <summary>     
168         /// 指定字体大小依赖属性。     
169         /// </summary>     
170         public static readonly DependencyProperty FontSizeProperty = DependencyProperty.Register(
171             "FontSize", typeof(double), typeof(OutlinedText),
172             new FrameworkPropertyMetadata((double)48.0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
173 
174         /// <summary>     
175         /// 指定字体是否显示斜体字体样式。  
176         /// </summary>     
177         public bool Italic
178         {
179             get { return (bool)GetValue(ItalicProperty); }
180             set { SetValue(ItalicProperty, value); }
181         }
182         /// <summary>     
183         /// 指定字体是否显示斜体字体样式依赖属性。   
184         /// </summary>     
185         public static readonly DependencyProperty ItalicProperty = DependencyProperty.Register(
186             "Italic", typeof(bool), typeof(OutlinedText),
187             new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
188 
189         /// <summary>     
190         /// 指定绘制空心字体边框画刷的颜色。    
191         /// </summary>     
192         public Brush Stroke
193         {
194             get { return (Brush)GetValue(StrokeProperty); }
195             set { SetValue(StrokeProperty, value); }
196         }
197         /// <summary>     
198         /// 指定绘制空心字体边框画刷的颜色依赖属性。    
199         /// </summary>     
200         public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
201             "Stroke", typeof(Brush), typeof(OutlinedText),
202             new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Teal), FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
203 
204         /// <summary>     
205         /// 指定空心字体边框大小。  
206         /// </summary>     
207         public ushort StrokeThickness
208         {
209             get { return (ushort)GetValue(StrokeThicknessProperty); }
210             set { SetValue(StrokeThicknessProperty, value); }
211         }
212         /// <summary>     
213         /// 指定空心字体边框大小依赖属性。      
214         /// </summary>     
215         public static readonly DependencyProperty StrokeThicknessProperty =
216             DependencyProperty.Register("StrokeThickness",
217             typeof(ushort), typeof(OutlinedText),
218             new FrameworkPropertyMetadata((ushort)0, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnOutlineTextInvalidated), null));
219 
220         /// <summary>    
221         /// 指定要显示的文字字符串。  
222         /// </summary>     
223         public string Text
224         {
225             get { return (string)GetValue(TextProperty); }
226             set { SetValue(TextProperty, value); }
227         }
228         /// <summary>     
229         /// 指定要显示的文字字符串依赖属性。  
230         ///  </summary>    
231         public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
232             "Text", typeof(string), typeof(OutlinedText),
233             new FrameworkPropertyMetadata("",
234                 FrameworkPropertyMetadataOptions.AffectsRender,
235                 new PropertyChangedCallback(OnOutlineTextInvalidated),
236                 null));
237 
238         #endregion
239 
240 
241         #region Public Methods
242 
243         /// <summary>
244         /// 添加子对象。
245         /// </summary>
246         /// <param name="value">要添加的子对象。</param>
247         public void AddChild(Object value)
248         { }
249 
250         /// <summary>
251         /// 将节点的文字内容添加到对象。
252         /// </summary>
253         /// <param name="value">要添加到对象的文字。</param>
254         public void AddText(string value)
255         {
256             Text = value;
257         }
258 
259         #endregion
260     }

 

 

 源码下载

 

posted on 2018-12-21 13:31 NET未来之路 阅读(...) 评论(...) 编辑 收藏

转载于:https://www.cnblogs.com/lonelyxmas/p/10155253.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/395670.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

php默认日志位置,Laravel 修改默认日志文件名称和位置的例子

修改默认日志位置我们平常的开发中可能一直把laravel的日志文件放在默认位置不会有什么影响&#xff0c;但如果我们的项目上线时是全量部署&#xff0c;每次部署都是git中最新的代码&#xff0c;那这个时候每次都会清空我们的日志&#xff0c;显示这不是我们所期望的&#xff0…

【转】UITableView详解(UITableViewCell

原文网址&#xff1a;http://www.kancloud.cn/digest/ios-1/107420 上一节中,我们定义的cell比较单一,只是单调的输入文本和插入图片,但是在实际开发中,有的cell上面有按钮,有的cell上面有滑动控件,有的cell上面有开关选项等等,具体参加下面2个图的对比: 我们可以通过…

Android 最简单的MVP案例;

随手撸个发出来&#xff1a; V&#xff1a;界面层 //界面层需要实现P.View方法&#xff0c;然后重写P.View中的方法&#xff1b;M层给的数据就在这些个方法的参数中&#xff1b; // 还要获取到P.Provide的实例&#xff0c;使用P.Provide去调用M层的方法&#xff1b; public cla…

c++编码风格指南_100%正确编码样式指南

c编码风格指南Tabs or spaces? Curly brace on the same line or a new line? 80 character width or 120?制表符或空格&#xff1f; 在同一行或新行上大括号&#xff1f; 80个字符的宽度还是120个字符&#xff1f; Coders love to argue about this kind of stuff. The ta…

Netty源码注释翻译-Channel类

定义为一个通往网络socket或者一个由I/O读写能力的组件。 通道提供&#xff1a; 1&#xff0c;通道的当前状态&#xff0c;打开&#xff1f;已连接&#xff1f; 2&#xff0c;跟通道关联的配置信息ChannelConfig&#xff0c;包括buffer大小等。 3&#xff0c;通道支持的I/O操作…

Today is weekend不是应该一定会输出吗

判断语句 If…else块&#xff0c;请看下面这个例子&#xff1a; <%! int day 3; %>                       //声明变量感叹号 <html> <head><title>IF...ELSE Example</title></head> <body> <% if (day …

时间模块和时间工具

一、time模块 三种格式 时间戳时间&#xff1a;浮点数 单位为秒 时间戳起始时间&#xff1a; 1970.1.1 0:0:0 英国伦敦时间 1970.1.1 8:0:0 我国(东8区) 结构化时间&#xff1a;元组(struct_time) 格式化时间&#xff1a;str数据类型的 1、常用方法 import timetime.sleep(secs…

redux扩展工具_用鸭子扩展您的Redux App

redux扩展工具How does your front-end application scale? How do you make sure that the code you’re writing is maintainable 6 months from now?您的前端应用程序如何扩展&#xff1f; 您如何确定您正在编写的代码从现在起6个月内可维护&#xff1f; Redux took the …

mac下源码安装redis

转载&#xff1a;http://www.jianshu.com/p/6b5eca8d908b 下载安装包 redis-3.0.7.tar.gz 官网地址&#xff1a;http://redis.io/download 解压&#xff1a;tar -zvxf redis-3.0.7.tar.gz 将解压后的文件夹放到 /usr/local目录下 编译测试:接下来在终端中切换到/usr/local/red…

代码扫描工具测试覆盖率工具

测试覆盖率工具转载于:https://www.cnblogs.com/vivian-test/p/5398289.html

php splqueue 5.5安装,解析PHP标准库SPL数据结构

SPL提供了双向链表、堆栈、队列、堆、降序堆、升序堆、优先级队列、定长数组、对象容器SplQueue 队列类进出异端&#xff0c;先进先出<?php $obj new SplQueue();//插入一个节点到top位置$obj->enqueue(1);$obj->enqueue(2);$obj->enqueue(3);/**SplQueue Object…

Beta阶段敏捷冲刺总结

设想和目标 1. 我们的软件要解决什么问题&#xff1f;是否定义得很清楚&#xff1f;是否对典型用户和典型场景有清晰的描述&#xff1f; 在最开始的时候我们就是为了解决集美大学计算机工程学院网页没有搜索引擎的问题。因为没有搜索引擎&#xff0c;在搜索内容时需要根据查找信…

c语言 二进制压缩算法_使用C ++解释的二进制搜索算法

c语言 二进制压缩算法by Pablo E. Cortez由Pablo E.Cortez 使用C 解释的二进制搜索算法 (Binary Search Algorithms Explained using C) Binary search is one of those algorithms that you’ll come across on every (good) introductory computer science class. It’s an …

【LATEX】个人版latex论文模板

以下是我的个人论文模板&#xff0c;运行环境为Xelatex(在线ide&#xff1a;Sharelatex.com) 鉴于本人常有插入程序的需求&#xff0c;故引用了lstlisting \RequirePackage{ifxetex} \ifxetex\documentclass[hyperref, UTF8, c5size, no-math, winfonts&#xff0c;a4paper]{ct…

初识virtual memory

一、先谈几个重要的东西 virtual memory是一个抽象概念&#xff0c;书上的原文是"an abstraction of main memory known as virtual memory"&#xff08;参考资料p776&#xff09;。那么什么是抽象概念。下面说说我个人对这个东西的理解。 所谓抽象概念是指抽象出来的…

java创建mysql驱动,JDBC之Java连接mysql实现增删改查

使用软件&#xff1a;mysql、eclipse链接步骤&#xff1a;1.注册驱动2.创建一个连接对象3.写sql语句4.执行sql语句并返回一个结果或者结果集5.关闭链接(一般就是connection、statement、setresult)这三个连接对象&#xff0c;关闭顺序一般是(setresult ---> statement …

算法第五章作业

1.你对回溯算法的理解&#xff08;2分&#xff09; 回溯法&#xff08;探索与回溯法&#xff09;是一种选优搜索法&#xff0c;又称为试探法&#xff0c;按选优条件向前搜索&#xff0c;以达到目标。但当探索到某一步时&#xff0c;发现原先选择并不优或达不到目标&#xff0c;…

c++编码风格指南_100%正确的编码样式指南

c编码风格指南Here are three links worth your time:这是三个值得您花费时间的链接&#xff1a; The 100% correct coding style guide (4 minute read) 100&#xff05;正确的编码样式指南( 阅读4分钟 ) I wrote a programming language. Here’s how you can, too (10 minu…

xp开机黑屏故障分析

今天装完xp系统之后&#xff0c;重启开机发现竟然黑屏了&#xff0c;查资料发现有很多用户在修改分辨率后&#xff0c;因显示器不支持修改后的分辨率&#xff0c;会出现电脑黑屏的情况。分辨率调高了&#xff0c;超出了屏幕的范围&#xff0c;肯定会黑屏&#xff0c;而且这个问…

应用程序图标_如何制作完美的应用程序图标

应用程序图标by Nabeena Mali通过Nabeena Mali 如何制作完美的应用程序图标 (How to Make the Perfect App Icon) With just 24 app icon slots on the first page of an iPhone home screen, or 28 if you have a fancy iPhone 7, creating the perfect app icon is a vital …