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