思路:
改变矩形大小:新的长宽 = 原始长宽 + 鼠标偏移量
上面鼠标偏移量是指:(移动到的位置MouseMove - 鼠标点击下去的位置MouseDown)
改变矩形位置:图形新坐标 = 当前鼠标坐标 - 偏移量(代码中的思路)
上面偏移量是指:(鼠标点下去的位置MouseDown - 图形的左上角坐标这里是图形的起始坐标)
改变矩形位置2:新坐标 = 图形原始坐标 +(鼠标到移动到的位置 - 鼠标点下去的位置)<个人思路,感兴趣的可以尝试一下>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sunny.UI;
namespace DVP_CSharp.UI
{
public partial class frmDResultView : UIForm
{
public frmDResultView()
{
InitializeComponent();
}
private Rectangle draggableRectangle1; //画矩形
private Rectangle draggableRectangle2;
private Rectangle draggableRectangle3;
private Rectangle draggableRectangle4;
private bool isResizing1; //是否可改变大小
private bool isMoving1; //是否可移动
private bool isResizing2;
private bool isMoving2;
private bool isResizing3;
private bool isMoving3;
private bool isResizing4;
private bool isMoving4;
private Point lastMousePosition; //记录鼠标点击下去后的位置
private Point mouseOffset; //记录鼠标偏移量
private void frmDResultView_Load(object sender, EventArgs e)
{
this.PictureBox.Load(DiameterTest.result_path);
PictureBox.Paint += PictureBox_Paint;
PictureBox.MouseDown += PictureBox1_MouseDown;
PictureBox.MouseUp += PictureBox1_MouseUp;
PictureBox.MouseMove += PictureBox1_MouseMove;
//初始化先画矩形
//上
int x1 = 619;
int y1 = 23;
int width1 = 100;
int height1 = 50;
draggableRectangle1 = new Rectangle(x1, y1, width1, height1);
//右
int x2 = 1010;
int y2 = 329;
int width2 = 50;
int height2 = 100;
draggableRectangle2 = new Rectangle(x2, y2, width2, height2);
//下
int x3 = 619;
int y3 = 730;
int width3 = 100;
int height3 = 50;
draggableRectangle3 = new Rectangle(x3, y3, width3, height3);
//左
int x4 = 300;
int y4 = 329;
int width4 = 50;
int height4 = 100;
draggableRectangle4 = new Rectangle(x4, y4, width4, height4);
PictureBox.Invalidate();
}
//绘制矩形
private void PictureBox_Paint(object sender, PaintEventArgs e)
{
if (draggableRectangle1 != null && draggableRectangle2 != null && draggableRectangle3 != null && draggableRectangle4 != null)
{
e.Graphics.DrawRectangle(Pens.Red, draggableRectangle1);
e.Graphics.DrawRectangle(Pens.Red, draggableRectangle2);
e.Graphics.DrawRectangle(Pens.Red, draggableRectangle3);
e.Graphics.DrawRectangle(Pens.Red, draggableRectangle4);
}
}
private void PictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (draggableRectangle1 != null && draggableRectangle1.Contains(e.Location))
{
if (e.Button == MouseButtons.Left)
{
isResizing1 = true;
lastMousePosition = e.Location;
}
else if (e.Button == MouseButtons.Right)
{
isMoving1 = true;
mouseOffset = new Point(e.X - draggableRectangle1.X, e.Y - draggableRectangle1.Y);
}
}
else if (draggableRectangle2 != null && draggableRectangle2.Contains(e.Location))
{
if (e.Button == MouseButtons.Left)
{
isResizing2 = true;
lastMousePosition = e.Location;
}
else if (e.Button == MouseButtons.Right)
{
isMoving2 = true;
mouseOffset = new Point(e.X - draggableRectangle2.X, e.Y - draggableRectangle2.Y);
}
}
else if (draggableRectangle3 != null && draggableRectangle3.Contains(e.Location))
{
if (e.Button == MouseButtons.Left)
{
isResizing3 = true;
lastMousePosition = e.Location;
}
else if (e.Button == MouseButtons.Right)
{
isMoving3 = true;
mouseOffset = new Point(e.X - draggableRectangle3.X, e.Y - draggableRectangle3.Y);
}
}
else if (draggableRectangle4 != null && draggableRectangle4.Contains(e.Location))
{
if (e.Button == MouseButtons.Left)
{
isResizing4 = true;
lastMousePosition = e.Location;
}
else if (e.Button == MouseButtons.Right)
{
isMoving4 = true;
mouseOffset = new Point(e.X - draggableRectangle4.X, e.Y - draggableRectangle4.Y);
}
}
}
private void PictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isResizing1 = false;
isMoving1 = false;
isResizing2 = false;
isMoving2 = false;
isResizing3 = false;
isMoving3 = false;
isResizing4 = false;
isMoving4 = false;
}
private void PictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isResizing1)
{
//改变大小:新的长宽 = 原始长宽 + 鼠标偏移量
int widthChange = e.X - lastMousePosition.X;
int heightChange = e.Y - lastMousePosition.Y;
draggableRectangle1.Width += widthChange;
draggableRectangle1.Height += heightChange;
lastMousePosition = e.Location;
PictureBox.Invalidate();
}
else if (isMoving1)
{
//改变位置:新坐标 = 当前鼠标坐标 - 鼠标偏移量
int newX = e.X - mouseOffset.X;
int newY = e.Y - mouseOffset.Y;
draggableRectangle1.Location = new Point(newX, newY);
PictureBox.Invalidate();
}
else if (isResizing2)
{
int widthChange = e.X - lastMousePosition.X;
int heightChange = e.Y - lastMousePosition.Y;
draggableRectangle2.Width += widthChange;
draggableRectangle2.Height += heightChange;
lastMousePosition = e.Location;
PictureBox.Invalidate();
}
else if (isMoving2)
{
int newX = e.X - mouseOffset.X;
int newY = e.Y - mouseOffset.Y;
draggableRectangle2.Location = new Point(newX, newY);
PictureBox.Invalidate();
}
else if (isResizing3)
{
int widthChange = e.X - lastMousePosition.X;
int heightChange = e.Y - lastMousePosition.Y;
draggableRectangle3.Width += widthChange;
draggableRectangle3.Height += heightChange;
lastMousePosition = e.Location;
PictureBox.Invalidate();
}
else if (isMoving3)
{
int newX = e.X - mouseOffset.X;
int newY = e.Y - mouseOffset.Y;
draggableRectangle3.Location = new Point(newX, newY);
PictureBox.Invalidate();
}
else if (isResizing4)
{
int widthChange = e.X - lastMousePosition.X;
int heightChange = e.Y - lastMousePosition.Y;
draggableRectangle4.Width += widthChange;
draggableRectangle4.Height += heightChange;
lastMousePosition = e.Location;
PictureBox.Invalidate();
}
else if (isMoving4)
{
int newX = e.X - mouseOffset.X;
int newY = e.Y - mouseOffset.Y;
draggableRectangle4.Location = new Point(newX, newY);
PictureBox.Invalidate();
}
//获取坐标
textBox1.Text = e.X.ToString() + "," + e.Y.ToString();
}
private void uiSymbolButton1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}