这东西邪门的很~ 开了之后无法相应 MouseDown 和 MouseUp了
msdn: 当控件已捕获鼠标时,它接收鼠标输入,不论光标是否在其边框内。 通常只有在执行拖动操作时才捕获鼠标。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Point DownPoint = Point.Empty;
private Pen MyPen;
private Rectangle srcRect;
private bool f = false;
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (srcRect != Rectangle.Empty)
{
MyPen = new Pen(Color.Black, 1);
e.Graphics.DrawRectangle(MyPen, srcRect);
}
}
private void pictureBox2_Paint(object sender, PaintEventArgs e)
{
if (srcRect != Rectangle.Empty)
{
e.Graphics.DrawImage(this.pictureBox1.Image, new Rectangle(0, 0, srcRect.Width, srcRect.Height), srcRect, GraphicsUnit.Pixel);
}
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (f)
{
int w = Math.Abs(e.X - DownPoint.X);
int h = Math.Abs(e.Y - DownPoint.Y);
if (e.X > DownPoint.X)
srcRect = new Rectangle(DownPoint.X, DownPoint.Y, w, h);
else
srcRect = new Rectangle(e.X, e.Y, w, h);
this.pictureBox1.Invalidate();
this.pictureBox1.Update();
this.pictureBox2.Invalidate();
this.pictureBox2.Update();
}
}
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
this.Capture = true; //注释掉后一切正常
if (DownPoint == Point.Empty)
{
f = true;
DownPoint.X = e.X;
DownPoint.Y = e.Y;
}
}
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
if (DownPoint != Point.Empty)
{
DownPoint = Point.Empty;
srcRect = Rectangle.Empty;
this.pictureBox1.Invalidate();
this.pictureBox1.Update();
}
MessageBox.Show("up");
f = false;
}
}
}