winforms 如何制作垂直进度条

j2qf4p5b  于 2022-12-19  发布在  其他
关注(0)|答案(4)|浏览(165)

我正在尝试制作一个垂直进度条,我知道没有任何简单的方法来做到这一点。
我在论坛上看到过这样的代码:

public class VerticalProgressBar : ProgressBar
{ 
    protected override CreateParams CreateParams 
    { 
        get 
        { 
            CreateParams cp = base.CreateParams; 
            cp.Style |= 0x04; 
            return cp; 
        } 
     } 
 }

我的问题是我应该把这些代码放在哪里?它应该放在我的program.cs文件中还是进度条所在的窗体中?

ruarlubt

ruarlubt1#

将代码放在哪里并不重要,只需确保在Form.Designer.cs文件中创建VerticalProgressBar
你必须改变

private System.Windows.Forms.ProgressBar progressBar1

private VerticalProgressBar progressBar1

(or不管它叫什么)和

this.progressBar1 = new System.Windows.Forms.ProgressBar();

this.progressBar1 = new VerticalProgressBar();
jogvjijk

jogvjijk2#

在VS 2022中仍然没有这个控件,所以我使用了一列两行的TablePanelLayout,并且只是更改了RowStyle SizeType百分比。当达到限制时,您可以轻松地添加代码来更改面板颜色等。

public class BarGraph : Panel
   {
       private Panel panel1;
       private Panel panel2;
       private TableLayoutPanel table;
       private float percentValue;

       public BarGraph()
       {
           table = new TableLayoutPanel();
           table.ColumnCount = 1;
           table.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F));
           table.Name = "tableLayoutPanel1";
           table.RowCount = 2;
           table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
           table.RowStyles.Add(new RowStyle(SizeType.Percent, 50F));
           table.Dock = DockStyle.Fill;
           table.Margin = new Padding(0);
           table.Padding = new Padding(0);
           table.CellBorderStyle = TableLayoutPanelCellBorderStyle.None;

           panel1 = new Panel();
           panel2 = new Panel();

           panel1.AutoSize = false;
           panel1.Dock = DockStyle.Fill;
           panel1.Padding = new Padding(0);
           panel1.Margin = new Padding(0);
           panel2.AutoSize = false;
           panel2.Dock = DockStyle.Fill;
           panel2.Padding = new Padding(0);
           panel2.Margin = new Padding(0);

           panel1.BackColor = System.Drawing.Color.White;
           panel2.BackColor = System.Drawing.Color.Blue;

           table.Controls.Add(panel1, 0, 0);
           table.Controls.Add(panel2, 0, 1);

           this.Controls.Add(table);
           this.Size = new System.Drawing.Size(150, 500);
           
           SetValue("50");
       }
       public float GetValue()
       {
           return this.percentValue;
       }
       public void IncreaseValue()
       {
           IncrementValue(percentValue - 1);
       }
       public void DecreaseValue()
       {
           IncrementValue(percentValue + 1);
       }
       private void IncrementValue(float value)
       {
           SetValue(value.ToString());
       }
       public void SetValue(string value)
       {
           float.TryParse(value, out percentValue);
           if ((percentValue >= 0) && (percentValue <=100))
           {
               table.RowStyles.RemoveAt(1);
               table.RowStyles.RemoveAt(0);
               table.RowStyles.Add(new RowStyle(SizeType.Percent, 100 - percentValue));
               table.RowStyles.Add(new RowStyle(SizeType.Percent, percentValue));
           }
       }
   }
vngu2lb8

vngu2lb83#

如果这是一个全新的应用程序,请使用WPF。垂直进度条是内置的

<ProgressBar Orientation="Vertical" />
b1uwtaje

b1uwtaje4#

我发现这篇文章,而寻找一种方法,使一个垂直的进度条,并使用代码在 * 问题 * 作为 * 答案 * 我自己的问题。* 接受 * 是正确的,所以肯定和✔。
我希望这只是一点附加值。在我自己的项目中,我也需要定制颜色。我最终使用OP的 question 来创建我的类,并修改它,以便可以设置ForeColor来更改指示器颜色。所有这些和一个要 Boot 的工作示例。

class VerticalProgressBar : ProgressBar
{
    public VerticalProgressBar() => SetWindowTheme(Handle, string.Empty, string.Empty);
    protected override CreateParams CreateParams
    {
        get
        { 
            CreateParams cp = base.CreateParams;
            cp.Style |= 0x04;
            return cp;
        }
    }
    [DllImport("uxtheme", ExactSpelling = true, CharSet = CharSet.Unicode)]
    public extern static Int32 SetWindowTheme(IntPtr hWnd,
                  String textSubAppName, String textSubIdList);
}

设计者代码

  • “代码的放置位置”(如已接受答案中所述)。*
private void InitializeComponent()
{
    this.progressBarTemperature = new weather_client.VerticalProgressBar();
    this.SuspendLayout();
    .
    .
    .
    // 
    // progressBarTemperature
    // 
    this.progressBarTemperature.Location = new System.Drawing.Point(35, 12);
    this.progressBarTemperature.Name = "progressBarTemperature";
    this.progressBarTemperature.Size = new System.Drawing.Size(20, 113);
    this.progressBarTemperature.TabIndex = 2;
    // 
    // MainForm
    // 
    this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 25F);
    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
    this.ClientSize = new System.Drawing.Size(385, 144);
    this.Controls.Add(this.progressBarTemperature);
    .
    .
    .
    this.Name = "MainForm";
    this.Text = "Main Form";
    this.ResumeLayout(false);
}
private VerticalProgressBar progressBarTemperature;

相关问题