C# 如何更改进度条以使用图像

C# 如何更改进度条以使用图像,c#,.net,winforms,progress-bar,element,C#,.net,Winforms,Progress Bar,Element,和标题差不多。我没什么可以补充的了 我使用的是Windows窗体应用程序。您只需通过继承控件、几个属性,然后使用控件的Paint事件绘制图像,就可以编写一个应用程序 我写这个例子作为一个基本控件。它带有默认值等属性,但我用VB编写并翻译成C#,因此不能保证在第一次尝试时一切都能正常工作 您仍然需要实现各种错误检查等等。控件将拉伸您为其定义的任何图像。如果未定义图像,它将恢复为默认颜色 更新:添加了允许显示图像的属性 用它做任何事;根据需要修改(以下为感兴趣的原始VB源代码): 使用System.

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


我使用的是Windows窗体应用程序。

您只需通过继承控件、几个属性,然后使用控件的Paint事件绘制图像,就可以编写一个应用程序

我写这个例子作为一个基本控件。它带有默认值等属性,但我用VB编写并翻译成C#,因此不能保证在第一次尝试时一切都能正常工作

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

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

用它做任何事;根据需要修改(以下为感兴趣的原始VB源代码):

使用System.ComponentModel;
[CLSCompliant(正确)]
公共类Progressbar:控件{
[CLSCompliant(正确)]
[刷新属性(刷新属性.重新绘制)]
[可浏览(正确)]
[类别(“外观”)]
[默认值(100)]
公共整数最大值{
得到{
返回最大值;
}
设置{
_最大值=最大值;
这个。使无效();
}
}
[CLSCompliant(正确)]
[刷新属性(刷新属性.重新绘制)]
[可浏览(正确)]
[类别(“外观”)]
[默认值(0)]
公共整数值{
得到{
返回_值;
}
设置{
_价值=价值;
这个。使无效();
}
}
[CLSCompliant(正确)]
[刷新属性(刷新属性.重新绘制)]
[可浏览(正确)]
[类别(“外观”)]
[默认值(null.ToString())]
公众形象{
得到{
返回_progressbarImage;
}
设置{
_progressbarImage=值;
这个。使无效();
}
}
[CLSCompliant(正确)]
[刷新属性(刷新属性.重新绘制)]
[可浏览(正确)]
[类别(“外观”)]
[默认值(颜色类型),“暗灰色”)]
公共颜色{
得到{
返回颜色;
}
设置{
_progressbarColor=值;
这个。使无效();
}
}
[CLSCompliant(正确)]
[可浏览(正确)]
[类别(“行为”)]
[默认值(真)]
公共图书馆{
得到{
返还(即返还);;
}
设置{
_收入=价值;
这个。使无效();
}
}
private bool_revalimage=true;
私有整数_最大值=100;
私有int_值=0;
私有映像_progressbarImage=null;
私有颜色_progressbarColor=Color.DarkGray;
Progressbar(){
初始化组件();
设置样式(ControlStyles.AllPaintingInWmPaint,true);
SetStyle(ControlStyles.OptimizedDoubleBuffer,true);
}
受保护的覆盖无效OnPaint(System.Windows.Forms.PaintEventArgs e){
图形g=e.图形;
矩形r=this.ClientRectangle;
ControlPaint.DrawBorder(g、r、颜色、黑色、纽扣顺序样式、纯色);
r、 充气(-1,-1);
r、 宽度=(int.Parse((r.Width
*(_值/_最大值))-1);
如果((r.宽度<1)){
返回;
}
如果(_progressbarImage==null){
使用(Solidbrush b=新的Solidbrush(_progressbarColor)){
g、 圆角矩形(b,r);
}
}
否则{
g、 插值模式=Drawing2D.InterpolationMode.HighQualityBicubic;
如果(_revalimage){
g、 DrawImage(_progressbarImage,r,r,GraphicsUnit.Pixel);
}
否则{
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
导入System.ComponentModel
公共级进度条
公共属性最大值为整数
得到
返回最大值
结束
设置(值为整数)
_最大值=最大值
使无效
端集
端属性
作为整数的公共属性值
得到
返回值
结束
设置(值为整数)
_价值=价值
使无效
端集
端属性
公共财产作为形象
得到
返回_progressbarImage
结束
设置(值为图像)
_progressbarImage=值
使无效
端集
端属性
公共财产将颜色作为颜色
得到
返回颜色
结束
设置(值为颜色)
_progressbarColor=值
使无效
端集
端属性
公共财产收入作为布尔值
得到
归还
结束
设置(值为布尔值)
_收入=价值
使无效
端集
端属性
私有_最大值为整数=100
私有_值为整数=0
Private _progressbarimageas Image=无
Private _progressbarcoloras Color=Color.DarkGray
Private _revalimage为布尔值=真
次新
初始化组件()
设置样式(ControlStyles.AllPaintingInWmPaint,真)
SetStyle(ControlStyles.OptimizedDoubleBuffer,True)
端接头
受保护的覆盖子OnPaint(如System.Windows.Forms.PaintEventArgs)
尺寸g为图形=e.图形
尺寸r为矩形=Me.ClientRectangle
ControlPaint.DrawBorder(g,r,颜色。黑色,按钮顺序样式。纯色)
r、 充气(-1,-1)
r、 宽度=CInt(r.宽度*_值/_最大值)-1
如果r.宽度<1,则返回
如果(u progressbarImage)什么都不是,
使用b作为新的SolidBrush(_progressbarColor)
g、 圆角矩形(b,r)
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