c#WinForms-以编程方式禁用Application.EnableVisualStyles()方法

本文关键字:EnableVisualStyles 方法 Application 编程 方式禁 c#WinForms- | 更新日期: 2024-09-19 05:44:19

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

c#WinForms-以编程方式禁用Application.EnableVisualStyles()方法

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

   using System.Windows.Forms.VisualStyles;
    Application.VisualStyleState = VisualStyleState.NonClientAreaEnabled;

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

创建自己的进度条类。禁用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);
}
}
相关文章:
  • 没有找到相关文章