面板上的鼠标移动事件 - c# winform
Mouse move event on panel - c# winform
我有一个包含按钮的面板。我想在鼠标悬停时移动此面板。我试过如下。
但是当鼠标放在按钮上方时,面板不会移动。仅当鼠标在面板上时才有效。
我必须让它在面板的任何一点上工作,而不管其中的控件。
this.panelLeft.Controls.Add(this.button1);
this.panelLeft.Location = new System.Drawing.Point(21, 171);
this.panelLeft.Name = "panelLeft";
this.panelLeft.Size = new System.Drawing.Size(662, 324);
this.panelLeft.TabIndex = 15;
this.panelLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseDown);
this.panelLeft.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseMove);
private void panelLeft_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void panelLeft_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
panelLeft.Left = e.X + panelLeft.Left - MouseDownLocation.X;
panelLeft.Top = e.Y + panelLeft.Top - MouseDownLocation.Y;
}
}
因为只为面板添加了事件处理程序
将相同的代码添加到按钮
Btn.MouseDown += panelLeft_MouseDown;
或者遍历面板内的每个控件并为它们分配事件处理程序
我有一个包含按钮的面板。我想在鼠标悬停时移动此面板。我试过如下。 但是当鼠标放在按钮上方时,面板不会移动。仅当鼠标在面板上时才有效。 我必须让它在面板的任何一点上工作,而不管其中的控件。
this.panelLeft.Controls.Add(this.button1);
this.panelLeft.Location = new System.Drawing.Point(21, 171);
this.panelLeft.Name = "panelLeft";
this.panelLeft.Size = new System.Drawing.Size(662, 324);
this.panelLeft.TabIndex = 15;
this.panelLeft.MouseDown += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseDown);
this.panelLeft.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelLeft_MouseMove);
private void panelLeft_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
MouseDownLocation = e.Location;
}
}
private void panelLeft_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
panelLeft.Left = e.X + panelLeft.Left - MouseDownLocation.X;
panelLeft.Top = e.Y + panelLeft.Top - MouseDownLocation.Y;
}
}
因为只为面板添加了事件处理程序 将相同的代码添加到按钮
Btn.MouseDown += panelLeft_MouseDown;
或者遍历面板内的每个控件并为它们分配事件处理程序