• <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://www.newhan.cn/zhishi/a/167765853077285.html

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

            本文word下載地址:stringformatflags.doc

            本文 PDF 下載地址:stringformatflags.pdf

            相關文章
            留言與評論(共有 0 條評論)
               
            驗證碼:
            Copyright ?2019-2022 Comsenz Inc.Powered by ? 實用文體寫作網旗下知識大全大全欄目是一個全百科類寶庫! 優秀范文|法律文書|專利查詢|
            主站蜘蛛池模板: 日本久久一区二区三区高清| mm1313亚洲国产精品| 日韩三级手机在线观看不卡| 亚洲欧洲日韩国内高清| 亚洲欧美国产另类首页| 色视频不卡一区二区三区| 日韩人妻无码精品久久| 亚洲夂夂婷婷色拍ww47| 午夜免费无码福利视频麻豆| 国产麻豆精品福利在线| 亚洲无av在线中文字幕| 亚洲熟妇丰满xxxxx小品| 国产精品制服丝袜第一页| 亚洲午夜天堂| 中文字幕av熟女人妻| 国产明星精品无码AV换脸| 激情内射人妻一区二区| 极品粉嫩小泬无遮挡20p| 亚洲中文字幕无线乱码va| 国产av一区二区亚洲精品| 亚洲精品电影院| 久久久精品94久久精品| 无码人妻丝袜在线视频| 一级毛片在线观看免费| 亚洲欧美人成电影在线观看| 天天爽天天爽天天爽| 亚洲avav天堂av在线网爱情| 精品国产成人国产在线视| 视频一区二区三区四区五区| 暖暖影院日本高清...免费| 内射老阿姨1区2区3区4区| 丰满的女邻居2| 人妻精品动漫h无码| 在线观看AV永久免费| 国产精品网红尤物福利在线观看| 动漫AV纯肉无码AV电影网| 男人深夜影院无码观看| 狠狠色噜噜狠狠狠狠777米奇| 年轻漂亮的人妻被公侵犯bd免费版| 久久国产精品伊人青青草| 久久久一本精品99久久精品88|