#300, avoid hide the same window to tray multiple times

This commit is contained in:
Kang Yu 2024-03-01 12:11:55 -08:00
parent e2c14237d8
commit 8b08fb3d07

View file

@ -1,17 +1,18 @@
 
using System; using System;
using System.Collections.Generic;
using System.Windows.Forms; using System.Windows.Forms;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Timers; using System.Timers;
using PersistentWindows.Common.WinApiBridge; using PersistentWindows.Common.WinApiBridge;
namespace PersistentWindows.Common.Minimize2Tray namespace PersistentWindows.Common.Minimize2Tray
{ {
public class MinimizeToTray : IDisposable public class MinimizeToTray : IDisposable
{ {
private static HashSet<IntPtr> _trayWindows = new HashSet<IntPtr>();
private NotifyIcon _systemTrayIcon = null; private NotifyIcon _systemTrayIcon = null;
private IntPtr _hwnd; private IntPtr _hwnd;
private string _window_txt; private string _window_txt;
@ -19,12 +20,16 @@ namespace PersistentWindows.Common.Minimize2Tray
static public void Create(IntPtr hwnd) static public void Create(IntPtr hwnd)
{ {
if (_trayWindows.Contains(hwnd))
return;
// clear ctrl state // clear ctrl state
User32.GetAsyncKeyState(0x11); User32.GetAsyncKeyState(0x11);
bool ctrl_key_pressed = (User32.GetAsyncKeyState(0x11) & 0x8000) != 0; bool ctrl_key_pressed = (User32.GetAsyncKeyState(0x11) & 0x8000) != 0;
if (!ctrl_key_pressed) if (!ctrl_key_pressed)
return; return;
_trayWindows.Add(hwnd);
new MinimizeToTray(hwnd); new MinimizeToTray(hwnd);
} }
@ -135,6 +140,8 @@ namespace PersistentWindows.Common.Minimize2Tray
{ {
if (disposing) if (disposing)
{ {
_trayWindows.Remove(_hwnd);
_systemTrayIcon.Visible = false; _systemTrayIcon.Visible = false;
//User32.ShowWindowAsync(_hwnd, (int)ShowWindowCommands.Show); //User32.ShowWindowAsync(_hwnd, (int)ShowWindowCommands.Show);
User32.ShowWindowAsync(_hwnd, (int)ShowWindowCommands.Restore); User32.ShowWindowAsync(_hwnd, (int)ShowWindowCommands.Restore);