您知道NotifyIcon没有MouseLeave事件。那么,有没有办法知道鼠标是否已离开NotifyIcon?
编辑: 实际上,我想在鼠标悬停NotifyIcon时显示一条消息,我想在鼠标离开NotifyIcon时显示另一条消息。
答案 0 :(得分:0)
创建一个Timer对象。在计时器刻度事件中,获取当前鼠标位置并检查它是否落在托盘图标区域之外并采取适当的操作
答案 1 :(得分:0)
以下方法使用计时器,该计时器仅在鼠标悬停在通知图标上时才起作用,然后在鼠标离开通知图标时停止,因此不必担心性能。
Interval
属性设置为50以快速响应。MouseMove
事件内部,使point变量的值等于当前鼠标位置,并在未启用计时器的情况下启动计时器。Tick
事件内,检查当前鼠标位置是否等于point变量,然后在鼠标离开通知图标时编写要执行的代码,然后停止计时器。VB.NET代码
Public Class Form1
Dim p As Point = Nothing
Private Sub NotifyIcon1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles NotifyIcon1.MouseMove
p = Cursor.Position
If Not Timer1.Enabled Then
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Timer1.Tick
If Not Cursor.Position = p Then
'the mouse now left the notify icon
'write the code you want to execute when mouse leave the notify icon
Timer1.Stop()
End If
End Sub
End Class
C#代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
private Point p;
public Form1()
{
InitializeComponent();
}
private void NotifyIcon1_MouseMove(object sender, MouseEventArgs e)
{
p = Cursor.Position;
if(!timer1.Enabled)
{
timer1.Start();
}
}
private void Timer1_Tick(object sender, EventArgs e)
{
if(Cursor.Position != p)
{
//The mouse now left the notify icon
//Write the code you want to execute when mouse leave the notify icon
timer1.Stop();
}
}
}
}