最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實(shí)現(xiàn)自定義單選和復(fù)選按鈕樣式

 更新時(shí)間:2022年12月23日 09:40:47   作者:芝麻粒兒  
這篇文章主要為大家詳細(xì)介紹了如何利用C#實(shí)現(xiàn)定義單選和復(fù)選按鈕樣式,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下

實(shí)踐過程

效果

代碼

public partial class GlorifyCheckBox : CheckBox
    {
        public GlorifyCheckBox()
        {
            InitializeComponent();
            FontAspect = getAspect(); //獲取當(dāng)前控件文本的讀取方向
        }

        #region 變量

        private bool FontAspect = false; //判斷字體的方向
        private int Measurement = 255; //設(shè)置漸變的初始值
        LinearGradientBrush Periphery_br; //外圓的顏色
        LinearGradientBrush Central_br; //移入控件時(shí)中圓的顏色
        LinearGradientBrush NoCentral_br; //無操作時(shí)中圓的顏色

        #endregion

        #region 添加屬性

        public enum StyleSort
        {
            Null = 0, //無
            Integer = 1, //整數(shù)
            Decimal = 2, //小數(shù)
        }

        private Color TPeripheryColor = Color.DarkBlue;

        [Browsable(true), Category("設(shè)置填充顏色"), Description("外圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
        public Color PeripheryColor
        {
            get { return TPeripheryColor; }
            set
            {
                TPeripheryColor = value;
                this.Invalidate();
            }
        }

        private Color TCentralColor = Color.CornflowerBlue;

        [Browsable(true), Category("設(shè)置填充顏色"), Description("移入控件時(shí)中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
        public Color CentralColor
        {
            get { return TCentralColor; }
            set
            {
                TCentralColor = value;
                this.Invalidate();
            }
        }

        private Color TNoCentralColor = Color.PowderBlue;

        [Browsable(true), Category("設(shè)置填充顏色"), Description("無操作時(shí)中圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
        public Color NoCentralColor
        {
            get { return TNoCentralColor; }
            set
            {
                TNoCentralColor = value;
                this.Invalidate();
            }
        }

        private Color TStippleColor = Color.DarkBlue;

        [Browsable(true), Category("設(shè)置填充顏色"), Description("選中后內(nèi)圓的顏色")] //在“屬性”窗口中顯示DataStyle屬性
        public Color StippleColor
        {
            get { return TStippleColor; }
            set
            {
                TStippleColor = value;
                this.Invalidate();
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 控件在需要重繪時(shí)觸發(fā)
        /// </summary>
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle); //填充矩形
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; //清除鋸齒
            //獲取左面圖標(biāo)的區(qū)域
            Rectangle boxrect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y,
                SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
            //獲取繪制的文本的區(qū)域
            Rectangle strrect = new Rectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width,
                e.ClipRectangle.Y, e.ClipRectangle.Width + 5 - SystemInformation.SmallIconSize.Width,
                e.ClipRectangle.Height);
            if (FontAspect) //判斷字體的讀取方式
            {
                boxrect.X = e.ClipRectangle.X + e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width; //設(shè)置橢圓的位置
                strrect.X = e.ClipRectangle.X; //設(shè)置字體位置
            }

            Point MousePos = this.PointToClient(Control.MousePosition); //獲取鼠標(biāo)的位置
            bool Above = e.ClipRectangle.Contains(MousePos); //獲取鼠標(biāo)是否在當(dāng)前控件上

            DrawBox(e.Graphics, boxrect, Above); //繪制單選圖案
            DrawText(e.Graphics, strrect); //繪制文字
            if (!Enabled)
                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), e.ClipRectangle);
        }

        /// <summary>
        /// 鼠標(biāo)移入控件的可見區(qū)域時(shí)觸發(fā)
        /// </summary>
        protected override void OnMouseEnter(System.EventArgs e)
        {
            base.OnMouseEnter(e);
            this.Invalidate();
        }

        /// <summary>
        /// 鼠標(biāo)移出控件的可見區(qū)域時(shí)觸發(fā)
        /// </summary>
        protected override void OnMouseLeave(System.EventArgs e)
        {
            base.OnMouseLeave(e);
            this.Invalidate();
        }

        /// <summary>
        /// RightToLeft屬性值更改時(shí)發(fā)生
        /// </summary>
        protected override void OnRightToLeftChanged(System.EventArgs e)
        {
            base.OnRightToLeftChanged(e);
            FontAspect = getAspect();
            this.Invalidate();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 繪制單選控件的圖案
        /// </summary>
        /// <param g="Graphics">封裝一個(gè)繪圖的類對象</param>
        /// <param rect="Rectangle">單選圖案的繪制區(qū)域</param> 
        /// <param Above="bool">斷判鼠標(biāo)是否在控件上方</param> 
        private void DrawBox(Graphics g, Rectangle rect, bool Above)
        {
            //設(shè)置外橢圓的漸變色
            int opacity = Measurement;
            Periphery_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 2, PeripheryColor),
                Color.FromArgb(opacity, PeripheryColor), LinearGradientMode.ForwardDiagonal);
            //設(shè)置中間橢圓形選中時(shí)的漸變色
            opacity = (int) (.4f * opacity + .5f);
            Central_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, CentralColor),
                Color.FromArgb(opacity, CentralColor), LinearGradientMode.ForwardDiagonal);
            //設(shè)置中間橢圓形無操作時(shí)的漸變色
            opacity = (int) (.4f * opacity + .5f);
            NoCentral_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, NoCentralColor),
                Color.FromArgb(opacity, NoCentralColor), LinearGradientMode.ForwardDiagonal);
            int size = this.Font.Height; //獲取字體的高度
            //獲取外橢圓的區(qū)域
            Rectangle box = new Rectangle(rect.X + ((rect.Width - size) / 2), rect.Y + ((rect.Height - size) / 2),
                size - 2, size - 2);
            Rectangle glyph = new Rectangle(box.X + 3, box.Y + 3, box.Width - 6, box.Height - 6); //設(shè)置內(nèi)圓的繪制區(qū)域
            Rectangle right = new Rectangle(box.X, box.Y - 1, box.Width + 2, box.Height + 2);
            g.FillEllipse(new SolidBrush(SystemColors.Window), box); //以白色填充單選圖案 

            if (this.CheckState != CheckState.Unchecked) //如果是選中狀態(tài)
            {
                base.ForeColor = Color.DarkBlue;
                ControlPaint.DrawMenuGlyph(g, right, MenuGlyph.Checkmark, this.StippleColor, Color.White); //繪制對號
                g.DrawRectangle(new Pen(new SolidBrush(SystemColors.Control), (float) (3)), box); //繪制外橢圓
            }

            if (this.CheckState == CheckState.Indeterminate)
                g.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), right);

            if (Above && this.Enabled) //如果鼠標(biāo)移入該控件
            {
                g.DrawRectangle(new Pen(Central_br, 2),
                    new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //繪制中心橢圓
            }
            else
            {
                g.DrawRectangle(new Pen(NoCentral_br, 2),
                    new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //繪制中心橢圓
            }

            g.DrawRectangle(new Pen(Periphery_br, (float) (1.5)), box); //繪制外橢圓
        }

        /// <summary>
        /// 繪制文本
        /// </summary>
        /// <param g="Graphics">封裝一個(gè)繪圖的類對象</param>
        /// <param rect="Rectangle">繪制文本的區(qū)域</param>
        private void DrawText(Graphics g, Rectangle rect)
        {
            StringFormat tem_StringF = new StringFormat();
            tem_StringF.Alignment = StringAlignment.Near;
            tem_StringF.LineAlignment = StringAlignment.Center; //文本居中對齊
            if (FontAspect)
                tem_StringF.FormatFlags = StringFormatFlags.DirectionRightToLeft; //按從左到右的順序顯示文本
            //g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);//繪制文本
            if (!FontAspect)
                g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF); //繪制文本
            else
            {
                rect.X = rect.X - SystemInformation.SmallIconSize.Width / 2 + 2;
                g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);
            }
        }

        /// <summary>
        /// 獲取文本的讀取方向
        /// </summary>
        /// <return>布爾型</return>
        private bool getAspect()
        {
            bool tem_Aspect = SystemInformation.RightAlignedMenus;
            if (this.RightToLeft == RightToLeft.Yes) //從右到左進(jìn)行讀取
                tem_Aspect = true;
            if (this.RightToLeft == RightToLeft.No) //從左到右進(jìn)行讀取
                tem_Aspect = false;
            return tem_Aspect;
        }

        #endregion
    }

到此這篇關(guān)于C#實(shí)現(xiàn)自定義單選和復(fù)選按鈕樣式的文章就介紹到這了,更多相關(guān)C#自定義按鈕樣式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果

    UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果

    這篇文章主要為大家詳細(xì)介紹了UnityShader3實(shí)現(xiàn)轉(zhuǎn)圈與冷卻效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • C#使用Dynamic實(shí)現(xiàn)簡化反射

    C#使用Dynamic實(shí)現(xiàn)簡化反射

    這篇文章主要為大家詳細(xì)介紹了C#如何使用Dynamic來實(shí)現(xiàn)簡化反射,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下
    2023-07-07
  • c# 使用OpenCV識別硬幣

    c# 使用OpenCV識別硬幣

    這篇文章主要介紹了c# 使用OpenCV識別硬幣的方法,幫助大家更好的利用c#進(jìn)行深度學(xué)習(xí),感興趣的朋友可以了解下
    2020-12-12
  • WPF+ASP.NET?SignalR實(shí)現(xiàn)簡易在線聊天功能的示例代碼

    WPF+ASP.NET?SignalR實(shí)現(xiàn)簡易在線聊天功能的示例代碼

    這篇文章將以一個(gè)簡單的聊天示例,簡述如何通過WPF+ASP.NET?SignalR實(shí)現(xiàn)消息后臺通知,僅供學(xué)習(xí)分享使用,如有不足之處,還請指正
    2022-09-09
  • C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實(shí)例

    C#使用HtmlAgilityPack抓取糗事百科內(nèi)容實(shí)例

    這篇文章主要介紹了C#使用HtmlAgilityPack抓取糗事百科內(nèi)容的方法,實(shí)例分析了C#中HtmlAgilityPack的相關(guān)使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • 使用C#9中records作為強(qiáng)類型ID的實(shí)例教程

    使用C#9中records作為強(qiáng)類型ID的實(shí)例教程

    這篇文章主要給大家介紹了關(guān)于使用C#9中records作為強(qiáng)類型ID的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • C# 如何生成 DataMatrix 格式的二維碼

    C# 如何生成 DataMatrix 格式的二維碼

    該文主要是利用OnBarcode.dll 生成DataMatrix 格式的二維碼的一些簡單方法和操作技巧,對C# 如何生成 DataMatrix 格式的二維碼相關(guān)知識感興趣的朋友一起看看吧
    2021-11-11
  • c# 接口interface基礎(chǔ)入門小例子

    c# 接口interface基礎(chǔ)入門小例子

    用于描述類的功能,類似于契約,指示了類將:執(zhí)行的工作,形參類型,返回結(jié)果類型,但本身沒有執(zhí)行的代碼
    2013-04-04
  • C#生成防偽碼的思路及源碼分享

    C#生成防偽碼的思路及源碼分享

    生成防偽碼其實(shí)挺簡單,但是如果要考慮效率和不重復(fù)的話,就需要稍微動(dòng)動(dòng)腦子了,下面我來說說我的思路及源碼
    2014-06-06
  • C#中跨線程訪問控件的實(shí)現(xiàn)方法

    C#中跨線程訪問控件的實(shí)現(xiàn)方法

    C#中不允許跨線程直接訪問界面控件,即一個(gè)線程中如主線程創(chuàng)建的控件不允許被其他線程例如子線程直接訪問,在一個(gè)線程中設(shè)置其他線程所有的控件屬性通常有兩種方法,本文將詳細(xì)的給大家介紹一下,需要的朋友可以參考下
    2024-12-12

最新評論

绥德县| 葫芦岛市| 兰州市| 乐都县| 广宁县| 香港| 溧水县| 吉林市| 安国市| 米易县| 宜城市| 卓资县| 扎囊县| 上林县| 江川县| 平陆县| 道孚县| 辰溪县| 准格尔旗| 盘锦市| 凤冈县| 伊川县| 五莲县| 琼结县| 宁乡县| 岳西县| 金溪县| 木兰县| 海城市| 九龙城区| 疏附县| 兴安县| 许昌市| 珠海市| 临海市| 图木舒克市| 隆林| 海宁市| 容城县| 吴忠市| 高清|