stringformatflags

更新时间:2023-03-01 16:15:30 阅读: 评论:0

关注我的朋友们应该知道,我最近开始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,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/zhishi/a/167765853077285.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:stringformatflags.doc

本文 PDF 下载地址:stringformatflags.pdf

相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 实用文体写作网旗下知识大全大全栏目是一个全百科类宝库! 优秀范文|法律文书|专利查询|