【问题标题】:c# WinForms - Programmatically disable the method Application.EnableVisualStyles()?c# WinForms - 以编程方式禁用 Application.EnableVisualStyles() 方法?
【发布时间】:2016-04-15 20:42:39
【问题描述】:

是否可以通过编程方式禁用Application.EnableVisualStyles();?我想为我的应用程序的某个部分关闭视觉样式,在那里我可以有一个彩色进度条。我知道你可以使用System.Drawing 来绘制它,但如果我可以暂时关闭它,这会更简单。这是可能的还是我必须画出来?

【问题讨论】:

标签: c# winforms


【解决方案1】:

感谢 GreatJobBob 为我链接了 MSDN 页面,以下实现了我想要的。

   using System.Windows.Forms.VisualStyles;

    Application.VisualStyleState = VisualStyleState.NonClientAreaEnabled;

这让我可以在不改变其余控件和表单的情况下更改进度条的颜色。

【讨论】:

    【解决方案2】:

    创建自己的进度条类。禁用Application.EnableVisualStyles 将导致其他UI 出现问题,例如MessageBox。这是一个让您入门的基础课程,只需将前景色更改为您想要的颜色即可。

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    class MyProgressBar : Control 
    {
    public MyProgressBar() 
    {
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.SetStyle(ControlStyles.Selectable, false);
        Maximum = 100;
        this.ForeColor = Color.Red; //This is where you choose your color
        this.BackColor = Color.White;
    }
    public decimal Minimum { get; set; }
    public decimal Maximum { get; set; }
    
    private decimal mValue;
    public decimal Value 
    {
        get { return mValue; }
        set { mValue = value; Invalidate(); }
    }
    
    protected override void OnPaint(PaintEventArgs e) 
    {
        var rc = new RectangleF(0, 0, (float)(this.Width * (Value - Minimum) / Maximum), this.Height);
        using (var br = new SolidBrush(this.ForeColor)) 
        {
            e.Graphics.FillRectangle(br, rc);
        }
        base.OnPaint(e);
    }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-01
      • 2011-08-11
      • 2010-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2017-11-06
      相关资源
      最近更新 更多