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.pswp.cn/news/395670.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/395670.shtml
英文地址,請注明出處:http://en.pswp.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 …