如何在 C# 中找到橡皮筋矩形的四个坐标并擦除绘图

发布于 2024-09-07 22:27:56 字数 2692 浏览 3 评论 0原文

您好,我尝试在 C# 中使用鼠标在表单上绘制橡皮筋矩形。

问题

1)鼠标释放后矩形消失。 [我希望它保留在表单上]

2)我还需要找到所绘制矩形的四个点的坐标

3)我还需要在必要时擦除矩形以绘制新矩形

表单:

alt text


代码

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;

namespace rubberbandrectangle
{
public partial class Form1 : Form
{
    Boolean bHaveMouse;
    Point ptOriginal = new Point();
    Point ptLast = new Point();


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        bHaveMouse = true;
        ptOriginal.X = e.X;
        ptOriginal.Y = e.Y;
        ptLast.X = -1;
        ptLast.Y = -1;
    }

    private void MyDrawReversibleRectangle(Point p1, Point p2)
    {
        Rectangle rc = new Rectangle();

        p1 = PointToScreen(p1);
        p2 = PointToScreen(p2);
        if (p1.X < p2.X)
        {
            rc.X = p1.X;
            rc.Width = p2.X - p1.X;
        }
        else
        {
            rc.X = p2.X;
            rc.Width = p1.X - p2.X;
        }
        if (p1.Y < p2.Y)
        {
            rc.Y = p1.Y;
            rc.Height = p2.Y - p1.Y;
        }
        else
        {
            rc.Y = p2.Y;
            rc.Height = p1.Y - p2.Y;
        }
        ControlPaint.DrawReversibleFrame(rc,
                        Color.Red, FrameStyle.Dashed);
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        bHaveMouse = false;
        if (ptLast.X != -1)
        {
            Point ptCurrent = new Point(e.X, e.Y);
            MyDrawReversibleRectangle(ptOriginal, ptLast);
        }
        ptLast.X = -1;
        ptLast.Y = -1;
        ptOriginal.X = -1;
        ptOriginal.Y = -1;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Point ptCurrent = new Point(e.X, e.Y);
        if (bHaveMouse)
        {
            if (ptLast.X != -1)
            {
                MyDrawReversibleRectangle(ptOriginal, ptLast);
            }
            ptLast = ptCurrent;
                MyDrawReversibleRectangle(ptOriginal, ptCurrent);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MouseDown += new MouseEventHandler(Form1_MouseDown);
        MouseUp += new MouseEventHandler(Form1_MouseUp);
        MouseMove += new MouseEventHandler(Form1_MouseMove);
        bHaveMouse = false;
    }





}
}

感谢您的阅读

Hi I have tried to draw an rubberband rectangle on a form using the mouse in C#.

Problems

1) After the mouse release the rectangle disappears. [I want it to stay on the form]

2) I also need to find the coordinates of the four points of the drawn rectangle

3) I also need to erase the rectangle to draw a new one when necessary

Form :

alt text


CODE

 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;
 using System.Text;
 using System.Windows.Forms;

namespace rubberbandrectangle
{
public partial class Form1 : Form
{
    Boolean bHaveMouse;
    Point ptOriginal = new Point();
    Point ptLast = new Point();


    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        bHaveMouse = true;
        ptOriginal.X = e.X;
        ptOriginal.Y = e.Y;
        ptLast.X = -1;
        ptLast.Y = -1;
    }

    private void MyDrawReversibleRectangle(Point p1, Point p2)
    {
        Rectangle rc = new Rectangle();

        p1 = PointToScreen(p1);
        p2 = PointToScreen(p2);
        if (p1.X < p2.X)
        {
            rc.X = p1.X;
            rc.Width = p2.X - p1.X;
        }
        else
        {
            rc.X = p2.X;
            rc.Width = p1.X - p2.X;
        }
        if (p1.Y < p2.Y)
        {
            rc.Y = p1.Y;
            rc.Height = p2.Y - p1.Y;
        }
        else
        {
            rc.Y = p2.Y;
            rc.Height = p1.Y - p2.Y;
        }
        ControlPaint.DrawReversibleFrame(rc,
                        Color.Red, FrameStyle.Dashed);
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        bHaveMouse = false;
        if (ptLast.X != -1)
        {
            Point ptCurrent = new Point(e.X, e.Y);
            MyDrawReversibleRectangle(ptOriginal, ptLast);
        }
        ptLast.X = -1;
        ptLast.Y = -1;
        ptOriginal.X = -1;
        ptOriginal.Y = -1;
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Point ptCurrent = new Point(e.X, e.Y);
        if (bHaveMouse)
        {
            if (ptLast.X != -1)
            {
                MyDrawReversibleRectangle(ptOriginal, ptLast);
            }
            ptLast = ptCurrent;
                MyDrawReversibleRectangle(ptOriginal, ptCurrent);
        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        MouseDown += new MouseEventHandler(Form1_MouseDown);
        MouseUp += new MouseEventHandler(Form1_MouseUp);
        MouseMove += new MouseEventHandler(Form1_MouseMove);
        bHaveMouse = false;
    }





}
}

Thanks for reading

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

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

发布评论

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

评论(2

旧伤慢歌 2024-09-14 22:27:56

1) 鼠标松开后,矩形消失。 [我希望它保留在表单上]

您需要覆盖表单的 OnPaint< /a> 方法以连续绘制矩形。现在,您可以在鼠标移动时绘制它,但也需要在鼠标移动后绘制它。

2)我还需要找到绘制矩形的四个点的坐标

这些应该在你的 ptOriginal 和 ptLast 变量中 - 你还需要什么?

3)必要时我还需要擦除矩形以绘制新的矩形

只需停止绘制矩形,然后在OnPaint中绘制新的矩形即可。

1) After the mouse release the rectangle disappears. [I want it to stay on the form]

You need to override the form's OnPaint method in order to continually draw your rectangle. Right now, you draw it when the mouse moves, but it needs to be drawn afterwards, as well.

2) I also need to find the coordinates of the four points of the drawn rectangle

These should be in your ptOriginal and ptLast variables - What more do you need?

3) I also need to erase the rectangle to draw a new one when necessary

Just stop drawing the rectangle, and draw the new one in the OnPaint.

我一直在寻找类似的东西,但对我找到的解决方案感到惊讶!
你有坐标吗?
您可以只使用 VisualBasic PowerPacks,它包含在我的 Visual Studio 2008 版本中

这是一个示例代码,它将在 TextBox 上绘制一个矩形,即我给它一个自定义边框
[代码]

Dim x = TextBox1.Location.X
Dim y = TextBox1.Location.Y
Dim width = TextBox1.Width
Dim height = TextBox1.Height
Dim ShapeContainer1 As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
Me.Controls.Add(ShapeContainer1)
Dim RectangleShape1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {RectangleShape1})
RectangleShape1.Location = New System.Drawing.Point(x - 1, y - 1)
RectangleShape1.Size = New System.Drawing.Size(width + 1, height + 1)
RectangleShape1.BorderColor = Color.MistyRose
ShapeContainer1.Refresh()

代码是自我描述的,但如果您有任何问题,请留言...
是的,如果你想删除矩形,只需处理控件(整个矩形或 ShapeContainer),无需绘画,没有麻烦!

I was looking for a similar thing but was amazed by the solution I found!
You have the coordinates right?
You can just use VisualBasic PowerPacks, it is included with my version of Visual Studio 2008

Here's a sample code that will draw a rectangle over a TextBox, i.e. I am giving it a custom border
[code]

Dim x = TextBox1.Location.X
Dim y = TextBox1.Location.Y
Dim width = TextBox1.Width
Dim height = TextBox1.Height
Dim ShapeContainer1 As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
Me.Controls.Add(ShapeContainer1)
Dim RectangleShape1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {RectangleShape1})
RectangleShape1.Location = New System.Drawing.Point(x - 1, y - 1)
RectangleShape1.Size = New System.Drawing.Size(width + 1, height + 1)
RectangleShape1.BorderColor = Color.MistyRose
ShapeContainer1.Refresh()

Code is self describing but if you'd have any problem, just leave a message...
Yes, if you want to remove the rectangle, just dispose the controls(either the Rectangle or the ShapeContainer in whole), no painting, no hassle!

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