public SysTrayApp() { // create vhdpath directory, if it doesnt exist String localVhdBasePath = Path.Combine(Application.StartupPath, Options.vhdlocalpath); if (!Directory.Exists(localVhdBasePath)) { Directory.CreateDirectory(localVhdBasePath); } gamesLibrary = new GamesLibrary(new VhdStorage(new DirectoryInfo(localVhdBasePath))); lobby = new Lobby(); lobby.Listen(); /****************************************************************************************** * Announce Own Games every 10s *****************************************************************************************/ this.announcingTimer = new Timer(); this.announcingTimer.Interval = Options.ANNOUNCING_INTERVAL; this.announcingTimer.Tick += delegate(object o, EventArgs e) { lobby.Send(new AnnouncementMessage(new UserInfo(Options.nickname), gamesLibrary.GetGameNames())); }; this.announcingTimer.Start(); /****************************************************************************************** * GUI Init *****************************************************************************************/ 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; }
/// <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 Rect? GetNotifyIconRectLegacy(NotifyIcon notifyicon) { Rect? 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; }
internal VulcanNotifyIcon(StatusForm statusForm, StatusMonitor statusMonitor, BrowserIntegration browserIntegration) { this.statusForm = statusForm; this.browserIntegration = browserIntegration; this.activeIcons = Blank; this.currentIconIndex = 0; this.timer = new Timer(AnimationInterval); this.timer.Elapsed += new ElapsedEventHandler(this.onTimerElapsed); this.timer.Start(); MenuItem[] items = new MenuItem[] { new MenuItem("Restore", new EventHandler(onMenuRestore)), new MenuItem("Go to Dashboard", new EventHandler(onMenuOpenDashboard)), new MenuItem("-"), new MenuItem("Exit", new EventHandler(onMenuExit)) }; items[0].DefaultItem = true; notifyIcon = new NotifyIcon(); notifyIcon.ContextMenu = new ContextMenu(items); notifyIcon.Icon = Icons.Blank; notifyIcon.Text = "Vulcan Tray Notifier"; notifyIcon.Visible = true; notifyIcon.DoubleClick += new EventHandler(this.onDoubleClick); statusMonitor.DataLoadError += new StatusMonitor.DataLoadErrorHandler(this.onLoadError); statusMonitor.NewBuildAvailable += new StatusMonitor.NewBuildHandler(this.onNewBuild); statusMonitor.DashboardStatusChanged += new StatusMonitor.DashboardStatusChangedHandler(this.onDashboardStatusChanged); // .NET 1.1 compat EventInfo balloonTipClickedEvent = notifyIcon.GetType().GetEvent("BalloonTipClicked"); if (balloonTipClickedEvent != null) { balloonTipClickedEvent.AddEventHandler(notifyIcon, new EventHandler(this.onBalloonTipClicked)); ballonsSupported = true; } else { ballonsSupported = false; } }
public static Rectangle GetNotificationIconRectangle(NotifyIcon Icon) { FieldInfo idFieldInfo = Icon.GetType().GetField("id", BindingFlags.NonPublic | BindingFlags.Instance); int iconid = (int)idFieldInfo.GetValue(Icon); FieldInfo windowFieldInfo = Icon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Instance); System.Windows.Forms.NativeWindow nativeWindow = (System.Windows.Forms.NativeWindow)windowFieldInfo.GetValue(Icon); 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 = Shell32.Shell_NotifyIconGetRect(ref nid, out rect); Rectangle notifyiconrectangle = Rectangle.FromLTRB(rect.left, rect.top, rect.right, rect.bottom); return notifyiconrectangle; }
/// <summary> /// Get the handle of the native window associated with the NotifyIcon. This is done /// by reflecting in to a private member field of the icon. /// </summary> /// <param name="icon"></param> /// <returns></returns> private IntPtr GetNativeHandle(NotifyIcon icon) { FieldInfo field = icon.GetType().GetField("window", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance); NativeWindow window = field.GetValue(icon) as NativeWindow; return (window == null ? IntPtr.Zero : window.Handle); }