0

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

问题

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

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

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

形式 :

替代文字


代码

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





}
}

谢谢阅读

4

2 回答 2

1

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

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

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

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

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

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

于 2010-07-05T19:50:19.493 回答
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),无需绘画,没有麻烦!

于 2010-12-28T16:25:18.790 回答