• <em id="6vhwh"><rt id="6vhwh"></rt></em>

    <style id="6vhwh"></style>

    <style id="6vhwh"></style>
    1. <style id="6vhwh"></style>
        <sub id="6vhwh"><p id="6vhwh"></p></sub>
        <p id="6vhwh"></p>
          1. 国产亚洲欧洲av综合一区二区三区 ,色爱综合另类图片av,亚洲av免费成人在线,久久热在线视频精品视频,成在人线av无码免费,国产精品一区二区久久毛片,亚洲精品成人片在线观看精品字幕 ,久久亚洲精品成人av秋霞

            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,感謝您對本站的認可!

            本文鏈接:http://m.newhan.cn/zhishi/a/167765853077285.html

            版權聲明:本站內容均來自互聯網,僅供演示用,請勿用于商業和其他非法用途。如果侵犯了您的權益請與我們聯系,我們將在24小時內刪除。

            本文word下載地址:stringformatflags.doc

            本文 PDF 下載地址:stringformatflags.pdf

            相關文章
            留言與評論(共有 0 條評論)
               
            驗證碼:
            Copyright ?2019-2022 Comsenz Inc.Powered by ? 實用文體寫作網旗下知識大全大全欄目是一個全百科類寶庫! 優秀范文|法律文書|專利查詢|
            主站蜘蛛池模板: 免费看黄色片| 激,情四虎欧美视频图片| 又湿又紧又大又爽a视频| 国产精品久久久久精品日日| 欧美视频在线播放观看免费福利资源| 国产精品亚洲一区二区三区| 嫩草研究院久久久精品| 亚洲丰满熟女一区二区v| 亚洲av无码专区在线亚| 在线中文字幕人妻视频| 久久99日韩国产精品久久99| 性无码专区一色吊丝中文字幕| 欧美性大战xxxxx久久久√| 被绑在坐桩机上抹春药| 人妻出轨av中文字幕| 中文字幕人成人乱码亚洲| 亚洲精品综合第一国产综合| 久久精品国产亚洲成人av| 国内揄拍国内精品对久久| 国产人成亚洲第一网站在线播放| 成人AV专区精品无码国产| 国产三级精品三级在线观看| 麻豆成人久久精品二区三| 中文字幕人妻av第一区| 色噜噜亚洲男人的天堂| 国内永久福利在线视频图片| 亚洲高清中文字幕在线看不卡| 中文字幕午夜福利片午夜福利片97| 她也色tayese在线视频| 国产精品福利无圣光一区二区| av免费看网站在线观看| 最近中文字幕完整版2019| 久久国产精品精品国产色| 免费午夜无码片在线观看影院| 国产午夜福利小视频在线| 深夜av免费在线观看| 国产日产欧产系列| 国产日韩欧美一区二区东京热| 亚洲国产成人无码网站| 亚洲国产精品午夜福利| 久久精品国产99久久久古代|