|
Hi,
first of all ur code is great million thanks for the same
i am facing little issue it would be great of u if u help
whenever i delete selected control its 8 labels in background stays
Please suggest how to i delete those labels with controls.
NOTE: 1) i need to delete those labels not hide
2) all the controls i am using are dynamically created
|
|
|
|
|
Hi,
this is great just what i needed, but i was working with the user control and it seems picbox doesn't work with it. is there any other options for me to make it work with usercontrols
thanks you
|
|
|
|
|
Thanks for the show of appreciation.
I'm afraid that I've been away from coding for a while. Recently loaded up VS12 trial to attempt to answer your question, but the project doesn't seem to be importable into the new version of VS. I unfortunately don't have much time available to devote to my hobbies .
I hope that someone keener than me that has taken this sample to the next level can answer your question. Sorry for the lack of assistance.
Jim
|
|
|
|
|
please upload the source project, the demo project having the project but having some errors.
|
|
|
|
|
We are using your code in a GUI designer for a windows based game engine that we are making. It works very well and saved me a ton of time so thanks!
-JC
|
|
|
|
|
Hi loving the resize and move but is there anyway to constrain proportions
Many thanks
Rob
|
|
|
|
|
I am creating panel as form. Depending on particular panel i am adding controls to that panel .
like that i am adding some no of controls on panel .
How to create multiple selection ,Moving of controls at runtime like in Visual studio design time environment .,cut,copy ,paste of those controls under selection . to another panel or with in panel .???
like that i am having shape from PPacks
For all type of controls how to get multiple selection ,drag n drop , move
|
|
|
|
|
Thank Very Much My Dear . this Class Very Good For me.
|
|
|
|
|
I added additional logic to be able to avoid collision and overlap. A bit of rectangle intersect logic in lbl and ctl MouseMove events. I had to also capture if the user action was to reduce the size or increase. works grt. thanks for your article.
|
|
|
|
|
Hello,
can you please post your changes?
thanks
steffen
|
|
|
|
|
Dear Jim,
Is there any license required to use this patent in a commercial package ?
Thank you.
Thai
|
|
|
|
|
Thai,
Please feel free to use all or any part of the code in any way that you wish. I did it as a hobbyist as a stand-alone example and not part of a software project, just to get a feel for NET technology. I hope it will be of help to you.
Good luck,
Jim
|
|
|
|
|
Too good! This is exactly what I was looking for! Plus the implementation looks well written.
|
|
|
|
|
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<dummyselect> DummySelectList = new List<dummyselect>();
// 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
}
}
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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!
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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?
|
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
how do i implement this class using keyboard to move controls..any idea guys???thanks...
|
|
|
|
|
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.
|
|
|
|
|