using System; using System.Windows.Forms; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ControlLibrary { public partial class LayerPictureBox : PictureBox { 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; public LayerPictureBox() { InitializeComponent(); } public LayerPictureBox(IContainer container) { container.Add(this); InitializeComponent(); } private void LayerPictureBox_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 LayerPictureBox_MouseUp(object sender, MouseEventArgs e) { // Release Click in Sizable Image Region - NB. Child picturebox SizePic = false; MovePic = false; MoveMain = false; } private void LayerPictureBox_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 > this.Size.Width - EdgeSize && ee.Location.X < this.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 > this.Size.Height - EdgeSize && ee.Location.Y < this.Size.Height) sEdge = true; else sEdge = false; Sizing = true; } else if (ee.Button == MouseButtons.Left) { if (MovePic) { this.Top = this.Top + (ee.Location.Y - StartCoord.Y); this.Left = this.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 = this.Top; Rect.Height = this.Height; Rect.Left = this.Left; Rect.Width = this.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; } this.Top = Rect.Top; this.Height = Rect.Height; this.Left = Rect.Left; this.Width = Rect.Width; } } if ((nEdge || sEdge) && !(wEdge || eEdge)) this.Cursor = Cursors.SizeNS; else if ((wEdge || eEdge) && !(nEdge || sEdge)) this.Cursor = Cursors.SizeWE; else if ((nEdge && eEdge) || (sEdge && wEdge)) this.Cursor = Cursors.SizeNESW; else if ((nEdge && wEdge) || (sEdge && eEdge)) this.Cursor = Cursors.SizeNWSE; else { this.Cursor = Cursors.Default; Sizing = false; } OldCoord = ee.Location; //if (MoveMain) // pictureBox2_MouseMove(sender, ee); } } }