如何通过面板上的点击拖动来进行高亮显示
问题描述:
我想创建一个功能,当用户在面板上单击并拖动光标时,它将显示一个临时突出显示的矩形。当用户释放他的拖动时,矩形将立即消失。如何通过面板上的点击拖动来进行高亮显示
我已经知道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();
}
}
}
}
但尽管如此,我当我试图单击从右到一个问题和阻力左或从底部到顶部,这将导致矩形的负值。这件事有什么想法吗?先谢谢你。 :)
使用面板的Paint事件绘制矩形。当鼠标移动时,通过调用它的Invalidate()方法来运行它。 –
[在拖拽时突出显示矩形区域]可能的重复(http://stackoverflow.com/questions/10091356/highlight-the-rectangular-area-while-dragging-it) –
@HansPassant是的,在我编辑的文章中已经找到了这种方式,但是当我尝试点击并从右向左或从下向上拖动时,它仍然不显示矩形,这将导致矩形的负值。 –