-1

和标题说的差不多。没有什么可以补充的了!

我正在使用 Windows 窗体应用程序。

4

1 回答 1

1

您几乎可以通过继承控件、几个属性来编写一个,然后使用控件的 Paint 事件绘制图像。

我写这个例子作为一个基本的控制。它归因于默认值等,但我用 VB 编写并翻译为 C#,因此不能保证第一次尝试一切正常。

您仍然需要实施各种错误检查等。该控件会拉伸您为其定义的任何图像。如果没有定义图像,它将恢复为默认颜色。

更新:添加了属性以允许显示图像。

用它做任何事情;根据需要修改(下面有兴趣的原始VB源):

using System.ComponentModel;

[CLSCompliant(true)]
public class Progressbar : Control {

    [CLSCompliant(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue(100)]
    public int Maximum {
        get {
            return _maximum;
        }
        set {
            _maximum = value;
            this.Invalidate();
        }
    }

    [CLSCompliant(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue(0)]
    public int Value {
        get {
            return _value;
        }
        set {
            _value = value;
            this.Invalidate();
        }
    }

    [CLSCompliant(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue(null.ToString())]
    public Image ProgressbarImage {
        get {
            return _progressbarImage;
        }
        set {
            _progressbarImage = value;
            this.Invalidate();
        }
    }

    [CLSCompliant(true)]
    [RefreshProperties(RefreshProperties.Repaint)]
    [Browsable(true)]
    [Category("Appearance")]
    [DefaultValue(typeof(Color), "DarkGray")]
    public Color ProgressbarColor {
        get {
            return _progressbarColor;
        }
        set {
            _progressbarColor = value;
            this.Invalidate();
        }
    }

    [CLSCompliant(true)]
    [Browsable(true)]
    [Category("Behavior")]
    [DefaultValue(true)]
    public bool RevealImage {
        get {
            return _revealImage;
        }
        set {
            _revealImage = value;
            this.Invalidate();
        }
    }

    private bool _revealImage = true;
    private int _maximum = 100;
    private int _value = 0;
    private Image _progressbarImage = null;
    private Color _progressbarColor = Color.DarkGray;

    Progressbar() {
        InitializeComponent();
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) {

        Graphics g = e.Graphics;
        Rectangle r = this.ClientRectangle;

        ControlPaint.DrawBorder(g, r, Color.Black, ButtonBorderStyle.Solid);

        r.Inflate(-1, -1);
        r.Width = (int.Parse((r.Width 
                        * (_value / _maximum))) - 1);
        if ((r.Width < 1)) {
            return;
        }

        if (_progressbarImage == null) {
            Using (Solidbrush b = new SolidBrush(_progressbarColor)) {
                g.FillRectangle(b, r);
            }
        }
        else {
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic;
            if (_revealImage) {
                g.DrawImage(_progressbarImage, r, r, GraphicsUnit.Pixel);
            }
            else {
                g.DrawImage(_progressbarImage, r);
            }
        }
    }
}

原VB源码:

Imports System.ComponentModel

<CLSCompliant(True)>
Public Class Progressbar

    <CLSCompliant(True),
    RefreshProperties(RefreshProperties.Repaint),
    Browsable(True),
    Category("Appearance"),
    DefaultValue(100)>
    Public Property Maximum As Integer
        Get
            Return _maximum
        End Get
        Set(value As Integer)
            _maximum = value
            Me.Invalidate()
        End Set
    End Property
    <CLSCompliant(True),
    RefreshProperties(RefreshProperties.Repaint),
    Browsable(True),
    Category("Appearance"),
    DefaultValue(0)>
    Public Property Value As Integer
        Get
            Return _value
        End Get
        Set(value As Integer)
            _value = value
            Me.Invalidate()
        End Set
    End Property
    <CLSCompliant(True),
    RefreshProperties(RefreshProperties.Repaint),
    Browsable(True),
    Category("Appearance"),
    DefaultValue(CStr(Nothing))>
    Public Property ProgressbarImage As Image
        Get
            Return _progressbarImage
        End Get
        Set(value As Image)
            _progressbarImage = value
            Me.Invalidate()
        End Set
    End Property
    <CLSCompliant(True),
    RefreshProperties(RefreshProperties.Repaint),
    Browsable(True),
    Category("Appearance"),
    DefaultValue(GetType(Color), "DarkGray")>
    Public Property ProgressbarColor As Color
        Get
            Return _progressbarColor
        End Get
        Set(value As Color)
            _progressbarColor = value
            Me.Invalidate()
        End Set
    End Property
    <CLSCompliant(True),
    Browsable(True),
    Category("Behavior"),
    DefaultValue(True)>
    Public Property RevealImage As Boolean
        Get
            Return _revealImage
        End Get
        Set(value As Boolean)
            _revealImage = value
            Me.Invalidate()
        End Set
    End Property


    Private _maximum As Integer = 100
    Private _value As Integer = 0
    Private _progressbarImage As Image = Nothing
    Private _progressbarColor As Color = Color.DarkGray
    Private _revealImage As Boolean = True

    Sub New()

        InitializeComponent()

        SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

    End Sub
    Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)

        Dim g As Graphics = e.Graphics
        Dim r As Rectangle = Me.ClientRectangle

        ControlPaint.DrawBorder(g, r, Color.Black, ButtonBorderStyle.Solid)

        r.Inflate(-1, -1)
        r.Width = CInt(r.Width * _value / _maximum) - 1
        If r.Width < 1 Then Return

        If _progressbarImage Is Nothing Then
            Using b As New SolidBrush(_progressbarColor)
                g.FillRectangle(b, r)
            End Using
        Else
            g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
            If _revealImage Then
                g.DrawImage(_progressbarImage, r, r, GraphicsUnit.Pixel)
            Else
                g.DrawImage(_progressbarImage, r)
            End If
        End If

    End Sub

End Class
于 2012-12-20T02:06:18.603 回答