<summary>/// program Tray area icon Location//// </summary> public class Iconinfo {// <summary >///Tray handle//// </summary> public IntPtr hWnd = IntPtr.Zero; <summary>///x coordinates/// </summary> public int x; <summary>/// icons y coordinates/// </summary> public int y; <summary>///Icon Width/// </summary> public int width; <summary>///Icon Height/// </summary> public int height; public bool Iniconrect (point P) { if (p.x >= x && p.x <= x + Width && p.y >= Y &&am P P.Y <= Y + Height) { return true; } return false; } }
Added to the icon's MouseMove event to determine the icon position information
Geticoninfo (); <pre name= "code" class= "CSharp" > //Get icon position information private void Geticoninfo () { point p = cursor.position; Myicon.hwnd = Windowfrompoint (p); int btnsize = SendMessage (Myicon.hwnd, Tb_getbuttonsize, IntPtr.Zero, IntPtr.Zero); Myicon.width = btnsize & 0xFFFF; Myicon.height = (btnsize/0x10000) & 0xFFFF; Rect rect = new rect (); if (GetWindowRect (Myicon.hwnd, out rect)) { if (myicon.width! = 0 && Myicon.height! = 0) { myicon.x = (p.x-rect. LEFT-PY)/myicon.width * myicon.width + rect. Left + py; MYICON.Y = (p.y-rect. TOP-PY)/myicon.height * myicon.height + rect. Top + py;}}}
Variables and functions used
<summary>////different operating system offsets///</summary> private int py = 0; Private Const int wm_user = 0x400; Private Const int tb_getbuttonsize = Wm_user + 58; Private Const int wm_syscommand = 0x112; Private Const long sc_minimize = 0xf020; Private Const int wm_mousemove = 0x0200; [DllImport ("User32.dll")] public static extern int SendMessage (INTPTR hwnd, int wmsg, IntPtr wParam, IntPtr LParam) ; [DllImport ("User32.dll")] private static extern bool GetWindowRect (INTPTR hwnd, out Rect lpRect); [DllImport ("User32.dll")] private static extern IntPtr Windowfrompoint (point P); <summary>///Icon Information///</summary> private Iconinfo Myicon = new Iconinfo (); After the WIN7 system has a two-pixel offset program load, add the following code operatingsystem OS = environment.osversion; if (OS. Platform = = platformid.win32nt && os. Version.major >= 6) { PY = 2; }
C # Determine if the mouse is on the NotifyIcon of its own program