C# Winform - 保持 MouseDown 并加入其他对象,对象 MouseMove 不起作用
C# Winform - Keep MouseDown and join to other object the object MouseMove not work
当对象A上的MouseDown然后mousemove到objectB时,objectB(mousemove handle)无法调用。
如何让其他对象检测到mousemovehandle,当mousedown进入对象区域之前。
public Test()
{
InitializeComponent();
this.Size = new Size(500, 500);
Panel pl = new Panel();
pl.Size = new Size(200, 200);
pl.Location = new Point(0, 0);
pl.BackColor = Color.Pink;
Label lb = new Label();
lb.Text = "Keep MouseDown and move to Panel2 , Panel2 MouseMoveHandler not work";
lb.Dock = DockStyle.Fill;
pl.Controls.Add(lb);
TextBox tb = new TextBox();
tb.Multiline = true;
tb.Size = new Size(400, 100);
tb.Location = new Point(0,300);
Panel pl2 = new Panel();
pl2.Size = new Size(100, 100);
pl2.Location = new Point(0, 0);
pl2.BackColor = Color.Red;
pl2.Location = new Point(300, 0);
pl2.MouseMove += new MouseEventHandler(delegate (object o, MouseEventArgs a)
{
tb.AppendText(a.X + "," + a.Y);
});
this.Controls.Add(pl);
this.Controls.Add(pl2);
this.Controls.Add(tb);
}
您看到的是标准行为。如果您将鼠标移到该对象之外,这是在按住鼠标的同时获取该对象的 MouseMove
事件的唯一方法。
如果您想在任何时间点找出光标下的控件(如果需要,您可以在 MouseMove
事件中进行),您可以使用此代码:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pnt);
public static Control GetControlUnderCursor() {
var handle = WindowFromPoint(Control.MousePosition);
if (handle != IntPtr.Zero)
return Control.FromHandle(handle);
return null;
}
如果您将表单的 Capture
property to true
(that's what is happening on your control when you hold the mouse button down) also and you can check Control.MouseButtons
设置为在需要时找出按下了哪些鼠标按钮,则可以在表单的 MouseMove
事件中使用此代码..
当对象A上的MouseDown然后mousemove到objectB时,objectB(mousemove handle)无法调用。
如何让其他对象检测到mousemovehandle,当mousedown进入对象区域之前。
public Test()
{
InitializeComponent();
this.Size = new Size(500, 500);
Panel pl = new Panel();
pl.Size = new Size(200, 200);
pl.Location = new Point(0, 0);
pl.BackColor = Color.Pink;
Label lb = new Label();
lb.Text = "Keep MouseDown and move to Panel2 , Panel2 MouseMoveHandler not work";
lb.Dock = DockStyle.Fill;
pl.Controls.Add(lb);
TextBox tb = new TextBox();
tb.Multiline = true;
tb.Size = new Size(400, 100);
tb.Location = new Point(0,300);
Panel pl2 = new Panel();
pl2.Size = new Size(100, 100);
pl2.Location = new Point(0, 0);
pl2.BackColor = Color.Red;
pl2.Location = new Point(300, 0);
pl2.MouseMove += new MouseEventHandler(delegate (object o, MouseEventArgs a)
{
tb.AppendText(a.X + "," + a.Y);
});
this.Controls.Add(pl);
this.Controls.Add(pl2);
this.Controls.Add(tb);
}
您看到的是标准行为。如果您将鼠标移到该对象之外,这是在按住鼠标的同时获取该对象的 MouseMove
事件的唯一方法。
如果您想在任何时间点找出光标下的控件(如果需要,您可以在 MouseMove
事件中进行),您可以使用此代码:
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(Point pnt);
public static Control GetControlUnderCursor() {
var handle = WindowFromPoint(Control.MousePosition);
if (handle != IntPtr.Zero)
return Control.FromHandle(handle);
return null;
}
如果您将表单的 Capture
property to true
(that's what is happening on your control when you hold the mouse button down) also and you can check Control.MouseButtons
设置为在需要时找出按下了哪些鼠标按钮,则可以在表单的 MouseMove
事件中使用此代码..