使用标签设置"mouseDown"布尔值



我试图建立一个系统来检查鼠标是否在winforms应用程序中被点击,并最终这样做

private void lblChips_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void lblChips_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}

8时间(每个标签)。

有人知道是否有更有效的方法来做这件事吗?我试着用

private void frmGame4_MouseDown(object sender, MouseEventArgs e)      
{
mouseDown = true;
}
private void frmGame4_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}

但这不起作用。如有任何帮助,我将不胜感激。

this

public Form1()
{
InitializeComponent();
CollectFormControls(this);
ControlList.Add(this);
MergeEvents();
}
List<Control> ControlList = new List<Control>();
private void CollectFormControls(Control c)
{
foreach (Control control in c.Controls)
{
ControlList.Add(control);
CheckSubControls(control);
}
}
private void CheckSubControls(Control control)
{
if (control.Controls.Count > -1)
{
CollectFormControls(control);
}
}
private void MergeEvents()
{
for (int i = 0; i < ControlList.Count; i++)
{
ControlList[i].MouseDown += All_MouseDown;
ControlList[i].MouseUp += All_MouseUp;
}
}
public void All_MouseUp(object sender, MouseEventArgs e)
{
this.Text = "up";
}
public void All_MouseDown(object sender, MouseEventArgs e)
{
this.Text = "down";
}

可以创建两个方法,一个用于鼠标上下,如下所示:

private void labels_mouseDown(object sender, MouseEventArgs e) 
{
mouseDown = true;
}
private void labels_mouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}

然后,将所有标签链接到这些方法。你也可以从Code:

label.mouseDown += labels_mouseDown;
label.mouseUp += labels_mouseUp;

这必须在InitializeComponent()下的某处完成;方法。你仍然需要对每个标签都这样做,但最终会减少代码。

相关内容

  • 没有找到相关文章

最新更新