Reputation: 1118
You know that the NotifyIcon does not have MouseLeave event. So, is there any way to know if mouse has left the NotifyIcon?
Edit: Actually, I want to show a message when the mouse hovers the NotifyIcon and I want to show another message when the mouse leaves the NotifyIcon.
Upvotes: 0
Views: 776
Reputation: 751
The following method is using a timer and the timer will work only when the mouse is over the notify icon and then will stop after the mouse leaves it so don't worry about the performance.
Interval
property to 50 for fast responding.MouseMove
event of the Notify Icon make the value of the point variable equals to the current mouse position and start the timer if it is not enabled.Tick
event of the timer check if the current mouse position equals to the point variable then write the code you want to execute when the mouse leaves the notify icon and then stop the timer.VB.NET Code
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# Code
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();
}
}
}
}
Upvotes: 2
Reputation: 1314
Create a Timer object. In the timers tick event, get the current mouse position and check if it falls outside the tray icon area and take suitable action
Upvotes: 0
Reputation: 1893
It is hard to get the position of the NotifyIcon on screen (link1, link2), but if you have a ContextMenu of the icon, it might be easier to implement for that.
You can get the current position with the GetCursorPos
method of user32.dll...
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetCursorPos(out System.Drawing.Point lpPoint);
...like this:
System.Drawing.Point p;
if (GetCursorPos(out p)) {
// do something with p
}
Here is a piece of code I used for ContextMenu (it is WPF, not WinForms), I know this is not for NotifyIcon, but it might help:
System.Drawing.Point cursorP;
var trayP = menu.PointFromScreen(new Point(0, 0));
if (GetCursorPos(out cursorP)) {
if (cursorP.X >= trayP.X && cursorP.X <= trayP.X + menu.RenderSize.Width && cursorP.Y >= trayP.Y && cursorP.Y <= trayP.Y + menu.RenderSize.Height) {
// the mouse is over menu
}
}
Upvotes: -1