Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#:如何使用计时器模拟鼠标悬停事件_C#_Mouseevent - Fatal编程技术网

C#:如何使用计时器模拟鼠标悬停事件

C#:如何使用计时器模拟鼠标悬停事件,c#,mouseevent,C#,Mouseevent,我有一个fom,它有一个停靠的用户控件来填充 此用户控件显示不同的图像。每个图像都有Id,我有一个imageId与imageDetail对象字典的列表 此用户控件的鼠标移动事件被捕获,我正在工具提示中显示鼠标的当前X和Y位置 当用户将鼠标停留在图像上一段时间时,我还想在工具提示中显示图像细节 我试图用鼠标悬停事件来实现这一点,但它只有在鼠标进入用户控件绑定时才会出现。在此之后,如果我在用户控件内移动鼠标,则不会触发鼠标悬停事件 如何在工具提示中沿图像细节显示当前X、Y位置 是否有任何方法可以使用

我有一个fom,它有一个停靠的用户控件来填充

此用户控件显示不同的图像。每个图像都有Id,我有一个imageId与imageDetail对象字典的列表

此用户控件的鼠标移动事件被捕获,我正在工具提示中显示鼠标的当前X和Y位置

当用户将鼠标停留在图像上一段时间时,我还想在工具提示中显示图像细节

我试图用鼠标悬停事件来实现这一点,但它只有在鼠标进入用户控件绑定时才会出现。在此之后,如果我在用户控件内移动鼠标,则不会触发鼠标悬停事件

如何在工具提示中沿图像细节显示当前X、Y位置

是否有任何方法可以使用计时器在鼠标移动中模拟鼠标悬停事件

是否有任何示例代码

我用计算机解决了这个问题

public partial class Form1 : Form
    {
        Timer timer;
        bool moveStart;
        int count = 0;
        Point prev;

        public Form1()
        {
            InitializeComponent();
            timer = new Timer();
            timer.Interval = 1000;
            timer.Tick += new EventHandler(timer_Tick);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            this.timer.Stop();
            this.moveStart = false;            
            this.toolTip1.SetToolTip(this, string.Format("Mouse Hover"));
            this.textBox1.Text = (++count).ToString();            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.prev.X == e.X && this.prev.Y == e.Y)
                return;
            if (moveStart)
            {
                this.prev = new Point(e.X, e.Y);
                this.timer.Stop();
                this.toolTip1.SetToolTip(this, string.Format("Mouse Move\nX : {0}\nY : {1}", e.X, e.Y));
                this.timer.Start();
            }
            else
            {
                moveStart = true;
            }
        }
    }

最简单的方法是从MouseMove子例程调用MouseOver子例程,如下所示:

void MouseMove(object sender, MouseEventArgs e)
{
    //Call the MouseHover event
    MouseHover(sender, e);
}

void MouseHover(object sender, EventArgs e)
{
    //MouseHover event code
}
但是,如果希望对何时以及如何显示工具提示进行更多控制,则需要执行以下类似操作:

  • 在类级别声明侦听变量
  • 钩住MouseHover事件,以便在鼠标进入时打开侦听变量
  • 钩住MouseLeave事件,以便在鼠标离开时关闭侦听变量
  • 将工具提示代码放入MouseMove处理程序,以便在侦听变量启用时显示工具提示
  • 这里有一些代码来演示我上面概述的内容

    class Form1
    {
        bool showPopup = false;
    
        void MouseHover(object sender, EventArgs e)
        {
            showPopup = true;
        }
    
        void MouseLeave(object sender, EventArgs e)
        {
            showPopup = false;
            toolTip.Hide(this);
        }
    
        void MouseMove(object sender, MouseEventArgs e)
        {
            if (showPopup) 
            {
                toolTip.Show("X: " + e.Location.X + "\r\nY: " + e.Location.Y, 
                             this, e.Location)
            }
        }
    }
    

    当然,您必须添加一个名为ToolTip
    工具提示
    ,并将各种方法(子例程)与控件的相应事件(表单、PictureBox等)关联起来。

    我无法解决此问题。请问还有别的解决办法吗