I have a control, that moves around alot of different controls inside of it in the form of a grid. So basically, none of the container control is shown at any point. But i do have capture of all the mousedown and mousemove events of the controls inside. What id like to do, is create an effect alot like the desktop of a windows computer, where you can drag, and every control that is inside of the mouse's drag will be thrown in a list, and when the mouse is released i can perform an action on all the selected items. Is this possible?
A:
Register for OnMouseDown, OnMouseMove, OnMouseUp events.
- OnMouseDown get the control's position and focus underneath the mouse.
- OnMouseMove, set the position of the control with mouse position.
- OnMouseUp release the control focus.
Checkout the example.
KMan
2010-04-10 10:58:55
Thanks, KMan, but I already know about all of those.
Tommy
2010-04-10 23:04:27
@Tommy: Sorry if it didnt help. I was after the question ` Is this possible?`, just so it may help you get started.
KMan
2010-04-11 09:55:17
A:
Well with some trial and error i solved my own question. in each of my childs controls, i did this:
protected override void OnMouseDown(MouseEventArgs e)
{
Point pnt = this.PointToScreen(e.Location);
base.OnMouseDown(new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta));
}
protected override void OnMouseMove(MouseEventArgs e)
{
Point pnt = this.PointToScreen(e.Location);
base.OnMouseMove(new MouseEventArgs(e.Button, e.Clicks, pnt.X, pnt.Y, e.Delta));
}
to make the cursor position usable from the container control, and in the container i did this:
#region Lambda
item.MouseDown += new MouseEventHandler((a, b) =>
{
MessageBase hovering = this.GetChildAtPoint(tr_MouseCurrentPoint) as MessageBase;
if (b.Button == MouseButtons.Left)
{
tr_MouseDown = true;
tr_MouseDownPoint = this.PointToClient(b.Location);
mouseupdate();
foreach (var t in tr_CurrentlySelected)
{
t.Key.Selected = false;
}
tr_CurrentlySelected.Clear();
if (hovering != null && tr_CurrentlySelected.ContainsKey(hovering) == false)
{
tr_CurrentlySelected.Add(hovering, new MouseCoordinateStore(tr_MouseDown, tr_MouseDownPoint, tr_MouseCurrentPoint, tr_MouseDifference));
hovering.Selected = true;
tr_LastHoveredOver = hovering;
}
}
else if (b.Button == MouseButtons.Right)
{
if (hovering.Selected == true)
{
MessageBox.Show("Show Right Click Logic Here");
}
else
{
foreach (var t in tr_CurrentlySelected)
{
t.Key.Selected = false;
}
tr_CurrentlySelected.Clear();
if (hovering != null && tr_CurrentlySelected.ContainsKey(hovering) == false)
{
tr_CurrentlySelected.Add(hovering, new MouseCoordinateStore(tr_MouseDown, tr_MouseDownPoint, tr_MouseCurrentPoint, tr_MouseDifference));
hovering.Selected = true;
tr_LastHoveredOver = hovering;
}
MessageBox.Show("Show Right Click Logic Here");
}
}
});
item.MouseUp += new MouseEventHandler((a, b) => { tr_MouseDown = false; mouseupdate(); });
item.MouseMove += new MouseEventHandler((a, b) => {
tr_MouseCurrentPoint = this.PointToClient(b.Location);
mouseupdate();
if (tr_MouseDown)
{
MessageBase hovering = this.GetChildAtPoint(tr_MouseCurrentPoint) as MessageBase;
if (hovering != null)
{
if (tr_CurrentlySelected.ContainsKey(hovering) == false)
{
tr_CurrentlySelected.Add(hovering, new MouseCoordinateStore(tr_MouseDown, tr_MouseDownPoint, tr_MouseCurrentPoint, tr_MouseDifference));
tr_LastHoveredOver = hovering;
hovering.Selected = true;
}
else
{
int ind = hovering.Index;
List<MessageBase> ItemsToRemove = new List<MessageBase>();
if (tr_MouseDifference.Y < 0) // Y is negative
{
foreach (var dic in tr_CurrentlySelected)
{
if (dic.Key.Index < ind)
{
ItemsToRemove.Add(dic.Key);
}
}
}
else //Y is positive
{
foreach (var dic in tr_CurrentlySelected)
{
if (dic.Key.Index > ind)
{
ItemsToRemove.Add(dic.Key);
}
}
}
foreach (var dic in ItemsToRemove)
{
tr_CurrentlySelected.Remove(dic);
dic.Selected = false;
}
}
}
}
});
item.KeyDown += new KeyEventHandler((a, b) =>
{
try
{
if (b.KeyData == Keys.Down)
{
MessageBase hovering = ControlCollection[tr_LastHoveredOver.Index + 1];
foreach (var t in tr_CurrentlySelected)
{
t.Key.Selected = false;
}
tr_CurrentlySelected.Clear();
if (hovering != null && tr_CurrentlySelected.ContainsKey(hovering) == false)
{
tr_CurrentlySelected.Add(hovering, null);
hovering.Selected = true;
tr_LastHoveredOver = hovering;
}
}
else if (b.KeyData == Keys.Up)
{
MessageBase hovering = ControlCollection[tr_LastHoveredOver.Index - 1];
foreach (var t in tr_CurrentlySelected)
{
t.Key.Selected = false;
}
tr_CurrentlySelected.Clear();
if (hovering != null && tr_CurrentlySelected.ContainsKey(hovering) == false)
{
tr_CurrentlySelected.Add(hovering, null);
hovering.Selected = true;
tr_LastHoveredOver = hovering;
}
}
}
catch { }
});
#endregion
obviously there is some other things happening elsewhere, but thats the basic just of it. Thanks for the one answer i got, allthough i already knew about that i appriciate every and all answers i get :) thanks guys!
Tommy
2010-04-10 22:50:10