我用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);期望值:

但已绘制(只有左上弧线是正确的):

如何解决这个问题?
发布于 2021-02-14 00:59:21
为了创建一个圆形矩形区域,我最终使用了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();
}
}https://stackoverflow.com/questions/62616383
复制相似问题