private void TrayIcon_Click(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { trayIcon.GetType().InvokeMember("ShowContextMenu", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, trayIcon, null); } }
public SysTrayApp() { // create vhdpath directory, if it doesnt exist if (!Directory.Exists(Application.StartupPath + @"\" + Options.vhdlocalpath)) { Directory.CreateDirectory(Application.StartupPath + @"\" + Options.vhdlocalpath); } // Create a tray icon. In this example we use a // standard system icon for simplicity, but you // can of course use your own custom icon too. trayIcon = new NotifyIcon(); trayIcon.Text = "vhdgamer"; trayIcon.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); // Add menu to tray icon and show it. trayMenu = new ContextMenu(); trayMenu.Popup += delegate { updateContextMenu(); }; trayIcon.ContextMenu = trayMenu; trayIcon.MouseClick += new MouseEventHandler(trayIcon_Click); trayIcon.Visible = true; updateContextMenu(); // info for user (if click on tooltop -> show menu) trayIcon.ShowBalloonTip(1000, "vhdgamer", "Click here to start or download games...", ToolTipIcon.Info); trayIcon.BalloonTipClicked += delegate { trayIcon.GetType().InvokeMember("ShowContextMenu", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod, null, trayIcon, null); }; }
/// <summary> /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows 7+.) /// </summary> /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param> /// <returns>The location of the specified NotifyIcon. Null if the location could not be found.</returns> public static Rect?GetNotifyIconRectWindows7(NotifyIcon notifyicon) { if (Compatibility.CurrentWindowsVersion != Compatibility.WindowsVersion.Windows7Plus) { throw new PlatformNotSupportedException("This method can only be used under Windows 7 or later. Please use GetNotifyIconRectangleLegacy() if you use an earlier operating system."); } // get notify icon id FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance); int iconid = (int)idFieldInfo.GetValue(notifyicon); // get notify icon hwnd IntPtr iconhandle; try { FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance); System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon); iconhandle = nativeWindow.Handle; if (iconhandle == null || iconhandle == IntPtr.Zero) { return(null); } } catch { return(null); } NativeMethods.RECT rect = new NativeMethods.RECT(); NativeMethods.NOTIFYICONIDENTIFIER nid = new NativeMethods.NOTIFYICONIDENTIFIER() { hWnd = iconhandle, uID = (uint)iconid }; nid.cbSize = (uint)Marshal.SizeOf(nid); int result = NativeMethods.Shell_NotifyIconGetRect(ref nid, out rect); // 0 means success, 1 means the notify icon is in the fly-out - either is fine if (result != 0 && result != 1) { return(null); } // convert to System.Rect and return return(rect); }
public static Rectangle GetNotifyIconPosition(NotifyIcon icon) { // obtain private field "id" // ReSharper disable once PossibleNullReferenceException var id = (uint)icon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(icon); // ReSharper disable once PossibleNullReferenceException var hwnd = (NativeWindow)icon.GetType() .GetField("window", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(icon); Native.NOTIFYICONIDENTIFIER identifier = new Native.NOTIFYICONIDENTIFIER() { cbSize = Marshal.SizeOf(typeof(Native.NOTIFYICONIDENTIFIER)), hWnd = hwnd.Handle, uID = (int)id }; Native.Shell_NotifyIconGetRect(ref identifier, out var rect); return(Rectangle.FromLTRB(rect.Left, rect.Top, rect.Right, rect.Bottom)); }
private IntPtr _GetHandler(NotifyIcon icon) { var fieldInfo = icon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); var nativeWindow = (NativeWindow)fieldInfo.GetValue(icon); return(nativeWindow.Handle == IntPtr.Zero ? IntPtr.Zero : nativeWindow.Handle); }
private void notifier_Click(object sender, EventArgs e) { NotifyIcon eventSource = null; Type nHandle = null; eventSource = (NotifyIcon)sender; nHandle = eventSource.GetType(); nHandle.InvokeMember("ShowContextMenu", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, eventSource, null); }
// also show menu on left click private void trayIcon_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //cmsTrayIcon.Show(); // causes empty window on taskbar? // call private ShowContextMenu() method via reflection trayIcon.GetType().InvokeMember("ShowContextMenu", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, trayIcon, null); } }
public static RECT GetNotifyIconLocation(NotifyIcon notifyIcon) { FieldInfo idFieldInfo = notifyIcon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance); int iconid = (int)idFieldInfo.GetValue(notifyIcon); FieldInfo windowFieldInfo = notifyIcon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance); NativeWindow nativeWindow = (NativeWindow)windowFieldInfo.GetValue(notifyIcon); IntPtr iconhandle = nativeWindow.Handle; RECT rect = new RECT(); NOTIFYICONIDENTIFIER nid = new NOTIFYICONIDENTIFIER() { hWnd = iconhandle, uID = (uint)iconid }; nid.cbSize = (uint)Marshal.SizeOf(nid); int result = Shell_NotifyIconGetRect(ref nid, out rect); return(rect); }
private void RemoveClickEvent(NotifyIcon b) { FieldInfo f1 = typeof(Control).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic); object obj = f1.GetValue(b); PropertyInfo pi = b.GetType().GetProperty("Events", BindingFlags.NonPublic | BindingFlags.Instance); EventHandlerList list = (EventHandlerList)pi.GetValue(b, null); list.RemoveHandler(obj, list[obj]); }
public static Rect?GetNotifyIconRectangle(NotifyIcon notifyicon) { // get notify icon id FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance); int iconid = (int)idFieldInfo.GetValue(notifyicon); // get notify icon hwnd FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance); System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon); IntPtr iconhandle = nativeWindow.Handle; if (iconhandle == IntPtr.Zero) { return(null); } RECT rect = new RECT(); NOTIFYICONIDENTIFIER nid = new NOTIFYICONIDENTIFIER() { hWnd = iconhandle, uID = (uint)iconid }; nid.cbSize = (uint)Marshal.SizeOf(nid); int result = Shell_NotifyIconGetRect(ref nid, out rect); // 0 means success, 1 means the notify icon is in the fly-out - either is fine if (result != 0 && result != 1) { return(null); } // convert to System.Rect and return return(rect); }
private void notifyBalloon(string title, string text, Int32 flags) { // Based on information found at http://www.thecodeproject.com/csharp/notifyballoon.asp // Has no effect on Windows 98 (and probably other) systems, even though it should. PInvoke.NotifyIconData uNIF = new PInvoke.NotifyIconData(); uNIF.hwnd = ((NativeWindow)trayIcon.GetType().GetField("window", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(trayIcon)).Handle; uNIF.uID = (int)trayIcon.GetType().GetField("id", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(trayIcon); uNIF.dwStateMask = 0; uNIF.hIcon = IntPtr.Zero; uNIF.szTip = ""; uNIF.uCallbackMessage = IntPtr.Zero; uNIF.dwState = PInvoke.NIS_SHAREDICON; uNIF.uTimeout = 11200; uNIF.dwInfoFlags = flags; uNIF.szInfoTitle = title; uNIF.szInfo = text; uNIF.uFlags = PInvoke.NIF_INFO; uNIF.cbSize = Marshal.SizeOf(uNIF); try { PInvoke.Shell_NotifyIconA(PInvoke.NIM_MODIFY, ref uNIF); } catch { } }
public void UpdateText(string text) { if (text.Length >= 128) { text = text.Substring(0, 124) + "..."; } var t = notifyIcon.GetType(); var hidden = BindingFlags.NonPublic | BindingFlags.Instance; var textField = t.GetField("text", hidden); textField.SetValue(notifyIcon, text); if ((bool)t.GetField("added", hidden).GetValue(notifyIcon)) { t.GetMethod("UpdateIcon", hidden).Invoke(notifyIcon, new object[] { true }); } }
private IntPtr GetWindowHandle(NotifyIcon notifyIcon) { if (notifyIcon == null) { return(IntPtr.Zero); } Type type = notifyIcon.GetType(); BindingFlags bf = BindingFlags.Instance | BindingFlags.NonPublic; FieldInfo fiWindow = type.GetField("window", bf); object objWindow = fiWindow.GetValue(notifyIcon); type = objWindow.GetType().BaseType; FieldInfo fiHandle = type.GetField("handle", bf); IntPtr handle = (IntPtr)fiHandle.GetValue(objWindow); return(handle); }
public TrayIcon() { notifyIcon = new NotifyIcon(); iconMenu = new ContextMenuStrip(); menuItemShowApp = new ToolStripMenuItem(string.Format("Open {0}", Constants.APP_NAME)); menuItemShowApp.Click += (s, e) => windowService.Value.OpenMainWindow(); menuItemExit = new ToolStripMenuItem("Exit"); iconMenu.Items.Add(menuItemShowApp); iconMenu.Items.Add(menuItemExit); notifyIcon.ContextMenuStrip = iconMenu; notifyIcon.Icon = Properties.Resources.icon1; notifyIcon.Text = Constants.APP_NAME; notifyIcon.Visible = true; notifyIcon.Click += (s, e) => { notifyIcon.GetType().InvokeMember("ShowContextMenu", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, null, notifyIcon, null); }; notifyIcon.DoubleClick += (s, e) => windowService.Value.OpenMainWindow(); menuItemExit.Click += (s, e) => ((App)App.Current).ShutdownApp(); IsVisible = true; }
/// <summary> /// Returns a rectangle representing the location of the specified NotifyIcon. (Windows Vista and earlier.) /// </summary> /// <param name="notifyicon">The NotifyIcon whose location should be returned.</param> /// <returns>The location of the specified NotifyIcon.</returns> public static Rectangle?GetNotifyIconRectLegacy(NotifyIcon notifyicon) { Rectangle?nirect = null; FieldInfo idFieldInfo = notifyicon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance); int niid = (int)idFieldInfo.GetValue(notifyicon); FieldInfo windowFieldInfo = notifyicon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance); System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(notifyicon); IntPtr nihandle = nativeWindow.Handle; if (nihandle == null || nihandle == IntPtr.Zero) { return(null); } // find the handle of the task bar IntPtr taskbarparenthandle = NativeMethods.FindWindow("Shell_TrayWnd", null); if (taskbarparenthandle == (IntPtr)null) { return(null); } // find the handle of the notification area IntPtr naparenthandle = NativeMethods.FindWindowEx(taskbarparenthandle, IntPtr.Zero, "TrayNotifyWnd", null); if (naparenthandle == (IntPtr)null) { return(null); } // make a list of toolbars in the notification area (one of them should contain the icon) List <IntPtr> natoolbarwindows = NativeMethods.GetChildToolbarWindows(naparenthandle); bool found = false; for (int i = 0; !found && i < natoolbarwindows.Count; i++) { IntPtr natoolbarhandle = natoolbarwindows[i]; // retrieve the number of toolbar buttons (i.e. notify icons) int buttoncount = NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_BUTTONCOUNT, IntPtr.Zero, IntPtr.Zero).ToInt32(); // get notification area's process id uint naprocessid; NativeMethods.GetWindowThreadProcessId(natoolbarhandle, out naprocessid); // get handle to notification area's process IntPtr naprocesshandle = NativeMethods.OpenProcess(NativeMethods.ProcessAccessFlags.All, false, naprocessid); if (naprocesshandle == IntPtr.Zero) { return(null); } // allocate enough memory within the notification area's process to store the button info we want IntPtr toolbarmemoryptr = NativeMethods.VirtualAllocEx(naprocesshandle, (IntPtr)null, (uint)Marshal.SizeOf(typeof(NativeMethods.TBBUTTON)), NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite); if (toolbarmemoryptr == IntPtr.Zero) { return(null); } try { // loop through the toolbar's buttons until we find our notify icon for (int j = 0; !found && j < buttoncount; j++) { int bytesread = -1; // ask the notification area to give us information about the current button NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETBUTTON, new IntPtr(j), toolbarmemoryptr); // retrieve that information from the notification area's process NativeMethods.TBBUTTON buttoninfo = new NativeMethods.TBBUTTON(); NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out buttoninfo, Marshal.SizeOf(buttoninfo), out bytesread); if (bytesread != Marshal.SizeOf(buttoninfo)) { return(null); } if (buttoninfo.dwData == IntPtr.Zero) { return(null); } // the dwData field contains a pointer to information about the notify icon: // the handle of the notify icon (an 4/8 bytes) and the id of the notify icon (4 bytes) IntPtr niinfopointer = buttoninfo.dwData; // read the notify icon handle IntPtr nihandlenew; NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer, out nihandlenew, Marshal.SizeOf(typeof(IntPtr)), out bytesread); if (bytesread != Marshal.SizeOf(typeof(IntPtr))) { return(null); } // read the notify icon id uint niidnew; NativeMethods.ReadProcessMemory(naprocesshandle, niinfopointer + Marshal.SizeOf(typeof(IntPtr)), out niidnew, Marshal.SizeOf(typeof(uint)), out bytesread); if (bytesread != Marshal.SizeOf(typeof(uint))) { return(null); } // if we've found a match if (nihandlenew == nihandle && niidnew == niid) { // check if the button is hidden: if it is, return the rectangle of the 'show hidden icons' button if ((byte)(buttoninfo.fsState & NativeMethods.TBSTATE_HIDDEN) != 0) { nirect = GetNotifyAreaButtonRectangle(); } else { NativeMethods.RECT result = new NativeMethods.RECT(); // get the relative rectangle of the toolbar button (notify icon) NativeMethods.SendMessage(natoolbarhandle, NativeMethods.TB_GETITEMRECT, new IntPtr(j), toolbarmemoryptr); NativeMethods.ReadProcessMemory(naprocesshandle, toolbarmemoryptr, out result, Marshal.SizeOf(result), out bytesread); if (bytesread != Marshal.SizeOf(result)) { return(null); } // find where the rectangle lies in relation to the screen NativeMethods.MapWindowPoints(natoolbarhandle, (IntPtr)null, ref result, 2); nirect = result; } found = true; } } } finally { // free memory within process NativeMethods.VirtualFreeEx(naprocesshandle, toolbarmemoryptr, 0, NativeMethods.FreeType.Release); // close handle to process NativeMethods.CloseHandle(naprocesshandle); } } return(nirect); }