2

我对 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);

预期的:

预期的

但是画了(只有左上弧是正确的):

但是画了

如何解决?

4

1 回答 1

1

为了创建一个圆角矩形区域,我最终使用了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();
    }
}
于 2021-02-13T16:59:21.143 回答