如何在C#中使用鼠标绘制矩形

发布于 2024-09-08 08:08:05 字数 162 浏览 0 评论 0原文

我想用 C# 在窗体上绘制一个矩形。我阅读并找到了这篇文章。有可用的示例或教程吗?这篇文章没有多大帮助。

I want to draw a rectangle on a form in C#. I read and found this article. Are there any samples or tutorials available ? The article was not very helpful.

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

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

发布评论

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

评论(2

我三岁 2024-09-15 08:08:05

您链接的文章似乎是 C++,这可能解释了为什么它对您没有多大帮助。

如果为 MouseDown 和 MouseUp 创建事件,则应该具有矩形所需的两个角点。从那里开始,就是在表格上绘图的问题。 System.Drawing.* 可能应该是您的第一站。下面链接了一些教程:

使用 C# 在 WinForms 中绘制图形

使用 Winforms (StackOverflow) 绘制矩形

使用 C# 进行图形编程

The article you linked appears to be C++, which may explain why it didn't help you much.

If you create events for MouseDown and MouseUp, you should have the two corner points you need for a rectangle. From there, it's a matter of drawing on the form. System.Drawing.* should probably be your first stop. There are a couple of tutorials linked below:

Drawing with Graphics in WinForms using C#

Draw a rectangle using Winforms (StackOverflow)

Graphics Programming using C#

放血 2024-09-15 08:08:05

您需要这 3 个函数和变量:

    private Graphics g;
    Pen pen = new System.Drawing.Pen(Color.Blue, 2F);
    private Rectangle rectangle;
    private int posX, posY, width, height; 

其次,您需要创建一个鼠标按下事件:

    private void pictureCrop_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            posX = e.X;
            posY = e.Y;
        }
    }

第三,您需要实现鼠标向上事件:

    private void pictureCrop_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;

        if (e.X > posX && e.Y > posY) // top left to bottom right
        {
            width = Math.Abs(e.X - posX);
            height = Math.Abs(e.Y - posY);
        }
        else if (e.X < posX && e.Y < posY) // bottom right to top left
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posX = e.X;
            posY = e.Y;
        }
        else if (e.X < posX && e.Y > posY) // top right to bottom left
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posX = e.X;
        }
        else if (e.X > posX && e.Y < posY) // bottom left to top right
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posY = e.Y;
        }

        g.DrawImage(_bitmap, 0, 0);
        rectangle = new Rectangle(posX, posY, width, height);
        g = pictureCrop.CreateGraphics();
        g.DrawRectangle(pen, rectangle);
    }

并确保当您调整大小或移动表单时,矩形将在那里:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

        Graphics graph = e.Graphics;
        graph.DrawImage(_bitmap, 0, 0);
        Rectangle rec = new Rectangle(posX, posY, width, height);
        graph.DrawRectangle(pen, rec);
    }

You need this 3 functions and variables:

    private Graphics g;
    Pen pen = new System.Drawing.Pen(Color.Blue, 2F);
    private Rectangle rectangle;
    private int posX, posY, width, height; 

Second you need to make a mouse down event:

    private void pictureCrop_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            posX = e.X;
            posY = e.Y;
        }
    }

Third, you need to implemente the mouse up event:

    private void pictureCrop_MouseUp(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left)
            return;

        if (e.X > posX && e.Y > posY) // top left to bottom right
        {
            width = Math.Abs(e.X - posX);
            height = Math.Abs(e.Y - posY);
        }
        else if (e.X < posX && e.Y < posY) // bottom right to top left
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posX = e.X;
            posY = e.Y;
        }
        else if (e.X < posX && e.Y > posY) // top right to bottom left
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posX = e.X;
        }
        else if (e.X > posX && e.Y < posY) // bottom left to top right
        {
            width = Math.Abs(posX - e.X);
            height = Math.Abs(posY - e.Y);

            posY = e.Y;
        }

        g.DrawImage(_bitmap, 0, 0);
        rectangle = new Rectangle(posX, posY, width, height);
        g = pictureCrop.CreateGraphics();
        g.DrawRectangle(pen, rectangle);
    }

And to ensure that when you resize or move the form the rectangle will be there:

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);

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