我有一个控件,它以网格的形式在其内部移动很多不同的控件。所以基本上,任何时候都没有显示容器控件。但我确实捕获了内部控件的所有mousedown和mousemove事件。喜欢做的事情就是创建一个类似于Windows计算机桌面的效果,你可以拖动它,鼠标拖动内的每个控件都会被抛出一个列表,当鼠标被释放时我可以执行对所有选定项目的操作。这可能吗?
答案 0 :(得分:0)
注册OnMouseDown,OnMouseMove,OnMouseUp事件。
结帐example。
答案 1 :(得分:0)
通过一些试验和错误,我解决了我自己的问题。 在我的每个孩子的控制中,我这样做了:
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));
}
使光标位置可以从容器控件中使用,并在容器中我这样做:
#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
显然,其他地方还有其他一些事情发生,但这只是它的基本内容。感谢我得到的一个答案,尽管我已经知道了,我得到了我得到的每一个答案:)谢谢你们!