简体   繁体   English

MouseHover在鼠标关闭时不会触发

[英]MouseHover not firing when mouse is down

I'm working on a WordSearch puzzle program (also called WordFind) where you have to find certain words in a mass of letters. 我正在开发一个WordSearch拼图程序(也称为WordFind),你必须在其中找到大量字母中的某些单词。 I'm using C# WinForms. 我正在使用C#WinForms。

My problem is when I want to click and hold 1 letter( Label ), then drag over to other letters to change their ForeColor . 我的问题是当我想点击并按住1个字母( Label ),然后拖动到其他字母以更改其ForeColor I've tried googling but to no avail. 我试过谷歌搜索但无济于事。

Here is what I have: 这是我有的:

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;
     };
}

However, the MouseHover event never fires unless the mouse is not being held down. 但是,除非未按住鼠标,否则MouseHover事件永远不会触发。 Also no luck swapping MouseHover with MouseEnter . 也没有运气与MouseEnter交换MouseHover So, I kept the MouseDown and MouseUp events and tried using MouseHover within the form itself: 所以,我保留了MouseDownMouseUp事件,并尝试在表单中使用MouseHover:

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. 任何建议表示赞赏。

You can use Drag and Drop events. 您可以使用拖放事件。

  1. Set AllowDrop property for each control that is target of drop. 为每个作为drop目标的控件设置AllowDrop属性。
  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. 处理拖动从它开始的每个控件的MouseDown事件,并在处理程序中调用该控件的DoDragDrop事件并设置要拖动的数据。
  3. Handle DragEnetr event of each target of drag and set e.Effect to determine if drop is allowed or not. 处理每个拖动目标的DragEnetr事件并设置e.Effect以确定是否允许拖放。 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. 处理DragLeave以重置背景颜色。
  5. Hanlde DragDrop and use GetData method if e.Data to get the data and perform the actions when drop. Hanlde DragDrop并使用GetData方法,如果e.Data获取数据并在删除时执行操作。

walking through 走过

Example

I have 3 buttons, button1 and button2 and button3 and button2 is target of drop. 我有3个按钮,button1和button2,button3和button2是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. 在下面的代码中,我将检查按钮2上的文本是否是button1的文本,我将按钮2的背面颜色更改为绿色,否则更改为红色。 also if you take dragging mouse out of button2, I'll set the back color to default. 如果你将鼠标拖出button2,我也会将背景颜色设置为默认值。 If you drop, I'll change the text of button2 and will set the text of button1: 如果你放弃,我将改变button2的文本并设置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;
}

You're looking for the various drag events: 您正在寻找各种拖动事件:

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragover(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/system.windows.forms.control.dragenter(v=vs.110).aspx https://msdn.microsoft.com/en-us/library/ system.windows.forms.control.dragover(v = vs.110)的.aspx

etc... 等等...

The problem that you are running into is that you are using the wrong events for what you are trying to accomplish. 您遇到的问题是您正在使用错误的事件来完成您要完成的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM