 |
|
 |
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Windows.Forms;
namespace ScreenEditor { /// /// This class implements sizing and moving functions for /// runtime editing of graphic controls /// public class ObjectSelect { private class DummySelect { public Control dummyCtl; public Label[] lbl = new Label[8]; } ////////////////////////////////////////////////////////////////// // PRIVATE CONSTANTS AND VARIABLES //////////////////////////////////////////////////////////////////
private const int BOX_SIZE = 8; private Color BOX_COLOR = Color.White; //private ContainerControl m_container; private Control m_control; private Label[] lbl = new Label[8]; private int startl; private int startt; private int startw; private int starth; private int startx; private int starty; private bool dragging; private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE}; private Cursor oldCursor; private Rectangle objectContainer;
private const int MIN_SIZE = 20;
//DUMMY Select labels List private List DummySelectList = new List();
// Constructor creates 8 sizing handles & wires mouse events // to each that implement sizing functions public ObjectSelect() { for (int i = 0; i < 8; i++) { lbl[i] = new Label(); lbl[i].TabIndex = i; lbl[i].FlatStyle = 0; lbl[i].BorderStyle = BorderStyle.FixedSingle; lbl[i].BackColor = BOX_COLOR; lbl[i].Cursor = arrArrow[i]; lbl[i].Text = ""; lbl[i].BringToFront(); lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown); lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove); lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp); } }
#region PUBLIC METHODS
// Wires a Click event handler to the passed Control // that attaches a pick box to the control when it is clicked public void WireControl(Control ctl) { ctl.Click += new EventHandler(this.SelectControl); }
//Call this when removing an object from the container, this simply //cleans up some event handlers from the system, make sure you call //before actually destroying the Control. public void UnWireControl(Control ctl) { if (m_control == ctl) { //m_control will be the only one having these handlers ctl.MouseDown -= new MouseEventHandler(this.ctl_MouseDown); ctl.MouseMove -= new MouseEventHandler(this.ctl_MouseMove); ctl.MouseUp -= new MouseEventHandler(this.ctl_MouseUp); for (int i = 0; i < 8; i++) { lbl[i].Visible = false; } m_control.Cursor = oldCursor; m_control = null; }
ctl.Click -= new EventHandler(this.SelectControl);
RemoveDummyFromList(ctl); }
//This will establish a boundry for the objects within it public void SetScreenContainerBounds(Rectangle ScreenContainerBounds) { objectContainer = ScreenContainerBounds; }
//Capture the Arrow Keys keyboard presses and send them here public void MoveSelectedObject(int ArrowDirection) { if (m_control == null) return;
int t = m_control.Top; int l = m_control.Left;
switch (ArrowDirection) { case (int)Keys.Left: l = l - 1; break; case (int)Keys.Right: l = l + 1; break; case (int)Keys.Up: t = t - 1; break; case (int)Keys.Down: t = t + 1; break; }
int holdT = t; int holdL = l;
int w = m_control.Width; int h = m_control.Height;
int RightBoundry = objectContainer.Width + objectContainer.Left; int BottomBoundry = objectContainer.Height + objectContainer.Top; int LeftBoundry = objectContainer.Left; int TopBoundry = objectContainer.Top;
l = (l < LeftBoundry) ? LeftBoundry : ((l + w > RightBoundry) ? RightBoundry - w : l); t = (t < TopBoundry) ? TopBoundry : ((t + h > BottomBoundry) ? BottomBoundry - h : t);
m_control.Left = l; m_control.Top = t;
if (holdL != l || holdT != t) return;//we have reached the border so discontinue
//Now move any dummy objects foreach (DummySelect ds in DummySelectList) { l = ds.dummyCtl.Left; t = ds.dummyCtl.Top; w = ds.dummyCtl.Width; h = ds.dummyCtl.Height;
switch (ArrowDirection) { case (int)Keys.Left: l = l - 1; break; case (int)Keys.Right: l = l + 1; break; case (int)Keys.Up: t = t - 1; break; case (int)Keys.Down: t = t + 1; break; }
RightBoundry = objectContainer.Width + objectContainer.Left; BottomBoundry = objectContainer.Height + objectContainer.Top; LeftBoundry = objectContainer.Left; TopBoundry = objectContainer.Top;
l = (l < LeftBoundry) ? LeftBoundry : ((l + w > RightBoundry) ? RightBoundry - w : l); t = (t < TopBoundry) ? TopBoundry : ((t + h > BottomBoundry) ? BottomBoundry - h : t);
ds.dummyCtl.Left = l; ds.dummyCtl.Top = t; }
MoveHandles(); ShowHandles(); } //Get an array of the controls that have a selection band internal Control[] GetSelectedControls() { int controlCount = DummySelectList.Count; if (m_control != null)//shouldnt be null if this function is called controlCount++;
Control [] cArray = new Control[controlCount]; int i = 0; foreach (DummySelect ds in DummySelectList) { cArray[i] = ds.dummyCtl; i++; } if (m_control != null)//shouldnt be null if this function is called cArray[i] = m_control;
return cArray; } #endregion #region PRIVATE METHODS
// Attaches a pick box to the sender Control private void SelectControl(object sender, EventArgs e) { if ((Control)sender == m_control) return;//user clicked on the same control
if (Control.ModifierKeys == Keys.Control) RemoveDummyFromList((Control)sender); else RemoveDummyFromList(null);//null will remove all dummy's
if (m_control is Control) { m_control.Cursor = oldCursor;
//Remove event any pre-existing event handlers appended by this class m_control.MouseDown -= new MouseEventHandler(this.ctl_MouseDown); m_control.MouseMove -= new MouseEventHandler(this.ctl_MouseMove); m_control.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
//Is the Control Key down? if (Control.ModifierKeys == Keys.Control) { //("Control Key is down"); //Then add the control that was selected to the dummy list //and put off-color labels on it DummySelect ds = new DummySelect(); ds.dummyCtl = m_control; for (int i = 0; i < 8; i++) { ds.lbl[i] = new Label(); ds.lbl[i].TabIndex = i; ds.lbl[i].FlatStyle = 0; ds.lbl[i].BorderStyle = BorderStyle.FixedSingle; ds.lbl[i].BackColor = Color.CadetBlue; ds.lbl[i].Text = ""; ds.dummyCtl.Parent.Controls.Add(ds.lbl[i]); ds.lbl[i].BringToFront(); ds.lbl[i].Visible = true; }
DummySelectList.Add(ds); }
m_control = null; }
m_control = (Control)sender;
//Add event handlers for moving the selected control around m_control.MouseDown += new MouseEventHandler(this.ctl_MouseDown); m_control.MouseMove += new MouseEventHandler(this.ctl_MouseMove); m_control.MouseUp += new MouseEventHandler(this.ctl_MouseUp); m_control.BringToFront();
//Add sizing handles to Control's container (Form or PictureBox) for (int i = 0; i < 8; i++) { m_control.Parent.Controls.Add(lbl[i]); lbl[i].BringToFront(); }
//Position sizing handles around Control MoveHandles();
//Display sizing handles ShowHandles();
oldCursor = m_control.Cursor; m_control.Cursor = Cursors.Hand; }
private void RemoveDummyFromList(Control control) { if (control != null) { DummySelect dsToRemove = null; foreach (DummySelect ds in DummySelectList) { if (ds.dummyCtl == control) { for (int i = 0; i < 8; i++) ds.dummyCtl.Parent.Controls.Remove(ds.lbl[i]);
dsToRemove = ds; break; } } if (dsToRemove != null) DummySelectList.Remove(dsToRemove); } else// if control is null then remove all { foreach (DummySelect ds in DummySelectList) { for (int i = 0; i < 8; i++) ds.dummyCtl.Parent.Controls.Remove(ds.lbl[i]); } DummySelectList.Clear(); } }
public void Remove() { HideHandles(); m_control.Cursor = oldCursor; }
private void ShowHandles() { if (m_control != null) { for (int i = 0; i < 8; i++) { lbl[i].Visible = true; }
//Show any dummy's labels foreach (DummySelect ds in DummySelectList) { for (int i = 0; i < 8; i++) { ds.lbl[i].Visible = true; } } } }
private void HideHandles() { for (int i = 0; i < 8; i++) { lbl[i].Visible = false; }
//Hide any dummy's labels foreach (DummySelect ds in DummySelectList) { for (int i = 0; i < 8; i++) { ds.lbl[i].Visible = false; } } }
private void MoveHandles() { int sX = m_control.Left - BOX_SIZE; int sY = m_control.Top - BOX_SIZE; int sW = m_control.Width + BOX_SIZE; int sH = m_control.Height + BOX_SIZE; int hB = BOX_SIZE / 2; int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB, sX + sW-hB, sX + sW / 2, sX+hB, sX+hB}; int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB, sY + sH-hB, sY + sH-hB, sY + sH / 2}; for (int i = 0; i < 8; i++) lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE);
foreach (DummySelect ds in DummySelectList) { sX = ds.dummyCtl.Left - BOX_SIZE; sY = ds.dummyCtl.Top - BOX_SIZE; sW = ds.dummyCtl.Width + BOX_SIZE; sH = ds.dummyCtl.Height + BOX_SIZE; hB = BOX_SIZE / 2; arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB, sX + sW-hB, sX + sW / 2, sX+hB, sX+hB}; arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB, sY + sH-hB, sY + sH-hB, sY + sH / 2}; for (int i = 0; i < 8; i++) ds.lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE); } }
///////////////////////////////////////////////////////////////// // MOUSE EVENTS THAT IMPLEMENT SIZING OF THE PICKED CONTROL /////////////////////////////////////////////////////////////////
// Store control position and size when mouse button pushed over // any sizing handle private void lbl_MouseDown(object sender, MouseEventArgs e) { dragging = true; startl = m_control.Left; startt = m_control.Top; startw = m_control.Width; starth = m_control.Height; HideHandles(); }
// Size the picked control in accordance with sizing handle being dragged // 0 1 2 // 7 3 // 6 5 4 private void lbl_MouseMove(object sender, MouseEventArgs e) { int l = m_control.Left; int w = m_control.Width; int t = m_control.Top; int h = m_control.Height; int RightBoundry = objectContainer.Width + objectContainer.Left; int BottomBoundry = objectContainer.Height + objectContainer.Top; int LeftBoundry = objectContainer.Left; int TopBoundry = objectContainer.Top;
if (dragging) { switch (((Label)sender).TabIndex) { case 0: // Dragging top-left sizing box if (t >= TopBoundry) { t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - m_control.Top; } if (l >= LeftBoundry) { l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - m_control.Left; } break; case 1: // Dragging top-center sizing box if (t >= TopBoundry) { t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - m_control.Top; } break; case 2: // Dragging top-right sizing box if (l + (startw + e.X) < RightBoundry) w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; if (t >= TopBoundry) { t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - m_control.Top; } break; case 3: // Dragging right-middle sizing box if (l + (startw + e.X) < RightBoundry) w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; break; case 4: // Dragging right-bottom sizing box if (l + (startw + e.X) < RightBoundry) w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; if (t + (starth + e.Y) < BottomBoundry) h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 5: // Dragging center-bottom sizing box if (t + (starth + e.Y) < BottomBoundry) h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 6: // Dragging left-bottom sizing box if (l >= LeftBoundry) { l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - m_control.Left; } if (t + (starth + e.Y) <= BottomBoundry) h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 7: // Dragging left-middle sizing box if (l >= LeftBoundry) { l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - m_control.Left; } break; }
l = (l < LeftBoundry) ? LeftBoundry : l; t = (t < TopBoundry) ? TopBoundry : t; m_control.SetBounds(l, t, w, h); } }
// Display sizing handles around picked control once sizing has completed private void lbl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); }
///////////////////////////////////////////////////////////////// // MOUSE EVENTS THAT MOVE THE PICKED CONTROL AROUND THE FORM /////////////////////////////////////////////////////////////////
// Get mouse pointer starting position on mouse down and hide sizing handles private void ctl_MouseDown(object sender, MouseEventArgs e) { dragging = true; startx = e.X; starty = e.Y; HideHandles(); }
// Reposition the dragged control private void ctl_MouseMove(object sender, MouseEventArgs e) { if (dragging) { int l = m_control.Left + e.X - startx; int holdL = l; int t = m_control.Top + e.Y - starty; int holdT = t; int w = m_control.Width; int h = m_control.Height;
int RightBoundry = objectContainer.Width + objectContainer.Left; int BottomBoundry = objectContainer.Height + objectContainer.Top; int LeftBoundry = objectContainer.Left; int TopBoundry = objectContainer.Top; l = (l < LeftBoundry) ? LeftBoundry : ((l + w > RightBoundry) ? RightBoundry - w : l); t = (t < TopBoundry) ? TopBoundry : ((t + h > BottomBoundry) ? BottomBoundry - h : t);
m_control.Left = l; m_control.Top = t;
if (holdL != l || holdT != t) return;//we have reached the border so discontinue
foreach (DummySelect ds in DummySelectList) { l = ds.dummyCtl.Left + e.X - startx; t = ds.dummyCtl.Top + e.Y - starty; w = ds.dummyCtl.Width; h = ds.dummyCtl.Height;
RightBoundry = objectContainer.Width + objectContainer.Left; BottomBoundry = objectContainer.Height + objectContainer.Top; LeftBoundry = objectContainer.Left; TopBoundry = objectContainer.Top;
l = (l < LeftBoundry) ? LeftBoundry : ((l + w > RightBoundry) ? RightBoundry - w : l); t = (t < TopBoundry) ? TopBoundry : ((t + h > BottomBoundry) ? BottomBoundry - h : t);
ds.dummyCtl.Left = l; ds.dummyCtl.Top = t; } } }
// Display sizing handles around picked control once dragging has completed private void ctl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); } #endregion } }
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi all.
I have the great PickBox class working in my VB Project but I have one problem.
When expanding my picturebox the box scales but the image doesn't.
I had a similar command then did work before I moved to PickBox that basically did:
picturebox1.SizeMode = PictureBoxSizeMode.StretchImage
but sizemode isn't available as attribute of m_Control.
Can anyone advise at how I can get the image to scale too?
Thanks in advance.
Julian
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Jim,
Thanks for your great code . I try to create a selections for multiple controls when ctrl +click . i am creating instances for the pickbox and i am changing the color of label controls to identify .
But how can i delete the instance again after ctrl +click once again on the selected control .I am not able to identify the instance of the pickbox for new multiple controls . so i am not able to unwire it as possible . Those instances are appearing on the screen which is not exaclty like in Visual Studio 2005 .
I am not identify the instance of pickbox and not able to dispose the instance causing the selection to stay on top when i click the control.
Private Sub ShowNewSelection(ByVal sender As Object) Try Dim pickselect As New pickbox() pickselect .SelectControl(sender, New EventArgs) Catch ex As Exception
End Try End Sub
on ctrl+ click on the control/s.
I am trying to create exactly like in Visual Studio 2005.
I struck here .
Any help ??
modified on Friday, February 20, 2009 7:57 AM
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
This is just what i was looking for.
I just added a few events so that i could show the current size and location of the control that was moved.
Thanks!
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi all. Thank you Jim for your great work 
I'm developing a designer that use your code. But, I had a little issue: I need to select more than one control, to allow resizing and moving in group.
I send your code with my own implementation for that multiple selection (C#, VS 2005). I hope this help to someone (sorry for the long message):
public class PickBox { private const int BOX_SIZE = 8; private Color BOX_COLOR = Color.White; private Label[] lbl = new Label[8]; private int startl; private int startt; private int startw; private int starth; private int startx; private int starty; private bool dragging; private bool moved; private Cursor[] arrArrow = new Cursor[] {Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS, Cursors.SizeNESW, Cursors.SizeWE};
private const int MIN_SIZE = 20;
public PickBox() { for (int i = 0; i < 8; i++) { lbl[i] = new Label(); lbl[i].TabIndex = i; lbl[i].FlatStyle = 0; lbl[i].BorderStyle = BorderStyle.FixedSingle; lbl[i].BackColor = BOX_COLOR; lbl[i].Cursor = arrArrow[i]; lbl[i].Text = ""; lbl[i].BringToFront(); lbl[i].MouseDown += new MouseEventHandler(this.lbl_MouseDown); lbl[i].MouseMove += new MouseEventHandler(this.lbl_MouseMove); lbl[i].MouseUp += new MouseEventHandler(this.lbl_MouseUp); } }
public void WireControl(Control ctl) { ctl.Click += new EventHandler(this.SelectControl); }
private Hashtable SelectedControlCursors = new Hashtable();
private List[Control] _SelectedControls = new List[Control](); public List[Control] SelectedControls { get { return _SelectedControls; } }
public void ClearSelection() { while (SelectedControls.Count > 0) { RemoveControl(SelectedControls[0]); } HideHandles(); }
private bool IsEmptySelection { get { return SelectedControls.Count == 0; } }
private void AddOrRemoveControl(Control c) { if (SelectedControls.Contains(c)) RemoveControl(c); else AddControl(c); }
private void AddControl(Control c) { c.MouseDown += new MouseEventHandler(this.ctl_MouseDown); c.MouseMove += new MouseEventHandler(this.ctl_MouseMove); c.MouseUp += new MouseEventHandler(this.ctl_MouseUp);
c.Click -= new EventHandler(this.SelectControl);
SelectedControlCursors.Add(c, c.Cursor);
c.Cursor = Cursors.SizeAll;
SelectedControls.Add(c); }
private void RemoveControl(Control c) { c.Cursor = (Cursor)SelectedControlCursors[c]; SelectedControlCursors.Remove(c);
c.MouseDown -= new MouseEventHandler(this.ctl_MouseDown); c.MouseMove -= new MouseEventHandler(this.ctl_MouseMove); c.MouseUp -= new MouseEventHandler(this.ctl_MouseUp);
c.Click += new EventHandler(this.SelectControl);
SelectedControls.Remove(c); }
private Rectangle GetSelectionBounds() { if (SelectedControls.Count == 0) { return Rectangle.Empty; }
int l = int.MaxValue; int r = int.MinValue; int t = int.MaxValue; int b = int.MinValue; foreach (Control c in SelectedControls) { l = Math.Min(l, c.Left); r = Math.Max(r, c.Right); t = Math.Min(t, c.Top); b = Math.Max(b, c.Bottom); } return new Rectangle(l, t, r - l, b - t); }
private void SelectControl(object sender, EventArgs e) { Control c = (Control)sender; AddOrRemoveControl(c);
if (IsEmptySelection) { HideHandles(); return; }
Control container = SelectedControls[0].Parent; for (int i = 0; i < 8; i++) { if (lbl[i].Parent == null) container.Controls.Add(lbl[i]); lbl[i].BringToFront(); }
MoveHandles();
ShowHandles(); }
private void ShowHandles() { if (!IsEmptySelection) { for (int i = 0; i < 8; i++) { lbl[i].Visible = true; } } }
private void HideHandles() { for (int i = 0; i < 8; i++) { lbl[i].Visible = false; } }
private void MoveHandles() { Rectangle bounds = GetSelectionBounds();
int sX = bounds.Left - BOX_SIZE; int sY = bounds.Top - BOX_SIZE; int sW = bounds.Width + BOX_SIZE; int sH = bounds.Height + BOX_SIZE; int hB = BOX_SIZE / 2; int[] arrPosX = new int[] {sX+hB, sX + sW / 2, sX + sW-hB, sX + sW-hB, sX + sW-hB, sX + sW / 2, sX+hB, sX+hB}; int[] arrPosY = new int[] {sY+hB, sY+hB, sY+hB, sY + sH / 2, sY + sH-hB, sY + sH-hB, sY + sH-hB, sY + sH / 2}; for (int i = 0; i < 8; i++) lbl[i].SetBounds(arrPosX[i], arrPosY[i], BOX_SIZE, BOX_SIZE); }
private void lbl_MouseDown(object sender, MouseEventArgs e) { dragging = true; Rectangle bounds = GetSelectionBounds(); startl = bounds.Left; startt = bounds.Top; startw = bounds.Width; starth = bounds.Height; HideHandles(); }
private void lbl_MouseMove(object sender, MouseEventArgs e) { Rectangle bounds = GetSelectionBounds(); int l = bounds.Left; int w = bounds.Width; int t = bounds.Top; int h = bounds.Height; if (dragging) { switch (((Label)sender).TabIndex) { case 0: l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; w = startl + startw - bounds.Left; h = startt + starth - bounds.Top; break; case 1: t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - bounds.Top; break; case 2: w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; t = startt + e.Y < startt + starth - MIN_SIZE ? startt + e.Y : startt + starth - MIN_SIZE; h = startt + starth - bounds.Top; break; case 3: w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; break; case 4: w = startw + e.X > MIN_SIZE ? startw + e.X : MIN_SIZE; h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 5: h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 6: l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - bounds.Left; h = starth + e.Y > MIN_SIZE ? starth + e.Y : MIN_SIZE; break; case 7: l = startl + e.X < startl + startw - MIN_SIZE ? startl + e.X : startl + startw - MIN_SIZE; w = startl + startw - bounds.Left; break; } l = (l < 0) ? 0 : l; t = (t < 0) ? 0 : t;
ChangeSelectionBounds(l, t, w, h); } }
private void ChangeSelectionBounds(int l, int t, int w, int h) { Rectangle oldB = GetSelectionBounds();
int deltaX = l - oldB.Left; int deltaY = t - oldB.Top; int deltaW = w - oldB.Width; int deltaH = h - oldB.Height;
foreach(Control c in SelectedControls) { c.SetBounds(c.Left + deltaX, c.Top + deltaY, c.Width + deltaW, c.Height + deltaH); } }
private void lbl_MouseUp(object sender, MouseEventArgs e) { dragging = false; MoveHandles(); ShowHandles(); }
private void ctl_MouseDown(object sender, MouseEventArgs e) { dragging = true; moved = false; startx = e.X; starty = e.Y; HideHandles(); }
private void ctl_MouseMove(object sender, MouseEventArgs e) { if (dragging) { int deltaX = e.X - startx; int deltaY = e.Y - starty;
Rectangle bounds = GetSelectionBounds(); Control parent = SelectedControls[0].Parent;
if (bounds.Left + deltaX < 0) deltaX = bounds.Left * -1; if (bounds.Top + deltaY < 0) deltaY = bounds.Top * -1; if (bounds.Right + deltaX > parent.ClientRectangle.Width) deltaX = parent.ClientRectangle.Width - bounds.Right; if (bounds.Bottom + deltaY > parent.ClientRectangle.Height) deltaY = parent.ClientRectangle.Height - bounds.Bottom;
foreach (Control c in SelectedControls) { c.Location = new Point(c.Left + deltaX, c.Top + deltaY); }
if (deltaX != 0 || deltaY != 0) { moved = true; } } }
private void ctl_MouseUp(object sender, MouseEventArgs e) { if (dragging && !moved) RemoveControl((Control)sender);
dragging = false; moved = false; MoveHandles(); ShowHandles(); } }
Regards,
Diego
modified on Friday, December 26, 2008 10:49 PM
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Diego,
private List[Control] _SelectedControls = new List[Control]();
Invalid token '[' in class, struct, or interface member declaration
I am getting this error in VS2008. Can you help me, because i liked the multiple selection idea.
Regards, Ammar
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Hi Ammar, please change "[" and "]" with "<" and ">". I wrote this way because the browser take my code as HTML when my post is shown. Now I know how to do it better 
In the example:
private List[Control] _SelectedControls = new List[Control]();
after you change it:
private List<Control> _SelectedControls = new List<Control>();
Regards,
Diego
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Thanks for a great piece of software. Would it be possible to change it so that a dragged control can be moved between containers, for example the label could be dropped into the panel or the panel into another panel?
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
thanks for your great work, but the problem is adding a Container control like a panel (level2) into a panel with another controls (level1), and then wiring the children controls of this panel. this way, when you click on a level1 control and then clicking on level2 control, you'll see that you have 2 controls selected.... !! got what i mean ?
ziad
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
 |
hi,
1. how can i put the Mouse_Right Click event for selection of a control while its now selecting on left mouse click i want to use Right mouse click for resize a control..
2. and how to put the control resize option under Tabs , pannel or subcontrols
MFS
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Create a Keydown event under select Control method.
m_control.KeyDown += new KeyEventHandler(m_control_KeyDown);
and add this menthod .
private void m_control_KeyDown(object sender, KeyEventArgs e) { //left = 37,Up= 38,right= 39,down=40; if (e.KeyValue == 37) { int l = m_control.Left - 1; int t = m_control.Top; int w = m_control.Width; int h = m_control.Height; l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ? m_control.Parent.ClientRectangle.Width - w : l); t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ? m_control.Parent.ClientRectangle.Height - h : t); m_control.Left = l; m_control.Top = t; MoveHandles(); } if (e.KeyValue == 38) { int l = m_control.Left; int t = m_control.Top - 1; int w = m_control.Width; int h = m_control.Height; l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ? m_control.Parent.ClientRectangle.Width - w : l); t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ? m_control.Parent.ClientRectangle.Height - h : t); m_control.Left = l; m_control.Top = t; MoveHandles(); } if (e.KeyValue == 39) { int l = m_control.Left + 1; int t = m_control.Top; int w = m_control.Width; int h = m_control.Height; l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ? m_control.Parent.ClientRectangle.Width - w : l); t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ? m_control.Parent.ClientRectangle.Height - h : t); m_control.Left = l; m_control.Top = t; MoveHandles(); } if (e.KeyValue == 40) { int l = m_control.Left; int t = m_control.Top + 1; int w = m_control.Width; int h = m_control.Height; l = (l < 0) ? 0 : ((l + w > m_control.Parent.ClientRectangle.Width) ? m_control.Parent.ClientRectangle.Width - w : l); t = (t < 0) ? 0 : ((t + h > m_control.Parent.ClientRectangle.Height) ? m_control.Parent.ClientRectangle.Height - h : t); m_control.Left = l; m_control.Top = t; MoveHandles(); } }
This should work fine.
I have used 1 px as a movement of the control when keyboard arrows are moved.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
when i created a label at runtime through button click .....although they are also controls but cannot b selected........can anybody help me
|
| Sign In·View Thread·PermaLink | 2.33/5 (3 votes) |
|
|
|
 |
|
|
 |
|
 |
how about impleminting a rotating of the control ? can it be done and how ?
Imagination is more important than knowledge... {Albert Einstein}
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This is exactly what i need too..... i need to place the sizing handles at different rotation angles and add an extra sizing handle to rotate the control....if u hav any code for that then pls do help.
Thank you
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
i do have some , but i cant give it out since its in commercial product
Imagination is more important than knowledge... {Albert Einstein}
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
This project does not run in visual studio 2005, it says it has problems during the conversion process, any help on this issue?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi,
I've been experimenting with this to try and get it to support custom made UserControls, but to no avail.
Are there any pointers you can give me?
|
| Sign In·View Thread·PermaLink | 1.67/5 (3 votes) |
|
|
|
 |
|
 |
Hi,
The sample code does not work for child controls inside panels etc. Therefore, I have added the following recursive function which maps all controls in a form:
public void WireControls(Control _Control) { if (!(_Control is Form)) WireControl(_Control); foreach (Control c in _Control.Controls) { WireControls(c); } }
Ronit
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Anyone know the VC++ 2005 equivalent of Ronit's code?
I've managed to convert Jim Korovessis excellent Simple Runtime Control Sizing and Dragging Class over to VC++ but it doesn't work (builds and runs fine but doesn't show the sizing grab handles). It took me about half a day to complete the conversion.
I think it's because my controls are inside another control. So, I wanted to add Ronit's code to see if that's the case. But VC++ is complaining error C2275 when I changed:
if (!(_Control is Form))
to
if (_Control != Form)
It said 'System::Windows::Forms::Form' is illegal use of this type as an expression. Does anyone know? Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I don't take credit for all of this translation. The reasonably good conversion tool by Kamal Patel[^] did most of it, but I did the rest (and tested). Enjoy...
If anyone else has any additional tweaks to share, such as multi-select for block moving, deleting controls, etc, that would be lovely. I'm just now getting started with a new project and this code was EXACTLY what I was looking for! Thank you very much for sharing your expertise!
Public Class PickBox Private Const BOX_SIZE As Integer = 8 Private BOX_COLOR As Color = Color.White Private m_container As ContainerControl Private m_control As Control Private lbl(8) As Label Private startl As Integer Private startt As Integer Private startw As Integer Private starth As Integer Private startx As Integer Private starty As Integer Private dragging As Boolean Private arrArrow() As Cursor = New Cursor() {Cursors.SizeNWSE, Cursors.SizeNS, _ Cursors.SizeNESW, Cursors.SizeWE, Cursors.SizeNWSE, Cursors.SizeNS, _ Cursors.SizeNESW, Cursors.SizeWE}
Private oldCursor As Cursor
Private Const MIN_SIZE As Integer = 20
Public Sub New() Dim i As Integer For i = 0 To 8 - 1 Step i + 1 lbl(i) = New Label lbl(i).TabIndex = i lbl(i).FlatStyle = 0 lbl(i).BorderStyle = BorderStyle.FixedSingle lbl(i).BackColor = BOX_COLOR lbl(i).Cursor = arrArrow(i) lbl(i).Text = "" lbl(i).BringToFront() AddHandler lbl(i).MouseDown, AddressOf Me.lbl_MouseDown AddHandler lbl(i).MouseMove, AddressOf Me.lbl_MouseMove AddHandler lbl(i).MouseUp, AddressOf Me.lbl_MouseUp Next End Sub
Public Sub WireControl(ByVal ctl As Control) AddHandler ctl.Click, AddressOf SelectControl End Sub
Private Sub SelectControl(ByVal sender As Object, ByVal e As EventArgs)
If TypeOf m_control Is Control Then m_control.Cursor = oldCursor
RemoveHandler m_control.MouseDown, AddressOf Me.ctl_MouseDown RemoveHandler m_control.MouseMove, AddressOf Me.ctl_MouseMove RemoveHandler m_control.MouseUp, AddressOf Me.ctl_MouseUp
m_control = Nothing End If
m_control = CType(sender, Control) AddHandler m_control.MouseDown, AddressOf Me.ctl_MouseDown AddHandler m_control.MouseMove, AddressOf Me.ctl_MouseMove AddHandler m_control.MouseUp, AddressOf Me.ctl_MouseUp AddHandler m_control.Parent.Click, AddressOf Me.Parent_Click
Dim i As Integer For i = 0 To 8 - 1 Step i + 1 m_control.Parent.Controls.Add(lbl(i)) lbl(i).BringToFront() Next
MoveHandles()
ShowHandles()
oldCursor = m_control.Cursor m_control.Cursor = Cursors.SizeAll
End Sub
Public Sub Remove() HideHandles() m_control.Cursor = oldCursor End Sub
Private Sub ShowHandles() If Not m_control Is Nothing Then Dim i As Integer For i = 0 To 8 - 1 Step i + 1 lbl(i).Visible = True Next End If End Sub
Private Sub HideHandles() Dim i As Integer For i = 0 To 8 - 1 Step i + 1 lbl(i).Visible = False Next End Sub
Private Sub MoveHandles() Dim sX As Integer = m_control.Left - BOX_SIZE Dim sY As Integer = m_control.Top - BOX_SIZE Dim sW As Integer = m_control.Width + BOX_SIZE Dim sH As Integer = m_control.Height + BOX_SIZE Dim hB As Integer = BOX_SIZE / 2 Dim arrPosX = New Integer() {sX + hB, sX + sW / 2, sX + sW - hB, _ sX + sW - hB, sX + sW - hB, sX + sW / 2, sX + hB, sX + hB}
Dim arrPosY = New Integer() {sY + hB, sY + hB, sY + hB, sY + sH / 2, _ sY + sH - hB, sY + sH - hB, sY + sH - hB, sY + sH / 2}
Dim i As Integer For i = 0 To 8 - 1 Step i + 1 lbl(i).SetBounds(arrPosX(i), arrPosY(i), BOX_SIZE, BOX_SIZE) Next End Sub
Private Sub lbl_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) dragging = True startl = m_control.Left startt = m_control.Top startw = m_control.Width starth = m_control.Height HideHandles() End Sub
Private Sub lbl_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Dim l As Integer = m_control.Left Dim w As Integer = m_control.Width Dim t As Integer = m_control.Top Dim h As Integer = m_control.Height If dragging Then Select Case (CType(sender, Label)).TabIndex Case 0 If startl + e.X < startl + startw - MIN_SIZE Then l = startl + e.X Else l = startl + startw - MIN_SIZE End If If startt + e.Y < startt + starth - MIN_SIZE Then t = startt + e.Y Else t = startt + starth - MIN_SIZE End If
w = startl + startw - m_control.Left h = startt + starth - m_control.Top Exit Select Case 1 If startt + e.Y < startt + starth - MIN_SIZE Then t = startt + e.Y Else t = startt + starth - MIN_SIZE End If h = startt + starth - m_control.Top Exit Select Case 2 If startw + e.X > MIN_SIZE Then w = startw + e.X Else w = MIN_SIZE End If If startt + e.Y < startt + starth - MIN_SIZE Then t = startt + e.Y Else t = startt + starth - MIN_SIZE End If h = startt + starth - m_control.Top Exit Select Case 3 If startw + e.X > MIN_SIZE Then w = startw + e.X Else w = MIN_SIZE End If Exit Select Case 4 If startw + e.X > MIN_SIZE Then w = startw + e.X Else w = MIN_SIZE End If If starth + e.Y > MIN_SIZE Then h = starth + e.Y Else h = MIN_SIZE End If Exit Select Case 5 If starth + e.Y > MIN_SIZE Then h = starth + e.Y Else h = MIN_SIZE End If Exit Select Case 6 If startl + e.X < startl + startw - MIN_SIZE Then l = startl + e.X Else l = startl + startw - MIN_SIZE End If w = startl + startw - m_control.Left If starth + e.Y > MIN_SIZE Then h = starth + e.Y Else h = MIN_SIZE End If Exit Select Case 7 If startl + e.X < startl + startw - MIN_SIZE Then l = startl + e.X Else l = startl + startw - MIN_SIZE End If w = startl + startw - m_control.Left Exit Select End Select If l < 0 Then l = 0 Else l = l End If If t < 0 Then t = 0 Else t = t End If m_control.SetBounds(l, t, w, h) End If End Sub
Private Sub lbl_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) dragging = False MoveHandles() ShowHandles() End Sub
Private Sub ctl_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) dragging = True startx = e.X starty = e.Y HideHandles() End Sub
Private Sub ctl_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) If dragging Then Dim l As Integer = m_control.Left + e.X - startx Dim t As Integer = m_control.Top + e.Y - starty Dim w As Integer = m_control.Width Dim h As Integer = m_control.Height If l < 0 Then l = 0 Else If l + w > m_control.Parent.ClientRectangle.Width Then l = m_control.Parent.ClientRectangle.Width - w Else l = l End If End If If t < 0 Then t = 0 Else If t + h > m_control.Parent.ClientRectangle.Height Then t = m_control.Parent.ClientRectangle.Height - h Else t = t End If End If
m_control.Left = l m_control.Top = t End If End Sub
Private Sub ctl_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) dragging = False MoveHandles() ShowHandles() End Sub
Private Sub Parent_Click(ByVal sender As Object, ByVal e As EventArgs) Remove() End Sub End Class
Mark
|
| Sign In·View Thread·PermaLink | 5.00/5 (2 votes) |
|
|
|
 |
|
 |
Nice, I was also looking for this one, how bout saving layout, how would you go about doing that? would you use xml?
para saaan naman ito
|
| Sign In·View Thread·PermaLink | 3.80/5 (5 votes) |
|
|
|
 |
|
 |
One feature that you could add would be the ability to double click on controls and change their text property.
For example, double click on a label (or GroupBox) and be able to type in the Text property directly in the form.
Whenever you have the time, of course Very nice piece of code that you have!
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|