首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >奇怪的GraphicsPath.AddArc()行为

奇怪的GraphicsPath.AddArc()行为
EN

Stack Overflow用户
提问于 2020-06-28 06:53:05
回答 1查看 61关注 0票数 2

我用GraphicPaths进行了实验,发现了一个有趣的事情: DrawArc()具有相同的椭圆宽度和高度,但不同的起始角度(0,90,180,270)是不同的代码:

代码语言:javascript
复制
            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);

期望值:

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

如何解决这个问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-02-14 00:59:21

为了创建一个圆形矩形区域,我最终使用了CreateRoundRectRgn

代码语言:javascript
复制
[System.Runtime.InteropServices.DllImport("gdi32.dll")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect,
    int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);

代码语言:javascript
复制
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();
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62616383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档