C# 3.0 如何在C语言中找到四个坐标并删除橡皮筋矩形的绘图#
嗨,我试着用C#中的鼠标在窗体上画一个橡皮筋矩形 问题 1) 释放鼠标后,矩形消失。[我希望它留在表格上] 2) 我还需要找到绘制矩形的四个点的坐标 3) 我还需要删除矩形,以便在必要时绘制一个新矩形 表格:C# 3.0 如何在C语言中找到四个坐标并删除橡皮筋矩形的绘图#,c#-3.0,erase,rubber-band,C# 3.0,Erase,Rubber Band,嗨,我试着用C#中的鼠标在窗体上画一个橡皮筋矩形 问题 1) 释放鼠标后,矩形消失。[我希望它留在表格上] 2) 我还需要找到绘制矩形的四个点的坐标 3) 我还需要删除矩形,以便在必要时绘制一个新矩形 表格: 代码 使用系统; 使用System.Collections.Generic; 使用系统组件模型; 使用系统数据; 使用系统图; 使用系统文本; 使用System.Windows.Forms; 命名空间rubberbandrectangle { 公共部分类Form1:Form { 布尔
代码
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用系统文本;
使用System.Windows.Forms;
命名空间rubberbandrectangle
{
公共部分类Form1:Form
{
布尔bHaveMouse;
点ptOriginal=新点();
点ptLast=新点();
公共表格1()
{
初始化组件();
}
私有void Form1\u MouseDown(对象发送方,MouseEventArgs e)
{
bHaveMouse=true;
ptOriginal.X=e.X;
ptOriginal.Y=e.Y;
ptLast.X=-1;
ptLast.Y=-1;
}
私有void MyDrawInversibleRectangle(点p1,点p2)
{
矩形rc=新矩形();
p1=指向屏幕的点(p1);
p2=指向屏幕的点(p2);
if(p1.X
感谢阅读1)松开鼠标后,矩形消失。[我希望它留在表格上] 您需要重写窗体的方法,以便继续绘制矩形。现在,您可以在鼠标移动时绘制它,但之后也需要绘制它 2) 我还需要找到绘制矩形的四个点的坐标 这些应该在您的原始变量和原始变量中-您还需要什么 3) 我还需要删除矩形,以便在必要时绘制一个新矩形
停止绘制矩形,在OnPaint中绘制新的矩形。我正在寻找类似的东西,但对我找到的解决方案感到惊讶! 你有坐标对吗? 您可以只使用VisualBasic PowerPacks,它包含在我的Visual Studio 2008版本中 下面是一个示例代码,它将在文本框上绘制一个矩形,也就是说,我给它一个自定义边框 [守则] 代码是自描述的,但如果您有任何问题,只需留下一条消息。。。 是的,如果要删除矩形,只需处理控件(矩形或整个ShapeContainer),无需绘制,无需麻烦
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;
}
}
}
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()