我想创建一个功能,当用户单击并在面板上拖动光标时,它将显示一个临时突出显示的矩形。当用户释放拖动时,矩形将立即消失。
我知道MouseMove,但它仍然让我感到困惑。我将不胜感激任何帮助。提前谢谢。
编辑: 好的,我现在找到了MouseMove的方式,这是我的代码:
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
int x;
int y;
int width;
int height;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle ee = new Rectangle(x, y, width, height);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, ee);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
width = e.X - x;
height = e.Y - y;
pictureBox1.Invalidate();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
x = e.X;
y = e.Y;
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
x = 0;
y = 0;
width = 0;
height = 0;
pictureBox1.Invalidate();
}
}
}
}
但是,当我尝试从右向左或从底部到顶部单击并拖动时,我遇到了问题,这将导致矩形的负值。对此事有什么想法吗?先感谢您。 :)