奇怪的 GraphicsPath.AddArc() 行为
Strange GraphicsPath.AddArc() behaviour
我尝试了 GraphicPaths 并发现了一件有趣的事情:DrawArc() 具有相同的椭圆宽度和高度,但不同的起始角度 (0,90,180,270) 是不同的
代码:
GraphicsPath pth = new GraphicsPath();
pth.AddArc(10, 10, 16, 16, 180, 90);
pth.AddArc( 40, 10, 16, 16, 270, 90);
pth.AddArc( 40, 40, 16, 16, 0, 90);
pth.AddArc( 10, 40, 16, 16, 90, 90);
e.Graphics.FillPath(new SolidBrush(Color.FromArgb(100, 120, 200)), pth);
预计:
但是涂了(只有左上弧是正确的):
如何解决?
为了创建一个圆角矩形区域,我最终使用了 CreateRoundRectRgn:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
public partial class RoundCornerControl : Control
{
private int radius = 20;
[DefaultValue(20)]
public int Radius
{
get { return radius; }
set { radius = value; this.RecreateRegion(); }
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
private void RecreateRegion()
{
var bounds = ClientRectangle;
this.Region = Region.FromHrgn(CreateRoundRectRgn(bounds.Left, bounds.Top,
bounds.Right, bounds.Bottom, Radius, radius));
this.Invalidate();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.RecreateRegion();
}
}
我尝试了 GraphicPaths 并发现了一件有趣的事情:DrawArc() 具有相同的椭圆宽度和高度,但不同的起始角度 (0,90,180,270) 是不同的 代码:
GraphicsPath pth = new GraphicsPath();
pth.AddArc(10, 10, 16, 16, 180, 90);
pth.AddArc( 40, 10, 16, 16, 270, 90);
pth.AddArc( 40, 40, 16, 16, 0, 90);
pth.AddArc( 10, 40, 16, 16, 90, 90);
e.Graphics.FillPath(new SolidBrush(Color.FromArgb(100, 120, 200)), pth);
预计:
但是涂了(只有左上弧是正确的):
如何解决?
为了创建一个圆角矩形区域,我最终使用了 CreateRoundRectRgn:
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
public partial class RoundCornerControl : Control
{
private int radius = 20;
[DefaultValue(20)]
public int Radius
{
get { return radius; }
set { radius = value; this.RecreateRegion(); }
}
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
private void RecreateRegion()
{
var bounds = ClientRectangle;
this.Region = Region.FromHrgn(CreateRoundRectRgn(bounds.Left, bounds.Top,
bounds.Right, bounds.Bottom, Radius, radius));
this.Invalidate();
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
this.RecreateRegion();
}
}