2012-12-20 5 views
-1

제목이 말하는대로. 별로 추가 할 수 없어요!이미지를 사용하려면 진행률 표시 줄을 변경하는 방법

Windows Form 응용 프로그램을 사용하고 있습니다.

+0

당신은 완전히 다른 윈폼에 대해, WPF, Silverlight를, 이야기라도하고 있는지 말할 수있다, 이것은 실제로 C# 질문이 아니기 때문에 사용중인 UI 프레임 워크에 관한 질문입니다. – Carson63000

+0

내가 사용하고있는 것을 추가했습니다. Windows 양식 응용 프로그램. 죄송합니다. – Dibesjr

+0

진행률 표시 줄의 색을 변경하고 부드럽게 보이게하는 방법을 여러 가지를 찾았지만 아무 것도 이미지를 사용하지 않는 것처럼 보였습니다. 진행 상황으로 사용하고 싶은 이미지가 있습니다. – Dibesjr

답변

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 
+0

매력처럼 작동합니다! 단 한 가지는 내가 제거 할 수없는 성가신 경계가 있다는 것입니다! 예 http://screencast.com/t/bpS0O35rQJ – Dibesjr

+0

나는 그것을 없앴습니다! 엄청 고마워! – Dibesjr

관련 문제