2012-12-20 52 views
-1

該標題的含義非常多。沒有其他我可以補充!如何更改進度條以使用圖像

我正在使用Windows窗體應用程序。

+0

你可以說無論你在談論的WinForms,WPF,Silverlight或別的東西完全,因爲這不是一個真正的C#問題,而是關於您使用的任何UI框架的問題。 – Carson63000

+0

添加了我使用的內容。 Windows窗體應用程序。對不起 – Dibesjr

+0

我查了幾種方法來改變進度欄的顏色,使其平滑,但沒有人似乎與圖像工作。我有一張我想用作進度的圖像。 – Dibesjr

回答

1

你可以簡單地通過繼承Control,一些屬性來編寫一個屬性,然後使用控件的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 
+0

工程就像一個魅力!唯一的問題是,有一個令人討厭的邊界,我無法擺脫!示例http://screencast.com/t/bpS0O35rQJ – Dibesjr

+0

我擺脫了它!萬分感謝! – Dibesjr