using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace WindowsApplication1
{
class PictureBoxDeluxe : System.Windows.Forms.PictureBox
{
public PictureBoxDeluxe()
{
}
#region props
private float _ZoomStep;
public float ZoomStep
{
get { return _ZoomStep; }
set { _ZoomStep = value; }
}
private bool _AllowZoom;
public bool AllowZoom
{
get { return _AllowZoom; }
set { _AllowZoom = value; }
}
private bool _AllowDrag = true;
public bool AllowDrag
{
get { return _AllowDrag; }
set { _AllowDrag = value; }
}
private float _ZoomLevel = 1;
public float ZoomLevel
{
get {return _ZoomLevel; }
set
{
_ZoomLevel = value;
this.Invalidate();
}
}
private Point _PictureLocation = new Point(0, 0);
public Point PictureLocation
{
get { return _PictureLocation; }
set
{
_PictureLocation = value;
this.Invalidate();
}
}
#endregion
protected override void OnPaint(System.Windows.Forms.PaintEventArgs pe)
{
if (this.Image != null)
{
pe.Graphics.DrawImage(
this.Image,
new Rectangle(
_PictureLocation.X,
_PictureLocation.Y,
(int)(_ZoomLevel * this.Image.Width),
(int)(_ZoomLevel * this.Image.Height)),
0,
0,
this.Image.Width,
this.Image.Height,
GraphicsUnit.Pixel,
new ImageAttributes());
}
}
private bool IsDragging = false;
private Point DragOffset = Point.Empty;
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
IsDragging = true;
DragOffset.X = e.X - _PictureLocation.X;
DragOffset.Y = e.Y - _PictureLocation.Y;
}
base.OnMouseDown(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
if (IsDragging)
{
this.PictureLocation = new Point(e.X - DragOffset.X, e.Y - DragOffset.Y);
}
base.OnMouseMove(e);
}
protected override void OnMouseUp(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) IsDragging = false;
base.OnMouseUp(e);
}
///
/// doesn't work
///
///
protected override void OnMouseWheel(MouseEventArgs e)
{
if (e.Delta < 0)
{
//scroll down, zoom out
this.ZoomLevel += _ZoomStep;
}
else if (e.Delta > 0)
{
this.ZoomLevel -= _ZoomStep;
}
base.OnMouseWheel(e);
}
}
}