using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace TestLayers { public partial class Form1 : Form { private struct Selection { public int Top; public int Left; public int Height; public int Width; } private bool wEdge; private bool eEdge; private bool nEdge; private bool sEdge; private bool Sizing; private const int EdgeSize = 6; private Point StartCoord; private Point OldCoord; private Point NewCoord; private Selection Rect; private bool MovePic; private bool MoveMain; private bool SizePic; private ControlLibrary.LayerPictureBox layerPictureBox4; public Form1() { InitializeComponent(); InitializeComponentExt(); pictureBox2.Parent = pictureBox1; pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox3.Parent = pictureBox2; pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox4.Parent = pictureBox2; pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage; layerPictureBox4.Parent = pictureBox2; layerPictureBox4.SizeMode = PictureBoxSizeMode.StretchImage; } private void InitializeComponentExt() { this.layerPictureBox4 = new ControlLibrary.LayerPictureBox(); // // layerPictureBox4 // this.layerPictureBox4.BackColor = System.Drawing.Color.Transparent; this.layerPictureBox4.Image = global::TestLayers.Properties.Resources.broculo; this.layerPictureBox4.Location = new System.Drawing.Point(37, 154); this.layerPictureBox4.Name = "pictureBox4"; this.layerPictureBox4.Size = new System.Drawing.Size(139, 48); this.layerPictureBox4.TabIndex = 7; this.layerPictureBox4.TabStop = false; //this.layerPictureBox4.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox4_MouseDown); //this.layerPictureBox4.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox4_MouseMove); //this.layerPictureBox4.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox4_MouseUp); } 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; } //} } private void Form1_Resize(object sender, EventArgs e) { ScrollAdjust(); } private void pictureBox2_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Middle) MoveMain = true; StartCoord = e.Location; } private void pictureBox2_MouseUp(object sender, MouseEventArgs e) { MoveMain = false; } private void pictureBox2_MouseMove(object sender, MouseEventArgs e) { if (MoveMain) { if (vScrollBar1.Enabled) { NewCoord.Y = pictureBox2.Top - (StartCoord.Y - e.Location.Y); if (NewCoord.Y > 0) NewCoord.Y = 0; if (NewCoord.Y < -vScrollBar1.Maximum) NewCoord.Y = -vScrollBar1.Maximum; pictureBox2.Top = NewCoord.Y; vScrollBar1.Value = -NewCoord.Y; } if (hScrollBar1.Enabled) { NewCoord.X = pictureBox2.Left - (StartCoord.X - e.Location.X); if (NewCoord.X > 0) NewCoord.X = 0; if (NewCoord.X < -hScrollBar1.Maximum) NewCoord.X = -hScrollBar1.Maximum; pictureBox2.Left = NewCoord.X; hScrollBar1.Value = -NewCoord.X; } } } private void pictureBox3_MouseDown(object sender, MouseEventArgs ee) { // Click on Sizable Image Region - NB. Child picturebox if (ee.Button == MouseButtons.Left) { OldCoord = ee.Location; StartCoord = ee.Location; if (Sizing) SizePic = true; else MovePic = true; } if (ee.Button == MouseButtons.Middle) { MoveMain = true; StartCoord = ee.Location; } } private void pictureBox3_MouseUp(object sender, MouseEventArgs e) { // Release Click in Sizable Image Region - NB. Child picturebox SizePic = false; MovePic = false; MoveMain = false; } private void pictureBox3_MouseMove(object sender, MouseEventArgs ee) { //Mouse move inside Image Region... - NB. Child Picturebox if (ee.Button == MouseButtons.None) { if (ee.Location.X < EdgeSize && ee.Location.X > 0) wEdge = true; else wEdge = false; if (ee.Location.X > pictureBox3.Size.Width - EdgeSize && ee.Location.X < pictureBox3.Size.Width) eEdge = true; else eEdge = false; if (ee.Location.Y < EdgeSize && ee.Location.Y > 0) nEdge = true; else nEdge = false; if (ee.Location.Y > pictureBox3.Size.Height - EdgeSize && ee.Location.Y < pictureBox3.Size.Height) sEdge = true; else sEdge = false; Sizing = true; } else if (ee.Button == MouseButtons.Left) { if (MovePic) { pictureBox3.Top = pictureBox3.Top + (ee.Location.Y - StartCoord.Y); pictureBox3.Left = pictureBox3.Left + (ee.Location.X - StartCoord.X); } // The code following works when used with a child Picturebox... // NB. Alternate code will apply if working on a rect region selection..... if (SizePic) { Rect.Top = pictureBox3.Top; Rect.Height = pictureBox3.Height; Rect.Left = pictureBox3.Left; Rect.Width = pictureBox3.Width; if (eEdge) Rect.Width = ee.Location.X; if (sEdge) Rect.Height = ee.Location.Y; if (wEdge) { Rect.Left = Rect.Left - (StartCoord.X - ee.Location.X); Rect.Width = Rect.Width + (StartCoord.X - ee.Location.X); } if (nEdge) { Rect.Top = Rect.Top - (StartCoord.Y - ee.Location.Y); Rect.Height = Rect.Height + (StartCoord.Y - ee.Location.Y); } if (Rect.Height < 0) { Rect.Top = Rect.Top + Rect.Height; Rect.Height = Math.Abs(Rect.Height); nEdge = !nEdge; sEdge = !sEdge; StartCoord.Y = 0; } if (Rect.Width < 0) { Rect.Left = Rect.Left + Rect.Width; Rect.Width = Math.Abs(Rect.Width); eEdge = !eEdge; wEdge = !wEdge; StartCoord.X = 0; } pictureBox3.Top = Rect.Top; pictureBox3.Height = Rect.Height; pictureBox3.Left = Rect.Left; pictureBox3.Width = Rect.Width; } } if ((nEdge || sEdge) && !(wEdge || eEdge)) pictureBox3.Cursor = Cursors.SizeNS; else if ((wEdge || eEdge) && !(nEdge || sEdge)) pictureBox3.Cursor = Cursors.SizeWE; else if ((nEdge && eEdge) || (sEdge && wEdge)) pictureBox3.Cursor = Cursors.SizeNESW; else if ((nEdge && wEdge) || (sEdge && eEdge)) pictureBox3.Cursor = Cursors.SizeNWSE; else { pictureBox3.Cursor = Cursors.Default; Sizing = false; } OldCoord = ee.Location; if (MoveMain) pictureBox2_MouseMove(sender, ee); } private void VScrollBar1_Scroll(object sender, ScrollEventArgs e) { pictureBox2.Top = -vScrollBar1.Value; } private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) { pictureBox2.Left = -hScrollBar1.Value; } private void pictureBox4_MouseDown(object sender, MouseEventArgs e) { // Click on Sizable Image Region - NB. Child picturebox if (e.Button == MouseButtons.Left) { OldCoord = e.Location; StartCoord = e.Location; if (Sizing) SizePic = true; else MovePic = true; } if (e.Button == MouseButtons.Middle) { MoveMain = true; StartCoord = e.Location; } } private void pictureBox4_MouseUp(object sender, MouseEventArgs e) { // Release Click in Sizable Image Region - NB. Child picturebox SizePic = false; MovePic = false; MoveMain = false; } private void pictureBox4_MouseMove(object sender, MouseEventArgs e) { //Mouse move inside Image Region... - NB. Child Picturebox if (e.Button == MouseButtons.None) { if (e.Location.X < EdgeSize && e.Location.X > 0) wEdge = true; else wEdge = false; if (e.Location.X > pictureBox4.Size.Width - EdgeSize && e.Location.X < pictureBox4.Size.Width) eEdge = true; else eEdge = false; if (e.Location.Y < EdgeSize && e.Location.Y > 0) nEdge = true; else nEdge = false; if (e.Location.Y > pictureBox4.Size.Height - EdgeSize && e.Location.Y < pictureBox4.Size.Height) sEdge = true; else sEdge = false; Sizing = true; } else if (e.Button == MouseButtons.Left) { if (MovePic) { pictureBox4.Top = pictureBox4.Top + (e.Location.Y - StartCoord.Y); pictureBox4.Left = pictureBox4.Left + (e.Location.X - StartCoord.X); } // The code following works when used with a child Picturebox... // NB. Alternate code will apply if working on a rect region selection..... if (SizePic) { Rect.Top = pictureBox4.Top; Rect.Height = pictureBox4.Height; Rect.Left = pictureBox4.Left; Rect.Width = pictureBox4.Width; if (eEdge) Rect.Width = e.Location.X; if (sEdge) Rect.Height = e.Location.Y; if (wEdge) { Rect.Left = Rect.Left - (StartCoord.X - e.Location.X); Rect.Width = Rect.Width + (StartCoord.X - e.Location.X); } if (nEdge) { Rect.Top = Rect.Top - (StartCoord.Y - e.Location.Y); Rect.Height = Rect.Height + (StartCoord.Y - e.Location.Y); } if (Rect.Height < 0) { Rect.Top = Rect.Top + Rect.Height; Rect.Height = Math.Abs(Rect.Height); nEdge = !nEdge; sEdge = !sEdge; StartCoord.Y = 0; } if (Rect.Width < 0) { Rect.Left = Rect.Left + Rect.Width; Rect.Width = Math.Abs(Rect.Width); eEdge = !eEdge; wEdge = !wEdge; StartCoord.X = 0; } pictureBox4.Top = Rect.Top; pictureBox4.Height = Rect.Height; pictureBox4.Left = Rect.Left; pictureBox4.Width = Rect.Width; } } if ((nEdge || sEdge) && !(wEdge || eEdge)) pictureBox4.Cursor = Cursors.SizeNS; else if ((wEdge || eEdge) && !(nEdge || sEdge)) pictureBox4.Cursor = Cursors.SizeWE; else if ((nEdge && eEdge) || (sEdge && wEdge)) pictureBox4.Cursor = Cursors.SizeNESW; else if ((nEdge && wEdge) || (sEdge && eEdge)) pictureBox4.Cursor = Cursors.SizeNWSE; else { pictureBox4.Cursor = Cursors.Default; Sizing = false; } OldCoord = e.Location; if (MoveMain) pictureBox2_MouseMove(sender, e); } } }