2013-06-07 10 views
0

WinForms 응용 프로그램에서 일부 글리프 그리기. 각 글리프는 그래픽 경로로 정의되며 기본적으로 둥근 모서리가있는 사각형입니다.두 가지 색으로 그래픽 경로 채우기

이제 그래픽 경로를 단일 색상으로 채 웁니다. 그러나 두 가지 색상으로 채워야합니다. 다음의 예는 내가 필요에 대해 설명 :

enter image description here

내가 응용 프로그램의 성능이 영향을받을 수 있기 때문에 새로운 GraphicsPath을 만들지 않도록하고 싶습니다.

새 그래픽 경로를 만들지 않고 두 번째 채우기 색을 그리는 데 까다로운 옵션이 있습니까? 여기

내 그래픽 경로의 코드입니다 : "새로운 그래픽 경로를 생성하지 않고 두 번째 채우기 색상을 그릴 수있는 까다로운 옵션이 있는가"

public class RoundedRectangle 
{ 
    public static GraphicsPath Create(int x, int y, int width, int height) 
    { 
     int radius = height/2; 
     int xw = x + width; 
     int yh = y + height; 
     int xwr = xw - radius; 
     int xr = x + radius; 
     int r2 = radius * 2; 
     int xwr2 = xw - r2; 

     GraphicsPath p = new GraphicsPath(); 

     p.StartFigure(); 

     // Right arc 
     p.AddArc(xwr2, y, r2, r2, 270, 180); 

     //Bottom Edge 
     p.AddLine(xwr, yh, xr, yh); 

     // Left arc 
     p.AddArc(x, y, r2, r2, 90, 180); 

     //closing the figure adds the top Edge automatically 
     p.CloseFigure(); 

     return p; 
    } 
} 

답변

3

중간 영역을 직사각형이 아닌 패션으로 나눠서 표시하려면 GraphicsPath가 필요합니다.

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     this.Paint += new PaintEventHandler(Form1_Paint); 
    } 

    GraphicsPath rect = RoundedRectangle.Create(100, 100, 100, 35); 

    void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     TwoColorFill(e.Graphics, rect, Color.Yellow, Color.Blue, Color.Gray, 5); 
    } 

    private void TwoColorFill(Graphics G, GraphicsPath roundRect, Color FillColorLeft, Color FillColorRight, Color BorderColor, float BorderThickness) 
    { 
     using (SolidBrush RightFill = new SolidBrush(FillColorRight)) 
     { 
      G.FillPath(RightFill, roundRect); 
     } 

     using (SolidBrush LeftFill = new SolidBrush(FillColorLeft)) 
     { 
      GraphicsPath gp = new GraphicsPath(); 
      gp.AddPolygon(new Point[] { 
       new Point((int)roundRect.GetBounds().Left, (int)roundRect.GetBounds().Top), 
       new Point((int)roundRect.GetBounds().Right, (int)roundRect.GetBounds().Top), 
       new Point((int)roundRect.GetBounds().Left, (int)roundRect.GetBounds().Bottom) 
      }); 
      G.SetClip(gp); 
      G.FillPath(LeftFill, rect); 
      G.ResetClip(); 
     } 

     using (Pen p = new Pen(BorderColor, BorderThickness)) 
     { 
      G.DrawPath(p, roundRect); 
     } 
    } 

} 

* 더 생각 후에는 기술적으로 가능 채워진 사각형 그리기 다음, 그 중심에 번역의 GraphicsPath 자체를 사용하여 클리핑 회전을 수행하고,에 의해 수 있습니다 : 여기

내가 생각 해낸거야 x 축을 따라 가장자리가 있습니다. 어쨌든 정확한 각도를 계산해야 할 것입니다. 위의 추가 GraphicsPath를 만드는 것보다 성능면에서 현명한 선택이 될 것이라고 확신 할 수 없습니다.