C#中关于鼠标获取感兴趣区域方式,分享下
在Form1.cs中添加
private void imageBox1_MouseDown(object sender, MouseEventArgs e)
{
_x = e.X;
_y = e.Y;
isDragging = true;
}
private void imageBox1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragging)
{
int sx = _x < e.X ? _x : e.X;
int sy = _y < e.Y ? _y : e.Y;
int w = Math.Abs(_x - e.X);
int h = Math.Abs(_y - e.Y);
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
lastRect = new Rectangle(_x,_y, w,h);
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
}
}
private void imageBox1_MouseUp(object sender, MouseEventArgs e1)
{
if (isDragging)
{
isDragging = false;
ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);
// MessageBox.Show("the rect is " + lastRect);
}
}
在Form1.Designer.cs中private void InitializeComponent()添加
this.imageBox1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.imageBox1_MouseDoubleClick);
this.imageBox1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.imageBox1_MouseDown);
this.imageBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.imageBox1_MouseMove);
this.imageBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.imageBox1_MouseUp);
private void imageBox1_MouseDoubleClick(object sender, MouseEventArgs e1)
{
frame = 1;
//MessageBox.Show("the rect is " + lastRect);
}