首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为GraphicsPath应用外部边框

如何为GraphicsPath应用外部边框
EN

Stack Overflow用户
提问于 2014-06-15 06:15:39
回答 1查看 1.4K关注 0票数 2

如何为GraphicsPath应用外部边框??我尝试了下面的代码,但它将边框应用于单个矩形,而不是整个路径。

我的预期输出截图在.下面。

我尝试过的可运行示例:

代码语言:javascript
复制
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(this.Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        GraphicsPath graphicsPath = new GraphicsPath();

        Rectangle rect1 = new Rectangle(20, 20, 100, 30);
        Rectangle rect2 = new Rectangle(20, 50, 40, 20);

        graphicsPath.StartFigure();
        graphicsPath.AddRectangle(rect1);
        graphicsPath.AddRectangle(rect2);

        graphicsPath.CloseAllFigures();
        e.Graphics.FillPath(Brushes.LightGreen, graphicsPath);
        e.Graphics.DrawPath(Pens.DarkGreen, graphicsPath);

    }
}

请建议我如何实现我的预期产出。提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-15 06:57:29

使用多边形而不是矩形绘制形状可以达到预期的效果:

代码语言:javascript
复制
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        this.Load += new System.EventHandler(this.Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        GraphicsPath graphicsPath = new GraphicsPath();
        Point[] pts = new Point[] { new Point(20, 20), 
                                    new Point(120, 20), 
                                    new Point(120, 50), 
                                    new Point(60, 50),        
                                    new Point(60, 70), 
                                    new Point(20, 70) };

        graphicsPath.AddPolygon(pts);

        e.Graphics.FillPath(Brushes.LightGreen, graphicsPath);
        e.Graphics.DrawPath(Pens.DarkGreen, graphicsPath);
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24227071

复制
相关文章

相似问题

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