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

C# Winform實現(xiàn)繪制圓形進度條

 更新時間:2024年02月18日 14:34:44   作者:漂泊_人生  
這篇文章主要為大家詳細介紹了使用C# Winform實現(xiàn)繪制圓形進度條的相關(guān)知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編 一起學(xué)習(xí)一下

效果圖

實現(xiàn)代碼

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
 
namespace Net6_GeneralUiWinFrm
{
        public class CircularProgressBar : Control
        {
            private int progress = 0;
            private int borderWidth = 20; // 增加的邊框?qū)挾?
 
            public int Progress
            {
                get { return progress; }
                set
                {
                    progress = Math.Max(0, Math.Min(100, value)); // 確保進度值在0到100之間
                    Invalidate(); // Causes the control to be redrawn
                }
            }
 
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
 
                // Draw background circle
                using (Pen pen = new Pen(Color.LightGray, borderWidth))
                {
                    pen.DashStyle = DashStyle.Dot; // 設(shè)置點狀線條
                    e.Graphics.DrawEllipse(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth);
                }
 
                // Draw progress arc
                using (Pen pen = new Pen(Color.LightGreen, borderWidth)) //lightgreen
                {
                    pen.DashStyle = DashStyle.Solid; // 進度使用實線
                                                     // Calculate sweep angle
                    float sweepAngle = (360f * progress) / 100f;
                    e.Graphics.DrawArc(pen, borderWidth / 2, borderWidth / 2, this.Width - borderWidth, this.Height - borderWidth, -90, sweepAngle);
                }
 
                // Draw progress text
                string progressText = $"{progress}%";
                using (Font font = new Font("Arial", 12))
                using (Brush brush = new SolidBrush(Color.Black))
                {
                    SizeF textSize = e.Graphics.MeasureString(progressText, font);
                    // Calculate text position
                    PointF textPoint = new PointF((this.Width - textSize.Width) / 2, (this.Height - textSize.Height) / 2);
                    e.Graphics.DrawString(progressText, font, brush, textPoint);
                }
            }
        }
}

方法補充

除了上文的方法,小編還為大家整理了其他C#實現(xiàn)繪制圓形進度條的方法,希望對大家有所幫助

方法一:

首先,在你的WinForms項目中添加一個Panel控件作為進度條的容器。然后,可以通過重寫Panel的OnPaint事件來處理繪圖邏輯。

using System;  
using System.Drawing;  
using System.Windows.Forms;  
  
public class CircularProgressBar : Panel  
{  
    private int _progress;  
  
    public CircularProgressBar()  
    {  
        this.DoubleBuffered = true; // 雙緩沖以減少閃爍  
        this.SetStyle(ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);  
    }  
  
    public int Progress  
    {  
        get { return _progress; }  
        set  
        {  
            if (value < 0) value = 0;  
            if (value > 100) value = 100;  
  
            _progress = value;  
            Invalidate(); // 觸發(fā)重繪  
        }  
    }  
  
    protected override void OnPaint(PaintEventArgs e)  
    {  
        base.OnPaint(e);  
  
        int diameter = Math.Min(Width, Height) - 1; // 直徑等于寬度和高度的最小值減一  
        int radius = diameter / 2; // 半徑等于直徑的一半  
        int centerX = Width / 2; // 圓心X坐標(biāo)  
        int centerY = Height / 2; // 圓心Y坐標(biāo)  
  
        // 繪制進度條背景  
        using (Pen backgroundPen = new Pen(Color.LightGray, 10))  
        {  
            e.Graphics.DrawEllipse(backgroundPen, centerX - radius, centerY - radius, diameter, diameter);  
        }  
  
        // 繪制進度  
        int sweepAngle = 360 * Progress / 100; // 計算掃過的角度  
        using (Pen progressPen = new Pen(Color.Blue, 10))  
        {  
            e.Graphics.DrawArc(progressPen, centerX - radius, centerY - radius, diameter, diameter, -90, sweepAngle);  
        }  
  
        // 繪制進度文本(可選)  
        using (Font textFont = new Font("Arial", 12, FontStyle.Bold))  
        using (Brush textBrush = new SolidBrush(Color.Black))  
        {  
            string text = Progress + "%";  
            e.Graphics.DrawString(text, textFont, textBrush, centerX - e.Graphics.MeasureString(text, textFont).Width / 2, centerY - e.Graphics.MeasureString(text, textFont).Height / 2);  
        }  
    }  
}

然后,你可以在你的WinForms窗體上實例化并使用這個CircularProgressBar控件:

public partial class MainForm : Form  
{  
    private CircularProgressBar circularProgressBar;  
  
    public MainForm()  
    {  
        InitializeComponent();  
  
        circularProgressBar = new CircularProgressBar();  
        circularProgressBar.Dock = DockStyle.Fill;  
        circularProgressBar.Progress = 50; // 設(shè)置初始進度為50%  
        this.Controls.Add(circularProgressBar);  
  
        // 你可以通過定時器或其他方式來動態(tài)改變進度  
        // 例如:  
        // System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();  
        // timer.Interval = 1000; // 1秒  
        // timer.Tick += (sender, e) => {  
        //     circularProgressBar.Progress = (circularProgressBar.Progress + 1) % 100;  
        // };  
        // timer.Start();  
    }  
}

請注意,這只是一個簡單的示例,你可以根據(jù)自己的需求調(diào)整樣式、顏色、字體等屬性,甚至添加動畫效果。如果你需要更復(fù)雜的儀表盤控件,可能需要考慮使用第三方庫或自定義繪制更復(fù)雜的圖形元素。

方法二:

完整代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace HZH_Controls.Controls
{
    public partial class UCProcessEllipse : UserControl
    {
        [Description("值改變事件"), Category("自定義")]
        public event EventHandler ValueChanged;

        private Color m_backEllipseColor = Color.FromArgb(22, 160, 133);
        /// <summary>
        /// 圓背景色
        /// </summary>
        [Description("圓背景色"), Category("自定義")]
        public Color BackEllipseColor
        {
            get { return m_backEllipseColor; }
            set
            {
                m_backEllipseColor = value;
                Refresh();
            }
        }

        private Color m_coreEllipseColor = Color.FromArgb(180, 180, 180);
        /// <summary>
        /// 內(nèi)圓顏色,ShowType=Ring 有效
        /// </summary>
        [Description("內(nèi)圓顏色,ShowType=Ring 有效"), Category("自定義")]
        public Color CoreEllipseColor
        {
            get { return m_coreEllipseColor; }
            set
            {
                m_coreEllipseColor = value;
                Refresh();
            }
        }

        private Color m_valueColor = Color.FromArgb(255, 77, 59);

        [Description("值圓顏色"), Category("自定義")]
        public Color ValueColor
        {
            get { return m_valueColor; }
            set
            {
                m_valueColor = value;
                Refresh();
            }
        }

        private bool m_isShowCoreEllipseBorder = true;
        /// <summary>
        /// 內(nèi)圓是否顯示邊框,ShowType=Ring 有效
        /// </summary>
        [Description("內(nèi)圓是否顯示邊框,ShowType=Ring 有效"), Category("自定義")]
        public bool IsShowCoreEllipseBorder
        {
            get { return m_isShowCoreEllipseBorder; }
            set
            {
                m_isShowCoreEllipseBorder = value;
                Refresh();
            }
        }

        private ValueType m_valueType = ValueType.Percent;
        /// <summary>
        /// 值文字類型
        /// </summary>
        [Description("值文字類型"), Category("自定義")]
        public ValueType ValueType
        {
            get { return m_valueType; }
            set
            {
                m_valueType = value;
                Refresh();
            }
        }

        private int m_valueWidth = 30;
        /// <summary>
        /// 外圓值寬度
        /// </summary>
        [Description("外圓值寬度,ShowType=Ring 有效"), Category("自定義")]
        public int ValueWidth
        {
            get { return m_valueWidth; }
            set
            {
                if (value <= 0 || value > Math.Min(this.Width, this.Height))
                    return;
                m_valueWidth = value;
                Refresh();
            }
        }

        private int m_valueMargin = 5;
        /// <summary>
        /// 外圓值間距
        /// </summary>
        [Description("外圓值間距"), Category("自定義")]
        public int ValueMargin
        {
            get { return m_valueMargin; }
            set
            {
                if (value < 0 || m_valueMargin >= m_valueWidth)
                    return;
                m_valueMargin = value;
                Refresh();
            }
        }

        private int m_maxValue = 100;
        /// <summary>
        /// 最大值
        /// </summary>
        [Description("最大值"), Category("自定義")]
        public int MaxValue
        {
            get { return m_maxValue; }
            set
            {
                if (value > m_value || value <= 0)
                    return;
                m_maxValue = value;
                Refresh();
            }
        }

        private int m_value = 0;
        /// <summary>
        /// 當(dāng)前值
        /// </summary>
        [Description("當(dāng)前值"), Category("自定義")]
        public int Value
        {
            get { return m_value; }
            set
            {
                if (m_maxValue < value || value <= 0)
                    return;
                m_value = value;
                if (ValueChanged != null)
                {
                    ValueChanged(this, null);
                }
                Refresh();
            }
        }
        private Font m_font = new Font("Arial Unicode MS", 20);
        [Description("文字字體"), Category("自定義")]
        public override Font Font
        {
            get
            {
                return m_font;
            }
            set
            {
                m_font = value;
                Refresh();
            }
        }
        Color m_foreColor = Color.White;
        [Description("文字顏色"), Category("自定義")]
        public override Color ForeColor
        {
            get
            {
                return m_foreColor;
            }
            set
            {
                m_foreColor = value;
                Refresh();
            }
        }

        private ShowType m_showType = ShowType.Ring;

        [Description("顯示類型"), Category("自定義")]
        public ShowType ShowType
        {
            get { return m_showType; }
            set
            {
                m_showType = value;
                Refresh();
            }
        }

        public UCProcessEllipse()
        {
            InitializeComponent();
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.DoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.Selectable, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.SetStyle(ControlStyles.UserPaint, true);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            var g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;  //使繪圖質(zhì)量最高,即消除鋸齒
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;

            int intWidth = Math.Min(this.Size.Width, this.Size.Height);
            //底圓
            g.FillEllipse(new SolidBrush(m_backEllipseColor), new Rectangle(new Point(0, 0), new Size(intWidth, intWidth)));
            if (m_showType == HZH_Controls.Controls.ShowType.Ring)
            {
                //中心圓
                int intCore = intWidth - m_valueWidth * 2;
                g.FillEllipse(new SolidBrush(m_coreEllipseColor), new Rectangle(new Point(m_valueWidth, m_valueWidth), new Size(intCore, intCore)));
                //中心圓邊框
                if (m_isShowCoreEllipseBorder)
                {
                    g.DrawEllipse(new Pen(m_valueColor, 2), new Rectangle(new Point(m_valueWidth + 1, m_valueWidth + 1), new Size(intCore - 1, intCore - 1)));
                }
                if (m_value > 0 && m_maxValue > 0)
                {
                    float fltPercent = (float)m_value / (float)m_maxValue;
                    if (fltPercent > 1)
                    {
                        fltPercent = 1;
                    }

                    g.DrawArc(new Pen(m_valueColor, m_valueWidth - m_valueMargin * 2), new RectangleF(new Point(m_valueWidth / 2 + m_valueMargin / 4, m_valueWidth / 2 + m_valueMargin / 4), new SizeF(intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1), intWidth - m_valueWidth - m_valueMargin / 2 + (m_valueMargin == 0 ? 0 : 1))), -90, fltPercent * 360);

                    string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
                    System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
                    g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
                }
            }
            else
            {
                if (m_value > 0 && m_maxValue > 0)
                {
                    float fltPercent = (float)m_value / (float)m_maxValue;
                    if (fltPercent > 1)
                    {
                        fltPercent = 1;
                    }

                    g.FillPie(new SolidBrush(m_valueColor), new Rectangle(m_valueMargin, m_valueMargin, intWidth - m_valueMargin * 2, intWidth - m_valueMargin * 2), -90, fltPercent * 360);

                    string strValueText = m_valueType == HZH_Controls.Controls.ValueType.Percent ? fltPercent.ToString("0%") : m_value.ToString();
                    System.Drawing.SizeF _txtSize = g.MeasureString(strValueText, this.Font);
                    g.DrawString(strValueText, this.Font, new SolidBrush(this.ForeColor), new PointF((intWidth - _txtSize.Width) / 2 + 1, (intWidth - _txtSize.Height) / 2 + 1));
                }
            }

        }
    }

    public enum ValueType
    {
        /// <summary>
        /// 百分比
        /// </summary>
        Percent,
        /// <summary>
        /// 數(shù)值
        /// </summary>
        Absolute
    }

    public enum ShowType
    {
        /// <summary>
        /// 圓環(huán)
        /// </summary>
        Ring,
        /// <summary>
        /// 扇形
        /// </summary>
        Sector
    }
}
namespace HZH_Controls.Controls
{
    partial class UCProcessEllipse
    {
        /// <summary>
        /// 必需的設(shè)計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region 組件設(shè)計器生成的代碼

        /// <summary>
        /// 設(shè)計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內(nèi)容。
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        }

        #endregion
    }
}

到此這篇關(guān)于C# Winform實現(xiàn)繪制圓形進度條的文章就介紹到這了,更多相關(guān)C#圓形進度條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • C#和lua相互調(diào)用的方法教程

    C#和lua相互調(diào)用的方法教程

    lua是一種腳本語言,可以方便的移植到各種宿主語言中,并且可以支持熱更新,在游戲開發(fā)中也能當(dāng)做主要的語言來編寫游戲的邏輯,所以這篇文章主要給大家介紹了關(guān)于C#和lua相互調(diào)用的方法教程,需要的朋友可以參考下。
    2017-11-11
  • 一篇文章帶你輕松了解C# Lock關(guān)鍵字

    一篇文章帶你輕松了解C# Lock關(guān)鍵字

    這篇文章主要給大家介紹了如何通過一篇文章帶你輕松了解C# Lock關(guān)鍵字的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Unity3D實現(xiàn)扭動擠壓瀏覽效果

    Unity3D實現(xiàn)扭動擠壓瀏覽效果

    這篇文章主要為大家詳細介紹了Unity3D實現(xiàn)扭動擠壓瀏覽效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02
  • Unity實現(xiàn)見縫插針小游戲

    Unity實現(xiàn)見縫插針小游戲

    這篇文章主要為大家詳細介紹了Unity實現(xiàn)見縫插針小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C#實現(xiàn)解壓GZip文件的方法

    C#實現(xiàn)解壓GZip文件的方法

    這篇文章主要介紹了C#實現(xiàn)解壓GZip文件的方法,涉及C#操作壓縮文件的技巧,需要的朋友可以參考下
    2015-05-05
  • c#批量整理xml格式示例

    c#批量整理xml格式示例

    這篇文章主要介紹了c#批量整理xml格式示例,win7的x64和x86系統(tǒng)下已驗證通過,需要的朋友可以參考下
    2014-03-03
  • C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法

    C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法

    這篇文章主要介紹了C#利用System.Uri轉(zhuǎn)URL為絕對地址的方法,涉及C#操作URL的技巧,非常具有實用價值,需要的朋友可以參考下
    2015-02-02
  • C#中使用WinRAR實現(xiàn)加密壓縮及解壓縮文件

    C#中使用WinRAR實現(xiàn)加密壓縮及解壓縮文件

    這篇文章主要介紹了C#中使用WinRAR實現(xiàn)加密壓縮及解壓縮文件,本文直接給出實例代碼,代碼中包含詳細注釋,需要的朋友可以參考下
    2015-07-07
  • C#實現(xiàn)字符串模糊匹配的方法小結(jié)

    C#實現(xiàn)字符串模糊匹配的方法小結(jié)

    在C#中實現(xiàn)字符串的模糊匹配可以借助正則表達式或者一些模糊匹配算法來實現(xiàn),文章通過代碼示例講解的非常詳細,對大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-07-07
  • C# MemoryStream類案例詳解

    C# MemoryStream類案例詳解

    這篇文章主要介紹了C# MemoryStream類案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08

最新評論