鼠标按下时鼠标悬停不触发 [英] MouseHover not firing when mouse is down

查看:41
本文介绍了鼠标按下时鼠标悬停不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 WordSearch 拼图程序(也称为 WordFind),您必须在其中找到大量字母中的某些单词.我正在使用 C# WinForms.

I'm working on a WordSearch puzzle program (also called WordFind) where you have to find certain words in a mass of letters. I'm using C# WinForms.

我的问题是当我想点击并按住 1 个字母 (Label),然后拖到其他字母上以更改它们的 ForeColor.我试过谷歌搜索但无济于事.

My problem is when I want to click and hold 1 letter(Label), then drag over to other letters to change their ForeColor. I've tried googling but to no avail.

这是我所拥有的:

foreach (Letter a in game.GetLetters())
{
     this.Controls.Add(a);
     a.MouseDown += (s, e2) =>
     {
         isDown = true;
         a.ForeColor = Color.Yellow;
     };
     a.MouseUp += (s, e2) =>
     {
         isDown = false;
     };
     a.MouseHover += (s, e2) =>
     {
         if (isDown)
             a.ForeColor = Color.Yellow;
     };
}

但是,除非鼠标没有被按住,否则 MouseHover 事件永远不会触发.也没有运气将 MouseHoverMouseEnter 交换.因此,我保留了 MouseDownMouseUp 事件并尝试在表单本身中使用 MouseHover:

However, the MouseHover event never fires unless the mouse is not being held down. Also no luck swapping MouseHover with MouseEnter. So, I kept the MouseDown and MouseUp events and tried using MouseHover within the form itself:

private void frmMain_MouseHover(object sender, MouseEventArgs e)
{
    if (isDown)
    {
        foreach (Letter l in game.GetLetters())
           if (l.ClientRectangle.Contains(l.PointToClient(Control.MousePosition)))
               l.ForeColor = Color.Purple;
    }
}

此事件也不会触发,我不知道为什么它没有触发以及一些替代解决方案是什么.任何建议表示赞赏.

This event does not fire either and I'm at a loss as to why it's not firing and what some alternative solutions are. Any advice is appreciated.

推荐答案

您可以使用拖放事件.

  1. 设置 AllowDrop 属性用于作为放置目标的每个控件.
  2. 为每个拖动的控件处理 MouseDown 事件,并在处理程序调用 DoDragDrop 该控件的事件并设置要拖动的数据.立>
  3. 处理DragEnetr 拖拽设置每个目标的事件 e.Effect 以确定是否允许放置.这是您可以检查是否允许掉落的地方,将背景颜色更改为您想要的颜色.
  4. 处理DragLeave 重置背景颜色.
  5. Hanlde DragDrop 并使用 GetData 方法如果 e.Data 获取数据并在删除时执行操作.
  1. Set AllowDrop property for each control that is target of drop.
  2. Handle MouseDown event for each control that drag starts with it and in the handler call DoDragDrop event of that control and set the data that you want to drag.
  3. Handle DragEnetr event of each target of drag and set e.Effect to determine if drop is allowed or not. Here is the place that you can check if drop is allowed, change the back color to your desired color.
  4. Handle DragLeave to reset the back color.
  5. Hanlde DragDrop and use GetData method if e.Data to get the data and perform the actions when drop.

走过

示例

我有 3 个按钮,button1 和 button2,button3 和 button2 是放置的目标.在下面的代码中,我将检查按钮 2 上的文本是否是按钮 1 的文本,我将按钮 2 的背景颜色更改为绿色,否则更改为红色.此外,如果您将鼠标从 button2 中拖出,我会将背景颜色设置为默认值.如果你放下,我会改变button2的文本并设置button1的文本:

I have 3 buttons, button1 and button2 and button3 and button2 is target of drop. In the below code, I'll check if the text that will drop on button 2, is the text of button1, I'll change the back color of button 2 to green, else to red. also if you take dragging mouse out of button2, I'll set the back color to default. If you drop, I'll change the text of button2 and will set the text of button1:

//Start drag for button 2
private void button1_MouseDown(object sender, MouseEventArgs e)
{
    this.button1.DoDragDrop(this.button1.Text, DragDropEffects.Copy);
}

//Start drag for button 3
private void button3_MouseDown(object sender, MouseEventArgs e)
{
    this.button3.DoDragDrop(this.button3.Text, DragDropEffects.Copy);
}

//Check if drop is allowed and change back color
private void button2_DragEnter(object sender, DragEventArgs e)
{
    if(e.Data.GetData(DataFormats.Text).ToString()== button1.Text)
    {
        e.Effect = DragDropEffects.Copy;
        this.button2.BackColor = Color.Green;
    }
    else
    {
        e.Effect = DragDropEffects.None;
        this.button2.BackColor = Color.Red;
    }
}

//Perform drop actions
private void button2_DragDrop(object sender, DragEventArgs e)
{
    this.button2.Text = e.Data.GetData(DataFormats.Text).ToString();
}

//Reset back color here
private void button2_DragLeave(object sender, EventArgs e)
{
    this.button2.BackColor = SystemColors.Control;
}

这篇关于鼠标按下时鼠标悬停不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆