欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Winform控件優(yōu)化之圓角按鈕2

 更新時(shí)間:2022年08月29日 09:57:38   作者:代碼迷途  
這篇文章主要介紹了Winform控件優(yōu)化之圓角按鈕2,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下

前言

接上一篇Winform控件優(yōu)化之圓角按鈕1繼續(xù)介紹圓角按鈕的實(shí)現(xiàn)和優(yōu)化,以及這個(gè)過(guò)程中遇到的問(wèn)題...

圓角按鈕實(shí)現(xiàn)的進(jìn)一步優(yōu)化

【最終實(shí)現(xiàn),兼容默認(rèn)按鈕】

主要功能【圓角方面】

結(jié)合前面兩部分介紹和代碼,最終優(yōu)化實(shí)現(xiàn)ButtonPro按鈕(繼承自Button),既提供Button原生功能,又提供擴(kuò)展功能,除了圓角以外,還實(shí)現(xiàn)了圓形、圓角矩形的腳尖效果、邊框大小和顏色、背景漸變顏色、圓角的繪制模式和創(chuàng)建Region模式、常用按鈕圖標(biāo)快速使用(此項(xiàng)暫未實(shí)現(xiàn))等其他功能。

圓角按鈕控件相關(guān)屬性和實(shí)現(xiàn):

  • RoundRadius:圓角半徑,>=0時(shí)啟用圓角按鈕,等于0為直角(但可使用背景色等所有Round圓角相關(guān)屬性),<0時(shí)使用默認(rèn)Button樣式。
  • RegionNewModel:創(chuàng)建新Region的模式,使用'繪制范圍'創(chuàng)建新的Region,實(shí)現(xiàn)控件區(qū)域貼合繪制范圍,實(shí)現(xiàn)圖形外的部分"正確的透明",但相對(duì)會(huì)有些鋸齒。
  • ShowCusp:是否顯示尖角,默認(rèn)不顯示,當(dāng)啟用Radius圓角(RoundRadius>=0)時(shí)才有效。
  • CuspAlign:(三角)尖角的顯示位置,當(dāng)啟用圓角按鈕(RoundRadius>=0),且顯示尖角時(shí)有效。
  • RoundBorderWidth:?jiǎn)⒂肦adius圓角(RoundRadius>=0)時(shí)邊框?qū)挾?,默認(rèn)0。
  • RoundBorderColor:?jiǎn)⒂肦adius圓角(RoundRadius>=0)時(shí)邊框顏色,默認(rèn)黑色。
  • EnableBGGradient:?jiǎn)⒂脻u變背景色(需要RoundRadius>=0),啟用后RoundNormalColor、RoundHoverColor、RoundPressedColor顏色無(wú)效。
  • GradientModel:線(xiàn)性漸變的模式,默認(rèn)垂直漸變。
  • BGColorBegin:漸變開(kāi)始色。
  • BGColorEnd:漸變結(jié)束色。
  • RoundNormalColor:?jiǎn)⒂肦adius圓角(RoundRadius>=0)時(shí)按鈕標(biāo)準(zhǔn)顏色。
  • RoundHoverColor:?jiǎn)⒂肦adius圓角(RoundRadius>=0)鼠標(biāo)位于按鈕上時(shí)的按鈕顏色。
  • RoundPressedColor:?jiǎn)⒂肦adius圓角(RoundRadius>=0)鼠標(biāo)按下時(shí)的按鈕顏色。
  • 圓形按鈕,長(zhǎng)寬一樣,圓角半徑為長(zhǎng)寬的一半,可實(shí)現(xiàn)圓形按鈕。
  • 擴(kuò)展控件屬性分類(lèi)修改為“高級(jí)”,使用CategoryAttribute特性。

注意:

borderPen繪制線(xiàn)條的對(duì)齊方式:borderPen.Alignment = PenAlignment.Inset;。但是指定Inset繪制邊框也有小問(wèn)題,如果是重新創(chuàng)建Region,則繪制邊框后內(nèi)部變?yōu)橹苯蔷匦危ㄏ忍畛湓倮L制邊框線(xiàn)條);如果使用就有Region(不新建),也會(huì)變成內(nèi)部直角,并且如果不指定Inset則會(huì)外部繪制的Border線(xiàn)條變成直接(由原本直角的Region承載圓角之外的部分)??傊加行﹩?wèn)題,可自行測(cè)試。

在繪制邊框時(shí),不推薦使用PenAlignment.Inset,通過(guò)計(jì)算減少Rectangle的范圍為半個(gè)Border的路徑范圍(寬高-RoundBorderWidth),繪制時(shí)在路徑內(nèi)外正好有一個(gè)Border的大小來(lái)實(shí)現(xiàn)。而且這樣不會(huì)發(fā)生上面介紹的內(nèi)或外直角而非圓角的情況 

OnPaint方法中不要使用e.ClipRectangle

應(yīng)該使用控件的寬高(Width、Height)計(jì)算Rectangle矩形,或者使用ClientRectangle屬性。

相互影響的問(wèn)題,復(fù)制的或拖拽的ButtonPro控件,會(huì)被其他控件影響,即調(diào)整某個(gè)控件,會(huì)導(dǎo)致ButtonPro的一個(gè)或多個(gè)也會(huì)牽連變化(很雜亂的關(guān)聯(lián)變化),如何解決?應(yīng)該是獨(dú)立的才對(duì)!且在設(shè)計(jì)器中沒(méi)法通過(guò)撤銷(xiāo)操作還原效果

與SetStyle設(shè)置無(wú)關(guān),后面測(cè)試和重新指定Region有關(guān),在后續(xù)測(cè)試發(fā)現(xiàn)最終導(dǎo)致此類(lèi)問(wèn)題的原因,在于使用了OnPaint參數(shù)e.ClipRectangle作為控件繪制范圍繪制產(chǎn)生的?!颈热?code>rect = e.Graphics.DrawRoundRectAndCusp(e.ClipRectangle, roundRadius, baseColor, showCusp, cuspAlign)】

直接使用控件的WidthHeight定義繪制繪制范圍,替換到e.ClipRectangle。

OnPaint方法中不要使用e.ClipRectangle作為控件繪制范圍

以下問(wèn)題均是由于OnPaint方法中使用e.ClipRectangle來(lái)繪制繪制范圍導(dǎo)致的(它是個(gè)自動(dòng)被影響的值,應(yīng)該使用Width、Height創(chuàng)建)

不重新賦值Region,拖動(dòng)或調(diào)整按鈕,會(huì)重寫(xiě)顯示相互影響:

如果賦值新的Region,當(dāng)調(diào)整或移動(dòng)控件時(shí),就有可能影響顯示的布局或大小,且無(wú)法通過(guò)撤銷(xiāo)還原

重新創(chuàng)建Region的鋸齒問(wèn)題和優(yōu)勢(shì)

【寫(xiě)錯(cuò)代碼實(shí)現(xiàn)圓形導(dǎo)致的錯(cuò)誤探索,但也很有意義】

重新賦值Region一個(gè)最大的確實(shí)是會(huì)產(chǎn)生一些鋸齒,即使使用抗鋸齒和最好質(zhì)量繪制,要想繪制圓形效果,必須重新賦值Region,否則控件只會(huì)是圓角。因此,提供了RoundCircleModel屬性,用于是否啟用可繪制圓形按鈕的模式,會(huì)有些鋸齒,默認(rèn)不啟用,如果需要時(shí)啟用即可。

需要記住的幾點(diǎn):

  • Region定義的是控件的區(qū)域,通過(guò)GDI+繪制可以實(shí)現(xiàn)一個(gè)自定義的區(qū)域,從而解除默認(rèn)的寬高矩形區(qū)域控件的限制。
  • 重新定義和賦值Region的缺點(diǎn)是會(huì)產(chǎn)生一定鋸齒,這個(gè)鋸齒是Region產(chǎn)生的,而不是GDI+繪制填充產(chǎn)生的。
  • 無(wú)法消除創(chuàng)建的Region鋸齒,至少?zèng)]提供相關(guān)API,因此實(shí)際重繪控件時(shí),通常不要?jiǎng)?chuàng)建新的Region
  • 由于設(shè)置中使用了Winform透明父控件的樣式,因此要注意其正確的父控件設(shè)置。

直接使用繪制后的正確的繪制范圍創(chuàng)建新的Region區(qū)域,則沒(méi)有透明父控件的問(wèn)題,可以實(shí)現(xiàn)“正確的透明”,具體可參加下圖所示【新建Region繪制圖形后圓角邊緣部分出現(xiàn)的1像素的控件顏色可以通過(guò)調(diào)整創(chuàng)建Region時(shí)和繪制時(shí)的范圍消除(多余的1像素白邊問(wèn)題無(wú)法簡(jiǎn)單的通過(guò)調(diào)整Region和繪制范圍解決,具體可自行測(cè)試)】。 

【若是消除新建Region的鋸齒問(wèn)題,將會(huì)非常完美】

若是能實(shí)現(xiàn)直接繪制無(wú)鋸齒的圓角Region區(qū)域,則,直接在Paint事件中實(shí)現(xiàn)控件的圓角Region即可,無(wú)需在額外重新繪制背景和文字?!靖?jiǎn)單、更完美的方案】

目前所知,無(wú)法對(duì)Region進(jìn)行抗鋸齒,即使使用使用GDI+的APICreateRoundRectRgn方法。相關(guān)介紹參見(jiàn) c# How to make smooth arc region using graphics pathWinforms: Smooth the rounded edges for panel

控件上重繪可以使用Graphics對(duì)象在背景透明的Region區(qū)域控件上實(shí)現(xiàn),其背后至少一個(gè)父控件(或頂層的Form窗體)。

但是,對(duì)于一個(gè)Form,要想實(shí)現(xiàn)圓角或多邊形窗體,則必須重新生成Region,但創(chuàng)建Region在不規(guī)則形狀時(shí)邊緣鋸齒無(wú)法解決(如果有大牛,應(yīng)該可以應(yīng)用消除鋸齒的算法),后面會(huì)介紹一種取巧或者Win32窗體推薦的一種方式,即,使用Layered Windows。

代碼具體實(shí)現(xiàn)

將繪制方法精簡(jiǎn)為擴(kuò)展方法后,擴(kuò)展控件的全部源代碼如下:

擴(kuò)展方法參見(jiàn)Winform控件優(yōu)化Paint事件實(shí)現(xiàn)圓角組件及提取繪制圓角的方法

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CMControls
{
    public class ButtonPro : Button
    {
        private int roundRadius;//半徑 

        private bool showCusp = false;//顯示尖角
        private RectangleAlign cuspAlign = RectangleAlign.RightTop;//三角尖角位置

        private Color roundBorderColor = Color.Black;//邊框顏色
        private int roundBorderWidth = 0;//邊框?qū)挾?

        private Color roundHoverColor = Color.FromArgb(220, 80, 80);//鼠標(biāo)位于控件時(shí)顏色
        private Color roundNormalColor = Color.FromArgb(51, 161, 224);//基顏色
        private Color roundPressedColor = Color.FromArgb(251, 161, 0);//鼠標(biāo)按下控件時(shí)基顏色

        // 鼠標(biāo)相對(duì)控件的狀態(tài)位置,對(duì)應(yīng)上面不同顏色
        private MouseControlState mouseControlState = MouseControlState.Normal;

        private bool regionNewModel = false; // 創(chuàng)建新Region的模式,使用"繪制范圍"創(chuàng)建新的Region,實(shí)現(xiàn)控件區(qū)域貼合繪制范圍,實(shí)現(xiàn)圖形外的部分"正確的透明",但相對(duì)會(huì)有些鋸齒

        private Color beginBGColor; //= Color.FromArgb(251, 161, 0);//漸變開(kāi)始色
        private Color endBGColor; //= Color.FromArgb(251, 161, 0);//漸變結(jié)束色
        private bool enableBGGradient = false; //使用漸變色
        private LinearGradientMode gradientModel = LinearGradientMode.Vertical; //線(xiàn)性漸變的模式

        private Region originRegion;

        /// <summary>
        /// 圓形按鈕的半徑屬性
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(20), Description("圓角半徑,>=0時(shí)啟用圓角按鈕,等于0為直角(但可使用背景色等所有Round圓角相關(guān)屬性),<0時(shí)使用默認(rèn)Button樣式")]
        public int RoundRadius
        {
            set
            {
                roundRadius = value;
                // 使控件的整個(gè)畫(huà)面無(wú)效并重繪控件
                this.Invalidate();
            }
            get
            {
                return roundRadius;
            }
        }
        /// <summary>
        /// 圓角下創(chuàng)建新Region模式
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(false), Description("創(chuàng)建新Region的模式,使用'繪制范圍'創(chuàng)建新的Region,實(shí)現(xiàn)控件區(qū)域貼合繪制范圍,實(shí)現(xiàn)'正確的透明',但相對(duì)會(huì)有些的鋸齒")]
        public bool RegionNewModel
        {
            set
            {
                regionNewModel = value;
                this.Invalidate();
            }
            get
            {
                return regionNewModel;
            }
        }

        /// <summary>
        /// 三角尖角位置,當(dāng)啟用圓角
        /// </summary>
        [CategoryAttribute("高級(jí)"), Description("(三角)尖角的顯示位置,當(dāng)啟用圓角按鈕(RoundRadius>=0),且顯示尖角時(shí)有效"), DefaultValue(RectangleAlign.RightTop)]
        public RectangleAlign CuspAlign
        {
            set
            {
                cuspAlign = value;
                this.Invalidate();
            }
            get
            {
                return cuspAlign;
            }
        }
        [CategoryAttribute("高級(jí)"), Description("是否顯示尖角,默認(rèn)不顯示,當(dāng)啟用Radius圓角(RoundRadius>=0)時(shí)才有效"), DefaultValue(false)]
        public bool ShowCusp
        {
            set
            {
                showCusp = value;
                this.Invalidate();
            }
            get
            {
                return showCusp;
            }
        }

        [CategoryAttribute("高級(jí)"), DefaultValue(0), Description("啟用Radius圓角(RoundRadius>=0)時(shí)邊框?qū)挾?,默認(rèn)0")]
        public int RoundBorderWidth
        {
            set
            {
                roundBorderWidth = value;
                this.Invalidate();
            }
            get
            {
                return roundBorderWidth;
            }
        }

        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "0, 0, 0"), Description("啟用Radius圓角(RoundRadius>=0)時(shí)邊框顏色,默認(rèn)黑色")]
        public Color RoundBorderColor
        {
            get
            {
                return this.roundBorderColor;
            }
            set
            {
                this.roundBorderColor = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 是否啟用背景漸變色,啟用后RoundNormalColor、RoundHoverColor、RoundPressedColor顏色無(wú)效
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(false), Description("啟用漸變背景色(需要RoundRadius>=0),啟用后RoundNormalColor、RoundHoverColor、RoundPressedColor顏色無(wú)效")]
        public bool EnableBGGradient
        {
            get
            {
                return this.enableBGGradient;
            }
            set
            {
                this.enableBGGradient = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 線(xiàn)性漸變的模式,默認(rèn)垂直漸變
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(LinearGradientMode.Vertical), Description("線(xiàn)性漸變的模式,默認(rèn)垂直漸變")]
        public LinearGradientMode GradientModel
        {
            get
            {
                return this.gradientModel;
            }
            set
            {
                this.gradientModel = value;
                this.Invalidate();
            }
        }

        /// <summary>
        /// 背景漸變色
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "0, 122, 204"), Description("漸變開(kāi)始色")]
        public Color BGColorBegin
        {
            get
            {
                return this.beginBGColor;
            }
            set
            {
                this.beginBGColor = value;
                this.Invalidate();
            }
        }
        /// <summary>
        /// 背景漸變色
        /// </summary>
        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "8, 39, 57"), Description("漸變結(jié)束色")]
        public Color BGColorEnd
        {
            get
            {
                return this.endBGColor;
            }
            set
            {
                this.endBGColor = value;
                this.Invalidate();
            }
        }

        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "51, 161, 224"), Description("啟用Radius圓角(RoundRadius>=0)時(shí)按鈕標(biāo)準(zhǔn)顏色")]
        public Color RoundNormalColor
        {
            get
            {
                return this.roundNormalColor;
            }
            set
            {
                this.roundNormalColor = value;
                this.Invalidate();
            }
        }
        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "220, 80, 80"), Description("啟用Radius圓角(RoundRadius>=0)鼠標(biāo)位于按鈕上時(shí)的按鈕顏色")]
        public Color RoundHoverColor
        {
            get
            {
                return this.roundHoverColor;
            }
            set
            {
                this.roundHoverColor = value;
                this.Invalidate();
            }
        }

        [CategoryAttribute("高級(jí)"), DefaultValue(typeof(Color), "251, 161, 0"), Description("啟用Radius圓角(RoundRadius>=0)鼠標(biāo)按下時(shí)的按鈕顏色")]
        public Color RoundPressedColor
        {
            get
            {
                return this.roundPressedColor;
            }
            set
            {
                this.roundPressedColor = value;
                this.Invalidate();
            }
        }
        protected override void OnMouseEnter(EventArgs e)//鼠標(biāo)進(jìn)入時(shí)
        {
            mouseControlState = MouseControlState.Hover;//Hover
            base.OnMouseEnter(e);
        }
        protected override void OnMouseLeave(EventArgs e)//鼠標(biāo)離開(kāi)
        {
            mouseControlState = MouseControlState.Normal;//正常
            base.OnMouseLeave(e);
        }
        protected override void OnMouseDown(MouseEventArgs e)//鼠標(biāo)按下
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)//鼠標(biāo)左鍵且點(diǎn)擊次數(shù)為1
            {
                mouseControlState = MouseControlState.Pressed;//按下的狀態(tài)
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)//鼠標(biāo)彈起
        {
            if (e.Button == MouseButtons.Left && e.Clicks == 1)
            {
                if (ClientRectangle.Contains(e.Location))//控件區(qū)域包含鼠標(biāo)的位置
                {
                    mouseControlState = MouseControlState.Hover;
                }
                else
                {
                    mouseControlState = MouseControlState.Normal;
                }
            }
            base.OnMouseUp(e);
        }
        public ButtonPro()
        {
            ForeColor = Color.White;

            this.FlatStyle = FlatStyle.Flat;
            this.FlatAppearance.BorderSize = 0;

            FlatAppearance.MouseDownBackColor = Color.Transparent;
            FlatAppearance.MouseOverBackColor = Color.Transparent;
            FlatAppearance.CheckedBackColor = Color.Transparent;

            RoundRadius = 20; // 似乎當(dāng)值為默認(rèn)20時(shí)重新生成設(shè)計(jì)器或者重新打開(kāi)項(xiàng)目后,此屬性就會(huì)變?yōu)?,必須在構(gòu)造函數(shù)中指定20來(lái)解決

            this.mouseControlState = MouseControlState.Normal;

            // 原始Region
            originRegion = Region;
        }

        public override void NotifyDefault(bool value)
        {
            base.NotifyDefault(false); // 去除窗體失去焦點(diǎn)時(shí)最新激活的按鈕邊框外觀樣式
        }

        //重寫(xiě)OnPaint
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            //base.OnPaintBackground(e);


            // 不能使用 e.ClipRectangle.GetRoundedRectPath(_radius) 計(jì)算控件全部的Region區(qū)域,e.ClipRectangle 似乎是變化的,必須使用固定的Width和Height,包括下面的繪制也不能使用e.ClipRectangle
            // 在Paint事件中也不推薦使用e.ClipRectangle時(shí)沒(méi)問(wèn)題的
            Rectangle controlRect = new Rectangle(0, 0, this.Width, this.Height);

            // roundRadius 修改回來(lái)是要還原
            if (roundRadius >= 0 && regionNewModel) // 圓角下創(chuàng)建新Region模式,使用自定義Region
            {
                var controlPath = controlRect.GetRoundedRectPath(roundRadius);

                // 要在繪制之前指定Region,否則無(wú)效
                this.Region = new Region(controlPath);
            }
            else // 修改對(duì)應(yīng)調(diào)整
            {
                //this.Region = new Region(controlRect);//也屬于重新修改
                this.Region = originRegion;
            }

            if (roundRadius >= 0)
            {
                Rectangle rect;

                if (enableBGGradient)
                {
                    rect = e.Graphics.DrawRoundRectAndCusp(controlRect, roundRadius, beginBGColor, endBGColor, true, CuspAlign, gradientModel, roundBorderWidth > 0 ? new Pen(roundBorderColor, roundBorderWidth) : null);
                }
                else
                {
                    Color baseColor;

                    switch (mouseControlState)
                    {
                        case MouseControlState.Hover:
                            baseColor = this.roundHoverColor;
                            break;
                        case MouseControlState.Pressed:
                            baseColor = this.roundPressedColor;
                            break;
                        case MouseControlState.Normal:
                            baseColor = this.roundNormalColor;
                            break;
                        default:
                            baseColor = this.roundNormalColor;
                            break;
                    }
                    rect = e.Graphics.DrawRoundRectAndCusp(controlRect, roundRadius, baseColor, showCusp, cuspAlign, roundBorderWidth > 0 ? new Pen(roundBorderColor, roundBorderWidth) : null);

                }

                // 使用合適的區(qū)域
                e.Graphics.DrawText(rect, Text, ForeColor, Font, TextAlign);
            }
        }
    }
}

測(cè)試擴(kuò)展按鈕控件ButtonPro

通過(guò)拖拽ButtonPro按鈕控件,調(diào)整各個(gè)參數(shù),查看不同樣式的按鈕效果。

TextRenderer.DrawText繪制文本

文本垂直居中偏上的問(wèn)題及文字大小不正確【推薦使用TextRenderer.DrawText繪制文本】

所有的一切都非常好,但是,目前還有一個(gè)小問(wèn)題,就是繪制垂直居中的文本時(shí),可以明顯看到偏上方。是的由此產(chǎn)生“瑕疵”。

目前沒(méi)有找到很好的解決辦法,更換字體、字體大小為偶數(shù)會(huì)有一定效果,但并不能完全解決。

使用StringFormat.GenericTypographic

后面經(jīng)過(guò)花木蘭控件庫(kù)的大佬提醒,使用StringFormat.GenericTypographic作為文本繪制的格式對(duì)象,可以看到偏上的問(wèn)題有了明顯改善。

        using (StringFormat strF = StringFormat.GenericTypographic)
        {
            // 文字布局
            switch (_textAlign)
            {
                //...
            }
            g.DrawString(text, font, brush, rect, strF);
        }

雖然如此,但是還是有一點(diǎn)點(diǎn)不完全垂直。而且對(duì)比同樣字體情況下,DrawString繪制出來(lái)的文本明顯和原生Button時(shí)顯示的文字有很大差別(大小、清晰度)

僅僅重寫(xiě)OnPaintBackground

【無(wú)效果】

后面由于文字繪制的問(wèn)題,想著直接重寫(xiě)OnPaintBackground,文字交由Winform自己繪制,應(yīng)該可以達(dá)到很好的效果。

但是,但是重寫(xiě)OnPaintBackground后背景沒(méi)有任何效果,僅僅是設(shè)置的透明背景,無(wú)法實(shí)現(xiàn)圓角等各種繪制。

目前暫時(shí)不知道該如何正確的處理OnPaintBackground方法。

使用TextRenderer.DrawText繪制文本

【不推薦Graphics.DrawString】

后來(lái)幾乎要放棄了,因?yàn)樽罱K繪制的文字確實(shí)很不理想,和原生Button對(duì)比起來(lái)差好多。。。

然后想著測(cè)試下TextRenderer.DrawText()繪制文本的效果如何,最終發(fā)現(xiàn)文字繪制效果非常好(大小正確、清晰),重點(diǎn)是文字位置的水平和垂直居中沒(méi)有任何問(wèn)題,基本和原生Button的文字效果一致。

// ...
TextRenderer.DrawText(g, text, font, rect, color, formatFlags);

擴(kuò)展方法的源碼參見(jiàn)Winform控件優(yōu)化Paint事件實(shí)現(xiàn)圓角組件及提取繪制圓角的方法

到此這篇關(guān)于Winform控件優(yōu)化之圓角按鈕2的文章就介紹到這了,更多相關(guān)Winform 圓角按鈕內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#使用GDI+創(chuàng)建縮略圖實(shí)例

    C#使用GDI+創(chuàng)建縮略圖實(shí)例

    這篇文章主要介紹了C#使用GDI+創(chuàng)建縮略圖實(shí)例,是C#程序設(shè)計(jì)中非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • C#操作SQLite數(shù)據(jù)庫(kù)幫助類(lèi)詳解

    C#操作SQLite數(shù)據(jù)庫(kù)幫助類(lèi)詳解

    這篇文章主要介紹了C#操作SQLite數(shù)據(jù)庫(kù)幫助類(lèi),詳細(xì)分析了C#針對(duì)sqlite數(shù)據(jù)庫(kù)的連接、查詢(xún)、分頁(yè)等各種常見(jiàn)操作的實(shí)現(xiàn)與封裝技巧,需要的朋友可以參考下
    2017-07-07
  • C#生成不重復(fù)隨機(jī)數(shù)列表實(shí)例

    C#生成不重復(fù)隨機(jī)數(shù)列表實(shí)例

    C#生成不重復(fù)隨機(jī)數(shù)列表實(shí)例的代碼,需要的朋友可以參考一下
    2013-02-02
  • c# 異步編程基礎(chǔ)講解

    c# 異步編程基礎(chǔ)講解

    這篇文章主要介紹了c# 異步編程的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-04-04
  • C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄

    C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄

    這篇文章主要介紹了C# 實(shí)現(xiàn)在當(dāng)前目錄基礎(chǔ)上找到上一層目錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • 關(guān)于C#?調(diào)用Dll?傳遞字符串指針參數(shù)的問(wèn)題

    關(guān)于C#?調(diào)用Dll?傳遞字符串指針參數(shù)的問(wèn)題

    這篇文章主要介紹了C#?調(diào)用Dll傳遞字符串指針參數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-01-01
  • 如何隨機(jī)選取一個(gè)機(jī)器的ip

    如何隨機(jī)選取一個(gè)機(jī)器的ip

    此方法可用于當(dāng)需要負(fù)載均衡時(shí),即想選擇多臺(tái)機(jī)器來(lái)處理程序執(zhí)行。 隨機(jī)的選擇一臺(tái)機(jī)器來(lái)處理。
    2013-03-03
  • Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè)

    Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè)

    這篇文章主要為大家詳細(xì)介紹了Unity相機(jī)移動(dòng)之屏幕邊緣檢測(cè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02
  • 利用Distinct()內(nèi)置方法對(duì)List集合的去重問(wèn)題詳解

    利用Distinct()內(nèi)置方法對(duì)List集合的去重問(wèn)題詳解

    這篇文章主要給大家介紹了關(guān)于利用Distinct()內(nèi)置方法對(duì)List集合的去重問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • C#實(shí)現(xiàn)插入排序

    C#實(shí)現(xiàn)插入排序

    這篇文章介紹了C#實(shí)現(xiàn)插入排序的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-08-08

最新評(píng)論