2017-12-30 42 views
3

레이블에 대해 타원형 영역을 그렸지만 그에 대한 앤티 앨리어싱을 설정하는 방법을 모르겠습니다.레이블에 대한 반올림 타원 영역을 그립니다.

발췌문 :

enter image description here

아무도 도와 줄 수 :

Rectangle circle = new Rectangle(0, 0, labelVoto.Width,labelVoto.Height); 
var path = new GraphicsPath(); 
path.AddEllipse(circle); 
labelVoto.Region = new Region(path); 

은이는 결과인가?

+1

지역은 앤티 앨리어싱을 지원하지 않습니다. Label은 부모의 Paint 이벤트에 코드를 추가하는 것을 피할 수있는 매우 비싼 방법이라는 것을 명심하십시오. –

답변

1

Graphics 개체의 SmoothingMode을 설정합니다. Region을 변경하는 대신 OnPaintBackground을 재정의하십시오. 영역은 앤티 앨리어싱을 지원하지 않습니다. 이 예에서는 Label에서 파생시켜 사용자 지정된 레이블을 만듭니다. 당신이 ClientRectangle 동일한 페인트 사각형의 크기를 설정하면

public class EllipticLabel : Label 
{ 
    protected override void OnPaintBackground(PaintEventArgs e) 
    { 
     // This ensures that the corners of the label will have the same color as the 
     // container control or form. They would be black otherwise. 
     e.Graphics.Clear(Parent.BackColor); 

     // This does the trick 
     e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 

     var rect = ClientRectangle; 
     rect.Width--; 
     rect.Height--; 
     using (var brush = new SolidBrush(BackColor)) { 
      e.Graphics.FillEllipse(brush, rect); 
     } 
    } 
} 

. 타원은 오른쪽과 아래쪽으로 한 픽셀 씩 클리핑됩니다. 따라서 크기를 1 픽셀 줄입니다.

코드 또는 속성 창에서 레이블의 BackColor 속성을 설정하여 원하는 타원 배경색을 설정합니다.

결과 :

코드를 컴파일하면

enter image description here

, 사용자 정의 라벨은 자동으로 현재 프로젝트의 Toolbox에 나타납니다.

+0

하지만 그는 지역에 대해 묻습니다. – TaW

관련 문제