關注我的朋友們應該知道,我最近開始C# Winform自定義控件的學習,上一篇文章完成了一個最基本面板的控件,見鏈接C#學習隨筆—自定義控件(面板)。
今天,我要分享的是自定義控件Label,在Winform自定義的Label中有一定局限性,舉幾個例子,比如,Label控件的換行就比較麻煩,Label文字中的對齊格式固定為居中對齊,Label沒有邊框,等等。那么自定義的Label控件,主要是解決上述問題。
讀過我上一篇文章的朋友們應該知道,C#學習隨筆—自定義控件(面板)里面的基本屬性中包含了邊框的顏色,寬度,虛實樣式,邊框的形狀以及背景底色的調整(甚至是顏色的漸變)。因此在Label控件中只需要繼承面板的這些屬性即可實現這些功能,如下:
public partial class SimplyCtrlLabel : SimplyCtrlBa
因此,我們主要實現兩點,一個是Label文字的對齊格式,如居中,靠左或者靠右,或者是邊框想要隨著文字多少的變化而變化。另外一個是Label Text的換行。
第一個關于Label文字的各種格式,我們需要對文本進行重繪,而且是在SimplyCtrlBa控件的基礎上進行重繪,那么,需要怎么做呢?只需要增加重繪的調用事件即可,如下:
public SimplyCtrlLabel(){ InitializeComponent(); //Set Default Property. //Paint Text bad on Paint Event this.Paint += SimplyCtrlLabel_Paint;}private void SimplyCtrlLabel_Paint(object nder,PaintEventArgs e){ //文本重繪函數}
另外一個是Label文字的換行,我們需要在重繪函數中,對label的Text進行處理,識別到“ ”字符串,然后將其變為轉義字符" "。
我這里先展示一下,我實現的控件的樣子,如下所示:
Label控件不同屬性的示例
直接上最后的代碼:
using System;using System.Collections.Generic;using System.ComponentModel;using System.Drawing;using System.Data;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace MyLib_Simply{ public partial class SimplyCtrlLabel : SimplyCtrlBa { private bool _multiLineFlag = fal; [Description("Whether Label Text is multi-line,
reprent change line in the LabelFontText"),Category("LabelUrDefined")] public bool MultiLineFlag { get { return _multiLineFlag; } t { _multiLineFlag = value; } } //邊框高度自動適應 private bool _autoSuitHeight = fal; [Description("Whether Label Height is suitable for Text"),Category("LabelUrDefined")] public bool AutoSuitHeight { get { return _autoSuitHeight; } t { _autoSuitHeight = value; } } //邊框寬度自動適應 private bool _autoSuitWidth = fal; [Description("Whether Label Width is suitable for Text"),Category("LabelUrDefined")] public bool AutoSuitWidth { get { return _autoSuitWidth; } t { _autoSuitWidth = value; } } private string _labelFontText = "Label"; [Description("Label Text"),Category("LabelUrDefined")] public string LabelFontText { get { return _labelFontText; } t { _labelFontText = value; } } //Text上下對齊方式 private StringAlignment _textHightAlignment = StringAlignment.Center; [Description("Label Text Height Alignment"),Category("LabelUrDefined")] public StringAlignment TextHightAlignment { get { return _textHightAlignment; } t { _textHightAlignment = value; } } //Text左右對齊方式 private StringAlignment _textWidthAlignment = StringAlignment.Center; [Description("Label Text Width Alignment"),Category("LabelUrDefined")] public StringAlignment TextWidthAlignment { get { return _textWidthAlignment; } t { _textWidthAlignment = value; } } public SimplyCtrlLabel() { InitializeComponent(); //Set Default Property. this.IsBorderShow = fal; this.IsRoundCorner = fal; this.IsKindsColor = fal; this.BackColor = SystemColors.Control; //Paint Text bad on Paint Event this.Paint += SimplyCtrlLabel_Paint; } private void SimplyCtrlLabel_Paint(object nder,PaintEventArgs e) { string label_text_modify = _labelFontText; if (_multiLineFlag) { //換行顯示對Text進行處理。 label_text_modify = multi_line_text(_labelFontText); } StringFormat textformat = new StringFormat(); textformat.Alignment = _textWidthAlignment; textformat.LineAlignment = _textHightAlignment; if (this.RightToLeft == RightToLeft.Yes)//Check Layout is right to left or left to right { textformat.FormatFlags = StringFormatFlags.DirectionRightToLeft; } if ((_autoSuitHeight == fal) && (_autoSuitWidth == fal)) { } el { //測量String的寬度和高度,讓邊框自適應 int border_width, border_height; border_width = this.Width; border_height = this.Height; Graphics g = this.CreateGraphics(); g.PageUnit = GraphicsUnit.Pixel; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; StringFormat sf = new StringFormat(); sf.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; SizeF string_size = g.MeasureString(label_text_modify, this.Font, 1500, sf); if (_autoSuitHeight == true) { this.Height = Convert.ToInt32(string_size.Height)+2 ; } if (_autoSuitWidth == true) { this.Width = Convert.ToInt32(string_size.Width)+5 ; } } e.Graphics.DrawString(label_text_modify, this.Font, new SolidBrush(this.ForeColor), new RectangleF(1, 1, this.Width - 2, this.Height - 2), textformat); } //check label text and find out '
',then convert to correct string private string multi_line_text(string text) { string text_modfiy=text; if (text != String.Empty) { string text_temp = text; text_modfiy = String.Empty; //@表示
為非轉義字符 while (text_temp.IndexOf(@"
") >= 0) { text_modfiy += text_temp.Substring(0, text_temp.IndexOf(@"
"))+"
"; text_temp = text_temp.Substring(text_temp.IndexOf(@"
") + 2); } text_modfiy += text_temp; } return text_modfiy; } }}
今天Winform的C#自定義控件Label就分享到這里。希望可以幫到大家。
本文發布于:2023-02-28 20:09:00,感謝您對本站的認可!
本文鏈接:http://www.newhan.cn/zhishi/a/167765853077285.html
版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。
本文word下載地址:stringformatflags.doc
本文 PDF 下載地址:stringformatflags.pdf
| 留言與評論(共有 0 條評論) |