如果在进入控件时 MouseButton 按下,C# 如何触发 MouseMove 或 MouseEnter 事件?
C# how to trigger MouseMove or MouseEnter events if MouseButton is down when entering a control?
我需要在鼠标左键按下时触发 MouseMove 或 MouseEnter 事件。不幸的是,当鼠标左键按下时,这些事件不会被触发。
我已经尝试过此线程中的解决方案:
C# triggering MouseEnter even even if mouse button is held down
但它不起作用,因为它只对窗体起作用,对控件不起作用。
这个想法很简单,假设我有 10 个标签并排放置。
在我在一个标签上按下鼠标后 --> 它的背景颜色变为红色,然后我按住 MouseLeftButton 并继续将鼠标移动到其他标签,我希望这些标签的背景色也变为红色,而无需松开鼠标任何一点。
我尝试使用一个布尔变量来存储鼠标按钮的状态,在 MouseDown 事件中 --> MouseDown = true;
然后在 MouseEnter 或 MouseMove 处使用该布尔值,但根本不会触发这些事件。
谢谢大家
必须在MouseDown事件中使用Label.Capture = false;
表示此事件允许其他事件运行。
完整代码我放上
public partial class Form1 : Form
{
bool Ispressed = false;
public Form1()
{
InitializeComponent();
for(int i=1; i<=10;i++)
{
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = "Label " + i;
lbl.Location = new System.Drawing.Point(150, i * 40);
lbl.MouseDown += new MouseEventHandler(Lbl_MouseDown);
lbl.MouseUp += new MouseEventHandler(Lbl_MouseUp);
lbl.MouseMove += new MouseEventHandler(Lbl_MouseMove);
lbl.MouseLeave += new System.EventHandler(Lbl_MouseLeave);
this.Controls.Add(lbl);
}
}
private void Lbl_MouseMove(object sender, MouseEventArgs e)
{
if (Ispressed)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Red;
}
}
private void Lbl_MouseLeave(object sender, EventArgs e)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Transparent;
}
private void Lbl_MouseDown(object sender, MouseEventArgs e)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Red;
lbl.Capture = false;
Ispressed = true;
}
private void Lbl_MouseUp(object sender, MouseEventArgs e)
{
Ispressed = false;
foreach(Control ctr in this.Controls)
{
if(ctr is Label)
{
ctr.BackColor = Color.Transparent;
}
}
}
}
我需要在鼠标左键按下时触发 MouseMove 或 MouseEnter 事件。不幸的是,当鼠标左键按下时,这些事件不会被触发。
我已经尝试过此线程中的解决方案: C# triggering MouseEnter even even if mouse button is held down
但它不起作用,因为它只对窗体起作用,对控件不起作用。 这个想法很简单,假设我有 10 个标签并排放置。 在我在一个标签上按下鼠标后 --> 它的背景颜色变为红色,然后我按住 MouseLeftButton 并继续将鼠标移动到其他标签,我希望这些标签的背景色也变为红色,而无需松开鼠标任何一点。
我尝试使用一个布尔变量来存储鼠标按钮的状态,在 MouseDown 事件中 --> MouseDown = true; 然后在 MouseEnter 或 MouseMove 处使用该布尔值,但根本不会触发这些事件。 谢谢大家
必须在MouseDown事件中使用Label.Capture = false;
表示此事件允许其他事件运行。
完整代码我放上
public partial class Form1 : Form
{
bool Ispressed = false;
public Form1()
{
InitializeComponent();
for(int i=1; i<=10;i++)
{
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = "Label " + i;
lbl.Location = new System.Drawing.Point(150, i * 40);
lbl.MouseDown += new MouseEventHandler(Lbl_MouseDown);
lbl.MouseUp += new MouseEventHandler(Lbl_MouseUp);
lbl.MouseMove += new MouseEventHandler(Lbl_MouseMove);
lbl.MouseLeave += new System.EventHandler(Lbl_MouseLeave);
this.Controls.Add(lbl);
}
}
private void Lbl_MouseMove(object sender, MouseEventArgs e)
{
if (Ispressed)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Red;
}
}
private void Lbl_MouseLeave(object sender, EventArgs e)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Transparent;
}
private void Lbl_MouseDown(object sender, MouseEventArgs e)
{
Label lbl = sender as Label;
lbl.BackColor = Color.Red;
lbl.Capture = false;
Ispressed = true;
}
private void Lbl_MouseUp(object sender, MouseEventArgs e)
{
Ispressed = false;
foreach(Control ctr in this.Controls)
{
if(ctr is Label)
{
ctr.BackColor = Color.Transparent;
}
}
}
}