257 lines
10 KiB
C#
257 lines
10 KiB
C#
using System;
|
|
using System.Drawing;
|
|
using System.Windows.Forms;
|
|
|
|
namespace ChildForms
|
|
{
|
|
public partial class DDPanBox : UserControl
|
|
{
|
|
//if true were at 2:1 false then 1:1
|
|
bool zoom = false;
|
|
bool dragging = false; //Tells us if our image has been clicked on.
|
|
Point start = new Point(); //Keep initial click for accurate panning.
|
|
private Image _image;
|
|
private Rectangle _drawRect;
|
|
|
|
|
|
public Image Image
|
|
{
|
|
get {return _image;}
|
|
set
|
|
{
|
|
_image = value;
|
|
//Calculate Draw Rectangle by calling the resize event.
|
|
panBox_Resize(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
public Rectangle DrawRect
|
|
{
|
|
get { return _drawRect; }
|
|
set { _drawRect = value; }
|
|
}
|
|
|
|
public static Rectangle DeflateRect(Rectangle rect, Padding padding)
|
|
{
|
|
rect.X += padding.Left;
|
|
rect.Y += padding.Top;
|
|
rect.Width -= padding.Horizontal;
|
|
rect.Height -= padding.Vertical;
|
|
return rect;
|
|
|
|
}
|
|
|
|
public void ClearImage(Graphics g)
|
|
{
|
|
g.Clear(Color.Black);
|
|
|
|
//picBox.Refresh();
|
|
//this.Image.Dispose();
|
|
//this.Image = null;
|
|
//this.Refresh();
|
|
//Graphics g = Graphics.FromImage(_image);
|
|
//Color c = Color.FromArgb(52, 52, 52);
|
|
//g.Clear(c);
|
|
//this.Invalidate();
|
|
//this.Refresh();
|
|
|
|
}
|
|
|
|
|
|
public DDPanBox()
|
|
{
|
|
InitializeComponent();
|
|
//Set up double buffering and a little extra.
|
|
this.SetStyle(ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer |
|
|
ControlStyles.AllPaintingInWmPaint | ControlStyles.SupportsTransparentBackColor,
|
|
true);
|
|
|
|
//set the part of the source image to be drawn.
|
|
//DrawRect = DeflateRect(ClientRectangle, Padding);
|
|
_drawRect = ClientRectangle;
|
|
|
|
//Subscribe to our event handlers.
|
|
this.MouseDown += new MouseEventHandler(panBox_MouseDown);
|
|
this.MouseMove += new MouseEventHandler(panBox_MouseMove);
|
|
this.MouseUp += new MouseEventHandler(panBox_MouseUp);
|
|
this.Resize += new EventHandler(panBox_Resize);
|
|
}
|
|
|
|
//private void ScrollAdjust()
|
|
//{
|
|
// if (this.Width < _drawRect.Width)
|
|
// {
|
|
// this.Left = (_drawRect.Width - this.Width) / 2;
|
|
// hScrollBar1.Enabled = false;
|
|
// }
|
|
// else
|
|
// {
|
|
// if (this.Left > 0)
|
|
// this.Left = 0;
|
|
// hScrollBar1.Minimum = 0;
|
|
// hScrollBar1.Maximum = this.Width - _drawRect.Width;
|
|
// hScrollBar1.SmallChange = 1;
|
|
// hScrollBar1.LargeChange = 10;
|
|
// hScrollBar1.Enabled = true;
|
|
// }
|
|
// if (this.Height < _drawRect.Height)
|
|
// {
|
|
// this.Top = (_drawRect.Height - this.Height) / 2;
|
|
// vScrollBar1.Enabled = false;
|
|
// }
|
|
// else
|
|
// {
|
|
// if (this.Top > 0)
|
|
// this.Top = 0;
|
|
// vScrollBar1.Minimum = 0;
|
|
// vScrollBar1.Maximum = this.Height - _drawRect.Height;
|
|
// vScrollBar1.SmallChange = 1;
|
|
// vScrollBar1.LargeChange = 10;
|
|
// vScrollBar1.Enabled = true;
|
|
// }
|
|
//}
|
|
|
|
void panBox_MouseDown(object sender, MouseEventArgs e)
|
|
{
|
|
if (e.Button == MouseButtons.Left)
|
|
{
|
|
dragging = true;
|
|
//offset new point by original one so we know where in the image we are.
|
|
start = new Point(e.Location.X + _drawRect.Location.X, e.Location.Y + _drawRect.Location.Y);
|
|
Cursor = Cursors.SizeAll; //just for looks.
|
|
}
|
|
else if (e.Button == MouseButtons.Right)
|
|
{
|
|
zoom = !zoom;
|
|
if (zoom)
|
|
{
|
|
// modify the drawrect to a smaller rectangle to zoom, and center it in previous drawrect
|
|
_drawRect = new Rectangle(_drawRect.X + ClientRectangle.Width / 4, _drawRect.Y + ClientRectangle.Height / 4, _drawRect.Width / 2, _drawRect.Height / 2);
|
|
}
|
|
else
|
|
{
|
|
//Do the reverse of above
|
|
_drawRect = new Rectangle(_drawRect.X - ClientRectangle.Width / 4, _drawRect.Y - ClientRectangle.Height / 4, ClientRectangle.Width, ClientRectangle.Height);
|
|
}
|
|
//Calculate Draw Rectangle by calling the resize event.
|
|
panBox_Resize(null, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
void panBox_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (dragging)
|
|
{
|
|
_drawRect.Location = new Point(start.X - e.Location.X, start.Y - e.Location.Y);
|
|
|
|
if (_drawRect.Location.X < 0 - Padding.Left)
|
|
_drawRect.Location = new Point(0 - Padding.Left, _drawRect.Location.Y);
|
|
|
|
if (_drawRect.Location.Y < 0 - Padding.Top)
|
|
_drawRect.Location = new Point(_drawRect.Location.X, 0 - Padding.Top);
|
|
|
|
if (_drawRect.Location.X > _image.Width - _drawRect.Width + Padding.Right)
|
|
_drawRect.Location = new Point(_image.Width - _drawRect.Width + Padding.Right, _drawRect.Location.Y);
|
|
|
|
if (_drawRect.Location.Y > _image.Height - _drawRect.Height + Padding.Bottom)
|
|
_drawRect.Location = new Point(_drawRect.Location.X, _image.Height - _drawRect.Height + Padding.Bottom);
|
|
|
|
this.Refresh();
|
|
}
|
|
}
|
|
|
|
void panBox_MouseUp(object sender, MouseEventArgs e)
|
|
{
|
|
dragging = false;
|
|
Cursor = Cursors.Default;
|
|
}
|
|
|
|
void panBox_Resize(object sender, EventArgs e)
|
|
{
|
|
if (_image != null)
|
|
{
|
|
if (zoom)
|
|
{
|
|
_drawRect = new Rectangle(_drawRect.Location.X, _drawRect.Location.Y, ClientRectangle.Width / 2, ClientRectangle.Height / 2);
|
|
}
|
|
else
|
|
_drawRect = new Rectangle(_drawRect.Location.X, _drawRect.Location.Y, ClientRectangle.Width, ClientRectangle.Height);
|
|
|
|
if (_drawRect.Location.X < 0 - Padding.Left)
|
|
_drawRect.Location = new Point(0 - Padding.Left, _drawRect.Location.Y);
|
|
|
|
if (_drawRect.Location.Y < 0 - Padding.Top)
|
|
_drawRect.Location = new Point(_drawRect.Location.X, 0 - Padding.Top);
|
|
|
|
if (_drawRect.Location.X > _image.Width - _drawRect.Width + Padding.Right)
|
|
_drawRect.Location = new Point(_image.Width - _drawRect.Width + Padding.Right, _drawRect.Location.Y);
|
|
|
|
if (_drawRect.Location.Y > _image.Height - _drawRect.Height + Padding.Bottom)
|
|
_drawRect.Location = new Point(_drawRect.Location.X, _image.Height - _drawRect.Height + Padding.Bottom);
|
|
|
|
this.Refresh();
|
|
}
|
|
}
|
|
|
|
protected override void OnPaint(PaintEventArgs e)
|
|
{
|
|
if (_image != null)
|
|
{
|
|
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
|
|
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
|
|
e.Graphics.DrawImage(_image, ClientRectangle, _drawRect, GraphicsUnit.Pixel);
|
|
|
|
if (zoom)
|
|
e.Graphics.DrawString("Zoom 2:1", this.Font, Brushes.White, new PointF(15F, 15F));
|
|
else
|
|
e.Graphics.DrawString("Zoom 1:1", this.Font, Brushes.White, new PointF(15F, 15F));
|
|
}
|
|
|
|
base.OnPaint(e);
|
|
}
|
|
|
|
//private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
|
|
//{
|
|
// MouseEventArgs ee = new MouseEventArgs(MouseButtons, 1, _drawRect.Left, -vScrollBar1.Value, 1);
|
|
// _drawRect.Location = new Point(start.X - ee.Location.X, start.Y - ee.Location.Y);
|
|
|
|
// if (_drawRect.Location.X < 0 - Padding.Left)
|
|
// _drawRect.Location = new Point(0 - Padding.Left, _drawRect.Location.Y);
|
|
|
|
// if (_drawRect.Location.Y < 0 - Padding.Top)
|
|
// _drawRect.Location = new Point(_drawRect.Location.X, 0 - Padding.Top);
|
|
|
|
// if (_drawRect.Location.X > _image.Width - _drawRect.Width + Padding.Right)
|
|
// _drawRect.Location = new Point(_image.Width - _drawRect.Width + Padding.Right, _drawRect.Location.Y);
|
|
|
|
// if (_drawRect.Location.Y > _image.Height - _drawRect.Height + Padding.Bottom)
|
|
// _drawRect.Location = new Point(_drawRect.Location.X, _image.Height - _drawRect.Height + Padding.Bottom);
|
|
// this.Refresh();
|
|
//}
|
|
|
|
//private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
|
|
//{
|
|
// MouseEventArgs ee = new MouseEventArgs(MouseButtons, 1, -hScrollBar1.Value, _drawRect.Top, 1);
|
|
// _drawRect.Location = new Point(start.X - ee.Location.X, start.Y - ee.Location.Y);
|
|
|
|
// if (_drawRect.Location.X < 0 - Padding.Left)
|
|
// _drawRect.Location = new Point(0 - Padding.Left, _drawRect.Location.Y);
|
|
|
|
// if (_drawRect.Location.Y < 0 - Padding.Top)
|
|
// _drawRect.Location = new Point(_drawRect.Location.X, 0 - Padding.Top);
|
|
|
|
// if (_drawRect.Location.X > _image.Width - _drawRect.Width + Padding.Right)
|
|
// _drawRect.Location = new Point(_image.Width - _drawRect.Width + Padding.Right, _drawRect.Location.Y);
|
|
|
|
// if (_drawRect.Location.Y > _image.Height - _drawRect.Height + Padding.Bottom)
|
|
// _drawRect.Location = new Point(_drawRect.Location.X, _image.Height - _drawRect.Height + Padding.Bottom);
|
|
// this.Refresh();
|
|
//}
|
|
|
|
private void DDPanBox_Resize(object sender, EventArgs e)
|
|
{
|
|
//ScrollAdjust();
|
|
}
|
|
}
|
|
}
|