2017-12-30 63 views
3

ラベルの楕円のような領域を描いたが、そのためのアンチエイリアスの設定方法はわからない。ラベルのためのアンチエイリアス楕円領域を描く

スニペット:

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

と、これが結果です:

enter image description here

は、誰も私を助けることができますか?

+1

リージョンは、アンチエイリアスをサポートしていません。ラベルは親のPaintイベントにコードを追加することを避けるために非常に高価な方法であることを覚えておいてください。 –

答えて

1

GraphicsオブジェクトのSmoothingModeを設定します。 Regionを変更する代わりにOnPaintBackgroundを上書きしてください。リージョンはアンチエイリアスをサポートしていません。この例では、カスタマイズしたラベルをLabelから派生させて作成します。

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ピクセル右と下にクリップされます。したがって、私はそのサイズを1ピクセル小さくします。

コードまたはプロパティウィンドウでラベルのBackColorプロパティを設定して、楕円の背景色を設定します。

結果:あなたは、コードをコンパイルしたら

enter image description here

、カスタマイズされたラベルが自動的に現在のプロジェクトにToolboxに表示されます。

+0

しかし、彼は地域について尋ねる – TaW

関連する問題