276 lines
11 KiB
C#
276 lines
11 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 this.pictureBox2.Image; }
|
|
set
|
|
{
|
|
//_image = value;
|
|
this.pictureBox2.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()
|
|
{
|
|
//using (pictureBox2)
|
|
//{
|
|
if (pictureBox2.Width < pictureBox1.Width)
|
|
{
|
|
pictureBox2.Left = (pictureBox1.Width - pictureBox2.Width) / 2;
|
|
hScrollBar1.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
if (pictureBox2.Left > 0)
|
|
pictureBox2.Left = 0;
|
|
hScrollBar1.Minimum = 0;
|
|
hScrollBar1.Maximum = pictureBox2.Width - pictureBox1.Width;
|
|
hScrollBar1.SmallChange = 1;
|
|
hScrollBar1.LargeChange = 10;
|
|
hScrollBar1.Enabled = true;
|
|
}
|
|
if (pictureBox2.Height < pictureBox1.Height)
|
|
{
|
|
pictureBox2.Top = (pictureBox1.Height - pictureBox2.Height) / 2;
|
|
vScrollBar1.Enabled = false;
|
|
}
|
|
else
|
|
{
|
|
if (pictureBox2.Top > 0)
|
|
pictureBox2.Top = 0;
|
|
vScrollBar1.Minimum = 0;
|
|
vScrollBar1.Maximum = pictureBox2.Height - pictureBox1.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 + pictureBox1.Location.X, e.Location.Y + pictureBox1.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
|
|
// pictureBox1 = new Rectangle(pictureBox1.X + ClientRectangle.Width / 4, pictureBox1.Y + ClientRectangle.Height / 4, pictureBox1.Width / 2, pictureBox1.Height / 2);
|
|
pictureBox1.Left = pictureBox1.Left + ClientRectangle.Width / 4;
|
|
pictureBox1.Top = pictureBox1.Top + ClientRectangle.Height / 4;
|
|
pictureBox1.Width = pictureBox1.Width / 2;
|
|
pictureBox1.Height = pictureBox1.Height / 2;
|
|
}
|
|
else
|
|
{
|
|
//Do the reverse of above
|
|
//pictureBox1 = new Rectangle(pictureBox1.X - ClientRectangle.Width / 4, pictureBox1.Y - ClientRectangle.Height / 4, ClientRectangle.Width, ClientRectangle.Height);
|
|
|
|
pictureBox1.Left = pictureBox1.Left - ClientRectangle.Width / 4;
|
|
pictureBox1.Top = pictureBox1.Top - ClientRectangle.Height / 4;
|
|
pictureBox1.Width = pictureBox1.Width;
|
|
pictureBox1.Height = pictureBox1.Height;
|
|
|
|
}
|
|
//Calculate Draw Rectangle by calling the resize event.
|
|
panBox_Resize(null, EventArgs.Empty);
|
|
}
|
|
}
|
|
|
|
void panBox_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
if (dragging)
|
|
{
|
|
pictureBox1.Location = new Point(start.X - e.Location.X, start.Y - e.Location.Y);
|
|
|
|
if (pictureBox1.Location.X < 0 - Padding.Left)
|
|
pictureBox1.Location = new Point(0 - Padding.Left, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y < 0 - Padding.Top)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, 0 - Padding.Top);
|
|
|
|
if (pictureBox1.Location.X > Image.Width - pictureBox1.Width + Padding.Right)
|
|
pictureBox1.Location = new Point(Image.Width - pictureBox1.Width + Padding.Right, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y > Image.Height - pictureBox1.Height + Padding.Bottom)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, Image.Height - pictureBox1.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)
|
|
{
|
|
pictureBox1.Width = ClientRectangle.Width / 2;
|
|
pictureBox1.Height = ClientRectangle.Height / 2;
|
|
}
|
|
else
|
|
{
|
|
pictureBox1.Width = ClientRectangle.Width;
|
|
pictureBox1.Height = ClientRectangle.Height;
|
|
}
|
|
|
|
if (pictureBox1.Location.X < 0 - Padding.Left)
|
|
pictureBox1.Location = new Point(0 - Padding.Left, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y < 0 - Padding.Top)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, 0 - Padding.Top);
|
|
|
|
if (pictureBox1.Location.X > Image.Width - pictureBox1.Width + Padding.Right)
|
|
pictureBox1.Location = new Point(Image.Width - pictureBox1.Width + Padding.Right, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y > Image.Height - pictureBox1.Height + Padding.Bottom)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, Image.Height - pictureBox1.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, pictureBox1, 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, pictureBox1.Left, -vScrollBar1.Value, 1);
|
|
pictureBox1.Location = new Point(start.X - ee.Location.X, start.Y - ee.Location.Y);
|
|
|
|
if (pictureBox1.Location.X < 0 - Padding.Left)
|
|
pictureBox1.Location = new Point(0 - Padding.Left, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y < 0 - Padding.Top)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, 0 - Padding.Top);
|
|
|
|
if (pictureBox1.Location.X > Image.Width - pictureBox1.Width + Padding.Right)
|
|
pictureBox1.Location = new Point(Image.Width - pictureBox1.Width + Padding.Right, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y > Image.Height - pictureBox1.Height + Padding.Bottom)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, Image.Height - pictureBox1.Height + Padding.Bottom);
|
|
this.Refresh();
|
|
}
|
|
|
|
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
|
|
{
|
|
MouseEventArgs ee = new MouseEventArgs(MouseButtons, 1, -hScrollBar1.Value, pictureBox1.Top, 1);
|
|
pictureBox1.Location = new Point(start.X - ee.Location.X, start.Y - ee.Location.Y);
|
|
|
|
if (pictureBox1.Location.X < 0 - Padding.Left)
|
|
pictureBox1.Location = new Point(0 - Padding.Left, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y < 0 - Padding.Top)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, 0 - Padding.Top);
|
|
|
|
if (pictureBox1.Location.X > Image.Width - pictureBox1.Width + Padding.Right)
|
|
pictureBox1.Location = new Point(Image.Width - pictureBox1.Width + Padding.Right, pictureBox1.Location.Y);
|
|
|
|
if (pictureBox1.Location.Y > Image.Height - pictureBox1.Height + Padding.Bottom)
|
|
pictureBox1.Location = new Point(pictureBox1.Location.X, Image.Height - pictureBox1.Height + Padding.Bottom);
|
|
this.Refresh();
|
|
}
|
|
|
|
private void DDPanBox_Resize(object sender, EventArgs e)
|
|
{
|
|
ScrollAdjust();
|
|
}
|
|
}
|
|
}
|