private void Init() { // Does the owner have an icon set? if (_owner.Icon == null) { throw new InvalidOperationException("SuperNotifyIcon: Dropping cannot be initialised without an icon set!"); } // When the mouse is close MouseHook.MouseMove += MouseHook_MouseMove; // Cancel the drop refreshing below if we do an actual click on the NotifyIcon _owner.NotifyIcon.MouseUp += (sender, e) => { mouseWasInNotifyArea = false; }; // Refresh the drop position if we click in the notification area on Windows 7; we might've moved an icon! if (SysInfo.IsWindows7OrLater) { MouseHook.MouseDown += (sender, e) => { mouseWasInNotifyArea = MouseInNotifyArea(); // Shall we cancel, then? if (e.Button != MouseButtons.Left) { mouseWasInNotifyArea = false; } }; MouseHook.MouseUp += (sender, e) => { if (MouseInNotifyArea() && mouseWasInNotifyArea) { // We should wait for the icon to settle in before doing anything Timer wait = new Timer(); wait.Tick += (sender2, e2) => { if (mouseWasInNotifyArea) ShowDrop(); mouseWasInNotifyArea = false; wait.Dispose(); }; wait.Interval = 200; wait.Start(); } }; } // Refresh the drop position if the size of the notification area changes Size notifyAreaLastSize = NotifyArea.GetRectangle().Size; Timer notifyAreaTimer = new Timer(); notifyAreaTimer.Tick += (sender, e) => { if (NotifyArea.GetRectangle().Size != notifyAreaLastSize) { notifyAreaLastSize = NotifyArea.GetRectangle().Size; ShowDrop(); } }; notifyAreaTimer.Interval = 1000; notifyAreaTimer.Start(); // Is the drop even in the right place at all? int unsuccessfulRefreshes = 0; Timer dropPlaceTimer = new Timer(); dropPlaceTimer.Tick += (sender, e) => { if (!NotifyArea.GetRectangle().Contains(new Point(this.Location.X + 2, this.Location.Y + 2))) { ShowDrop(); unsuccessfulRefreshes++; // Don't keep refreshing every second if we can't find our icon! if (unsuccessfulRefreshes >= 3) dropPlaceTimer.Interval = unsuccessfulRefreshes * 1000; } else { unsuccessfulRefreshes = 0; dropPlaceTimer.Interval = 1000; } }; dropPlaceTimer.Interval = 1000; dropPlaceTimer.Start(); // Okay... still no success? Let's fall back to the mouse timer... //// TODO: See whether this should only be run on WinXP/Vista systems and whether this should //// run even if we have a valid drop position MouseHoldTimed mouseHold = new MouseHoldTimed(500); mouseHold.MouseDown += (sender, e) => { if (e.Button != MouseButtons.Left || OwnApplicationActive() || !lastNotifyIconPoint.HasValue) mouseHold.Cancel(); }; mouseHold.MouseHoldTimeout += (sender, e) => { if (lastNotifyIconPoint.HasValue) ShowDrop(); }; }
private void Init() { // Does the owner have an icon set? if (_owner.Icon == null) { throw new InvalidOperationException("SuperNotifyIcon: Dropping cannot be initialised without an icon set!"); } // When the mouse is close MouseHook.MouseMove += MouseHook_MouseMove; // Cancel the drop refreshing below if we do an actual click on the NotifyIcon _owner.NotifyIcon.MouseUp += (sender, e) => { mouseWasInNotifyArea = false; }; // Refresh the drop position if we click in the notification area on Windows 7; we might've moved an icon! if (SysInfo.IsWindows7OrLater) { MouseHook.MouseDown += (sender, e) => { mouseWasInNotifyArea = MouseInNotifyArea(); // Shall we cancel, then? if (e.Button != MouseButtons.Left) { mouseWasInNotifyArea = false; } }; MouseHook.MouseUp += (sender, e) => { if (MouseInNotifyArea() && mouseWasInNotifyArea) { // We should wait for the icon to settle in before doing anything Timer wait = new Timer(); wait.Tick += (sender2, e2) => { if (mouseWasInNotifyArea) { ShowDrop(); } mouseWasInNotifyArea = false; wait.Dispose(); }; wait.Interval = 200; wait.Start(); } }; } // Refresh the drop position if the size of the notification area changes Size notifyAreaLastSize = NotifyArea.GetRectangle().Size; Timer notifyAreaTimer = new Timer(); notifyAreaTimer.Tick += (sender, e) => { if (NotifyArea.GetRectangle().Size != notifyAreaLastSize) { notifyAreaLastSize = NotifyArea.GetRectangle().Size; ShowDrop(); } }; notifyAreaTimer.Interval = 1000; notifyAreaTimer.Start(); // Is the drop even in the right place at all? int unsuccessfulRefreshes = 0; Timer dropPlaceTimer = new Timer(); dropPlaceTimer.Tick += (sender, e) => { if (!NotifyArea.GetRectangle().Contains(new Point(this.Location.X + 2, this.Location.Y + 2))) { ShowDrop(); unsuccessfulRefreshes++; // Don't keep refreshing every second if we can't find our icon! if (unsuccessfulRefreshes >= 3) { dropPlaceTimer.Interval = unsuccessfulRefreshes * 1000; } } else { unsuccessfulRefreshes = 0; dropPlaceTimer.Interval = 1000; } }; dropPlaceTimer.Interval = 1000; dropPlaceTimer.Start(); // Okay... still no success? Let's fall back to the mouse timer... //// TODO: See whether this should only be run on WinXP/Vista systems and whether this should //// run even if we have a valid drop position MouseHoldTimed mouseHold = new MouseHoldTimed(500); mouseHold.MouseDown += (sender, e) => { if (e.Button != MouseButtons.Left || OwnApplicationActive() || !lastNotifyIconPoint.HasValue) { mouseHold.Cancel(); } }; mouseHold.MouseHoldTimeout += (sender, e) => { if (lastNotifyIconPoint.HasValue) { ShowDrop(); } }; }