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

时间:2010-07-05 19:38:00

标签: c#-3.0 erase rubber-band

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

问题

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

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

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

表格:

alt text http://i46.tinypic.com/9stlcm.jpg


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;
    }





}
}

感谢您阅读

2 个答案:

答案 0 :(得分:1)

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

您需要覆盖表单的OnPaint方法才能不断绘制矩形。现在,你在鼠标移动时绘制它,但之后也需要绘制它。

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

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

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

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

答案 1 :(得分:1)

我正在寻找类似的东西,但我发现的解决方案感到惊讶! 你的坐标正确吗? 你可以使用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()

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