2013-11-21 3 views
1

단색 배경색, 그라디언트 배경색 또는 그림을 표시 할 수 있도록 설계된 사용자 지정 패널이 있습니다.패널에서 파생 된 사용자 정의 컨트롤로 OnPaint 루프에 고정 된 경우

내가 지금처럼 BackgroundImage 속성과 OnPaint 메서드를 모두 무시했습니다

protected override void OnPaint(PaintEventArgs pe){ 
    Rectangle rc = new Rectangle(0, 0, this.Width, this.Height); 
    if (this.DrawStyle != DrawStyle.Picture){ 
     base.BackgroundImage = null; 

      if (this.DrawStyle == DrawStyle.Solid){ 
       using (SolidBrush brush = new SolidBrush(this.PrimaryColor)) 
        pe.Graphics.FillRectangle(brush, rc); 
      } 
      else if (this.DrawStyle == DrawStyle.Gradient){ 
       using (LinearGradientBrush brush = 
         new LinearGradientBrush(
          rc, this.PrimaryColor, this.SecondaryColor, this.Angle)) 
       pe.Graphics.FillRectangle(brush, rc); 
      } 
     } 
    else if (this._BGImage != null) 
     base.BackgroundImage = this._BGImage; 
} 

public override Image BackgroundImage{ 
    get { return this._BGImage; } 
    set { this._BGImage = value; } 
} 

존재가 컨트롤이시의 배경 형식 (단색, 그라데이션 또는 그림)을 변경할 수있을만큼 유연해야한다는 것입니다 이유 실행 시간이므로 컨트롤이 설정된 배경 이미지를 유지하고 필요할 때 표시합니다.

그러나 문제가 있습니다.

배경 이미지를 설정하지 않으면 문제가 없습니다. OnPaint는 약 5 번 호출 된 다음 잘됩니다.

그러나 배경 이미지를 설정하면 OnPaint를 계속해서 반복해서 호출하는 것이 좋지 않은 것처럼 보입니다.

이것은 분명히 배경 이미지를 덮어 쓰는 것과 관련이 있습니다. 끊어지기 때문에 문제가됩니다. 패널 모양을 바꿀 때까지 패널의 아무 것도 업데이트되지 않습니다.

내가 가지고있는 질문은 배경 이미지를 설정할 때 왜이 OnPaint 루프에서 멈추는 것일까 요? 이 밖에

+0

최신 업데이트가 실제 질문을 삭제했기 때문에 나는 원래 질문으로 포스트를 굴렸다. – LarsTech

답변

2

코멘트 :

// base.BackgroundImage = this._BGImage; 

는 자신을 재귀 적으로 페인트의 원인. 페인트 루틴에서 속성을 설정해서는 안됩니다.

또한 BackgroundImage를 재정 의하여 아무것도 수행하지 않으며 속성을 재정의하는 경우 base.BackgroundImage 값을 할당하는 유일한 장소 여야합니다. 그것을 제거하는 것을 고려하십시오.

protected override void OnPaintBackground(PaintEventArgs e) { 
    if (this.DrawStyle == DrawStyle.Picture) { 
    base.OnPaintBackground(e); 
    } 
} 

protected override void OnPaint(PaintEventArgs e) { 
    Rectangle rc = this.ClientRectangle; 
    if (this.DrawStyle == DrawStyle.Solid) { 
    using (SolidBrush brush = new SolidBrush(this.PrimaryColor)) 
     e.Graphics.FillRectangle(brush, rc); 
    } else if (this.DrawStyle == DrawStyle.Gradient) { 
    using (LinearGradientBrush brush = 
      new LinearGradientBrush(
       rc, this.PrimaryColor, this.SecondaryColor, this.Angle)) 
     e.Graphics.FillRectangle(brush, rc); 
    } 
    base.OnPaint(e); 
} 

불필요한 깜박임을 피하기 위해 패널의 생성자에서 this.DoubleBuffered = true;this.ResizeRedraw = true;를 추가해야합니다 :

나는 이것에 코드를 재 작업.

+0

감사합니다. 해결 방법을 찾았습니다. 변경 사항을 반영하도록 수정되었습니다. – Will

+0

@ 당신은 페인트 호출에서이'base.BackgroundImage = null;'을하지 않을 것입니다. – LarsTech

+0

남자에게 기지를 지정하라고 해줄 수 있습니까? 그의 속성 설정자의 배경 이미지는? –