【问题标题】:WFA Progress bar Background color or texture (not foreground)WFA 进度条 背景颜色或纹理(不是前景)
【发布时间】:2015-06-14 10:50:32
【问题描述】:

我一直在寻找这个解决方案很长一段时间,在这里,或其他指导网站.. 但是很多人只想改变进度条的前景色(随着进度上升的部分)

我想,我的小图可以说明一切:

http://screenshot.cz/WT5KO/progbar.png

现在,我正在使用此代码绘制自定义栏,您可以在上面看到。是否可以将后台编辑代码组合到我的构造函数代码中?

 public class NewProgressBar : ProgressBar
{
    public NewProgressBar()
    {
        this.SetStyle(ControlStyles.UserPaint, true);

    }

    protected override void OnPaintBackground(PaintEventArgs pevent)
    {
        // None... Helps control the flicker.
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        const int inset = 0; // A single inset value to control teh sizing of the inner rect.

        using (Image offscreenImage = new Bitmap(this.Width, this.Height))
        {
            using (Graphics offscreen = Graphics.FromImage(offscreenImage))
            {


                Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);


                if (ProgressBarRenderer.IsSupported)
                    ProgressBarRenderer.DrawHorizontalBar(offscreen, rect);

                rect.Inflate(new Size(-inset, -inset)); // Deflate inner rect.
                rect.Width = (int)(rect.Width * ((double)this.Value / this.Maximum));
                if (rect.Width == 0) rect.Width = 1; // Can't draw rec with width of 0.
                TextureBrush mybrush = new TextureBrush(Properties.Resources.loading);
                LinearGradientBrush brush = new LinearGradientBrush(rect, Color.DarkGoldenrod, Color.Gold, LinearGradientMode.Vertical);
                offscreen.FillRectangle(mybrush, inset, inset, rect.Width, rect.Height);

                e.Graphics.DrawImage(offscreenImage, 0, 0);
                offscreenImage.Dispose();
            }
        }
    }
}

我在 .NET 4.5 下使用 WFA 模板

提前致谢

【问题讨论】:

  • 它是 WinForms 控件吗?
  • 我认为添加您正在使用的 GUI 工具包的确切名称是明智的,以吸引更多专家。通常,描述良好的标签会使问题变得更好。
  • 如果你想换背景,为什么不OnPaintBackground
  • 嗨,很抱歉缺少信息(现在添加)是的,它是 WFA 模板 OnpaintBackground - 如果我理解正确,它是“如果背景被绘制会发生什么”如您所见,进度条是部分的集合,如矩形(参见 sn-p)。我需要找到一段代码,它将重写实际绘制的定义。我一直在寻找 originall ProgressBar 类来编辑它,但我没有找到任何有用的东西:(

标签: c# .net winforms


【解决方案1】:

您在自定义进度条上使用可视进度条样式,因为您在课堂上将控件声明为进度条。

public class NewProgressBar : ProgressBar

如果您想自己制作,请将您的自定义控件类声明转换为 Control

public class NewProgressBar : Control

然后你就可以使用 BackColor 但您需要自己编写“值、最小值/最大值”以使进度条正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 2014-01-16
    • 2015-08-13
    • 2014-06-02
    相关资源
    最近更新 更多