2013-06-18 4 views
0

System.Windows.Forms.ProgressBar에서 파생 된 클래스를 만들었습니다. System.Windows.Forms.ProgressBar에서 파생 된 클래스가 해당 override를 가져 오지 않습니다. OnPaint

  • 프로젝트
  • 열기 대신을 System.Windows.Forms.UserControl에서 파생의 사용자 제어
  • 의 코드의 ProgressBar에서 파생에 UserControl을 추가 : 나는 MSDN에서 권장하는 방법을 따랐다.

는 또한 나는의 OnPaint를 무시, 그래서 내가 직접 그릴 수 :

protected override void OnPaint(PaintEventArgs e) 
{ 
    base.OnPaint(e); 
    // etc, do your own painting. 

아직이 함수가 호출되지 않습니다. 중단 점이 여기에서 중단되지 않습니다. 진행 표시 줄이 정상적으로 그려집니다. 내가 뭘 놓치고 있니?

+0

진행률 표시 줄은 기본 Windows 컨트롤이며 자체 그림을 그립니다. ProgressBar 클래스는 작은 .NET 래퍼 클래스이며 모든 기본 Windows 컨트롤처럼 UserPaint 스타일이 해제되어 있습니다. 왜 그렇게 생각하는지 설명하지 않는 고전적인 실수를 저지르고 있습니다. –

답변

2

다음 절차는 MSDN에서 제안하는 것과 동일합니다. 진행 표시 줄에는 OnPaint 함수가 있으므로 MSDN에 따라 OnPaint를 호출해야합니다.

호출되지 않는 이유는 컨트롤을 직접 그릴 것이라고 선언해야하기 때문입니다. 오래된 MFC에서는이 이름이 OwnerDrawn입니다. 시스템에 컨트롤을 직접 그려 넣으려는 경우 컨트롤의 스타일을 변경해야합니다. 이 작업은 Control.SetStyle을 사용하여 수행됩니다.

public partial class ColorProgressBar : System.Windows.Forms.ProgressBar 
{ 
    public ColorProgressBar() 
    { 
     InitializeComponent(); 
     this.SetStyle(ControlStyles.UserPaint, true); 
     // etc, other initializations 
    } 

이렇게하면 OnPaint가 호출됩니다.

예를 들어 완전한 ColorProgressBar. 이 클래스의 코드는 다른 곳에서 찾을 수 있지만 여기에서는 System.Windows.Forms.ProgressBar의 파생 클래스로 다시 작성됩니다. 이렇게하면 코드가 훨씬 작아집니다. 게다가 당신은 Progressbar의 모든 기능을 가지고 있습니다.

이 진행 표시 줄에는 막대의 일반 색상이 없지만 두 색상 사이의 그라디언트 색상이 있습니다. 다른 컨트롤과 마찬가지로 도구 상자를 사용하여 추가 할 수 있습니다. Progressbar의 속성을 변경하는 것처럼 속성을 변경할 수 있습니다. 속성 창 아래쪽에 추가 속성이 표시됩니다.

것은 그것을 만들려면 : 솔루션 Exploser에서

  • 프로젝트 만들기

    • 을 - 추가 - 사용자 정의 컨트롤을, 예를 들어, 이름을 지정 ColorProgressBar
    • 열기 편집기
    • 에서 ColorProgressBar의 코드
    • 기본 클래스를 UserControl에서 System.Windows.Forms.ProgressBar로 변경하십시오.
    • 클래스를 필요에 맞게 변경하십시오. 아래 예제를 참조하십시오.

    가장 중요한 기능은 OnPaint이며 ProgressBar의 모양이 변경됩니다.

    공공 부분 클래스 ColorProgressBar : 시스템 당신의 OnPaint가 호출되어 있는지 확인하기 위해 전술 한 바와 같이

    • 생성자에서 그라디언트 색상에게하는 SetStyle를
    • 를 설명하는 두 가지 속성을 추가 나머지는 매우 간단합니다 .Windows.Forms.ProgressBar { public Color BarColorOutside {get; 세트; } public Color BarColorCenter {get; 세트; 이 기본 작동하는지}

      public ColorProgressBar() 
      { 
          BarColorOutside = Color.Black; 
          BarColorCenter = Color.Yellow; 
          InitializeComponent(); 
          this.SetStyle(ControlStyles.UserPaint, true); 
      } 
      
      protected override void OnPaint(PaintEventArgs e) 
      { 
          base.OnPaint(e); 
          // your own painting will be added later 
      } 
      

    지금 확인 :

    • 빌드
    • 양식을 만듭니다. 진행률 막대를 양식에 추가하십시오.
    • 속성을 사용하여 진행 표시 줄에 값과 초기 색상을 지정하십시오.
    • onPaint가 호출되는지 확인하려면 디버그하십시오.

    이제는 onPaint. 채워진 진행 막대의 부분은 그라디언트 색상으로 채워집니다. 그렇게하려면 진행률 표시 줄의 채우기 너비와 높이를 알아야합니다. 우리는 두 개의 직사각형을 만들 수 있습니다 : 하나는 상반부를 채우고 다른 하나는 하반부를 채울 것입니다. 채우기는 그라디언트 브러시로 수행됩니다. 위쪽 절반은 barColorOutside에서 barColorCenter, 아래쪽은 barColorCenter에서 barColorOutside입니다. 이렇게하면 가운데 색상이 진행 막대의 가운데에 표시됩니다.

    protected override void OnPaint(PaintEventArgs e) 
    { 
        base.OnPaint(e); 
    
        // the part that has to be filled with a colored progress: 
        int fillWidth = (Width * Value)/(Maximum - Minimum); 
        if (fillWidth == 0) 
        { // nothing to fill 
         return; 
        } 
    
        // the two rectangles: 
        Rectangle topRect = new Rectangle(0, 0, fillWidth, Height/2); 
        Rectangle bottomRect = new Rectangle(0, Height/2, fillWidth, Height); 
    
        // Paint upper half: the gradient fills the complete topRect, 
        // from background color to foreground color 
        LinearGradientBrush brush = new LinearGradientBrush(topRect, BarColorOutside, 
         BarColorCenter, LinearGradientMode.Vertical); 
        e.Graphics.FillRectangle(brush, topRect); 
        brush.Dispose(); 
    
        // paint lower half: gradient fills the complete bottomRect, 
        // from foreground color to background color 
        brush = new LinearGradientBrush(bottomRect, BarColorCenter, BarColorOutside, 
         LinearGradientMode.Vertical); 
        e.Graphics.FillRectangle(brush, bottomRect); 
        brush.Dispose(); 
    
        // we have missed one line in the center: draw a line: 
        Pen pen = new Pen(BarColorCenter); 
        e.Graphics.DrawLine(pen, new Point(0, topRect.Bottom), 
         new Point(fillWidth, topRect.Bottom)); 
        pen.Dispose(); 
    
        // if style is blocks, draw vertical lines to simulate blocks 
        if (Style == ProgressBarStyle.Blocks) 
        { 
         int seperatorWidth = (int)(this.Height * 0.67); 
         int NrOfSeparators = (int)(fillWidth/seperatorWidth); 
         Color sepColor = ControlPaint.LightLight(BarColorCenter); 
    
         for (int i = 1; i <= NrOfSeparators; i++) 
         { 
          e.Graphics.DrawLine(new Pen(sepColor, 1), 
          seperatorWidth * i, 0, seperatorWidth * i, this.Height); 
         } 
        } 
    }