日期:2014-05-18 浏览次数:21204 次
public partial class Form1 : Form
{
Bitmap bitmap = new Bitmap(@"c:\temp\penguins.jpg");
RectangleF sourceRectange;
Point lastMouseDown;
Point lastMouseMove;
public Form1()
{
InitializeComponent();
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.lastMouseDown = this.lastMouseMove = e.Location;
}
protected override void OnMouseMove(MouseEventArgs e)
{
if( !this.Capture) return;
DrawReversibleFrame();
lastMouseMove = e.Location;
DrawReversibleFrame();
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
float l = sourceRectange.Left + sourceRectange.Width * lastMouseDown.X / this.ClientRectangle.Width;
float t = sourceRectange.Top + sourceRectange.Height * lastMouseDown.Y / this.ClientRectangle.Height;
float r = sourceRectange.Left + sourceRectange.Width * e.X / this.ClientRectangle.Width;
float b = sourceRectange.Top + sourceRectange.Height * e.Y / this.ClientRectangle.Height;
sourceRectange = RectangleF.FromLTRB(l, t, r, b);
}
else
{
this.sourceRectange = new RectangleF(PointF.Empty, bitmap.Size);
}
DrawReversibleFrame();
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
if ( Math.Abs(sourceRectange.Width * sourceRectange.Height) < 1) sourceRectange = new RectangleF(PointF.Empty, bitmap.Size);
e.Graphics.DrawImage(bitmap, (RectangleF)ClientRectangle, sourceRectange, GraphicsUnit.Pixel);
}
private void DrawReversibleFrame()
{
Rectangle rect = this.RectangleToScreen(Rectangle.FromLTRB(lastMouseMove.X, lastMouseMove.Y, lastMouseDown.X, lastMouseDown.Y));
ControlPaint.DrawReversibleFrame(rect, Color.Cyan, FrameStyle.Dashed);
}
}
------解决方案--------------------
http://blog.csdn.net/happy09li/article/details/7056567
参考
------解决方案--------------------
this.invaldate(true); 里面最好加个true参数