C#中动态删除/添加(取消删除)表单区域

发布于 2024-08-27 18:23:04 字数 36 浏览 9 评论 0原文

如何使用区域和图形路径类动态删除/添加(取消删除)表单区域

How do i dynamically removing/adding(cancel the removal) form area using region and graphics path class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红墙和绿瓦 2024-09-03 18:23:04

要动态更改表单的形状,只需将表单的 Region 属性设置为从 GraphicsPath 创建的新 Region 对象即可。例如,带有单个按钮的表单可以更改其形状,如下所示:(工作示例)

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Sample
{
    public class ShapedForm : Form
    {
        Button testbutton;

        public ShapedForm()
        {
            // Create a button.
            testbutton = new Button();
            testbutton.Location = new Point(10, 10);
            testbutton.Size = new Size(50, 50);
            testbutton.Text = "Click me!";
            testbutton.Click += new EventHandler(this.testbutton_Click);
            this.Controls.Add(testbutton);

            // Remove the border around the form.
            this.FormBorderStyle = FormBorderStyle.None;

            // Set the initial shape of the form to an ellipse.
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, 200, 100);

            this.Region = new Region(path);
        }

        private void testbutton_Click(object sender, EventArgs e)
        {
            // Change the shape of the form to some other ellipse.
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, 100, 100);
            path.AddEllipse(120, 40, 50, 50);

            this.Region = new Region(path);
        }
    }
}

To change the shape of your Form dynamically, just set the Region property of the form to a new Region object created from a GraphicsPath. For example, a form with a single button on it could change it's shape like this: (working example)

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Sample
{
    public class ShapedForm : Form
    {
        Button testbutton;

        public ShapedForm()
        {
            // Create a button.
            testbutton = new Button();
            testbutton.Location = new Point(10, 10);
            testbutton.Size = new Size(50, 50);
            testbutton.Text = "Click me!";
            testbutton.Click += new EventHandler(this.testbutton_Click);
            this.Controls.Add(testbutton);

            // Remove the border around the form.
            this.FormBorderStyle = FormBorderStyle.None;

            // Set the initial shape of the form to an ellipse.
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, 200, 100);

            this.Region = new Region(path);
        }

        private void testbutton_Click(object sender, EventArgs e)
        {
            // Change the shape of the form to some other ellipse.
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, 100, 100);
            path.AddEllipse(120, 40, 50, 50);

            this.Region = new Region(path);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文