private void UnhookThunderbird() { log.Information("Unhooking Thunderbird..."); if (thunderbirdProcess != null) { if (thunderbirdMainWindowHandle != IntPtr.Zero) { if (!thunderbirdShown) { log.Debug("Previously hidden, showing."); // If previously hidden, show to avoid bug User32.ShowWindow(thunderbirdMainWindowHandle, User32.ShowWindowType.SW_NORMAL); } thunderbirdMainWindowHandle = IntPtr.Zero; } windowStateHook.Unhook(); windowStateHook.WindowStateChange -= Thunderbird_VisualStateChanged; windowStateHook = null; thunderbirdProcess.Dispose(); thunderbirdProcess = null; initTask = null; } }
private bool HookThunderbird() { log.Information("Hooking on to Thunderbird..."); if (thunderbirdProcess != null && thunderbirdMainWindowHandle != IntPtr.Zero) { log.Information("Already hooked!"); // Already hooked return(true); } thunderbirdProcess = Process.GetProcessesByName("thunderbird").FirstOrDefault(); if (thunderbirdProcess == null) { log.Error("Cannot find the Thunderbird process to hook to."); // Hook failure, process does not exist return(false); } thunderbirdMainWindowHandle = FindMainThunderbirdWindow(thunderbirdProcess); if (thunderbirdMainWindowHandle == IntPtr.Zero) { log.Error("Cannot find Thunderbird's main window."); // Main window is lost (hidden) return(false); } else { log.Debug("Hooked on to window handle {@thunderbirdMainWindowHandle}", thunderbirdMainWindowHandle); } thunderbirdProcess.EnableRaisingEvents = true; thunderbirdProcess.Exited += Thunderbird_Exited; switch (Properties.Settings.Default.HookMethod) { case (int)HookMethod.UIAutomation: windowStateHook = new UIAutomation(); break; case (int)HookMethod.Polling: default: windowStateHook = new Polling(); break; } currentHookMethod = (HookMethod)Properties.Settings.Default.HookMethod; if (!windowStateHook.Hook(thunderbirdMainWindowHandle)) { return(false); } windowStateHook.WindowStateChange += Thunderbird_VisualStateChanged; log.Debug("Attached event handlers for window."); // If not already hidden and is currently minimised, hide immediately var isIconic = User32.IsIconic(thunderbirdMainWindowHandle); if (trayLaunched && Properties.Settings.Default.MinimiseOnStart) { log.Information("Thunderbird launched with tray application, hiding now. {@thunderbirdShown}.", thunderbirdShown); HideThunderbird(); } if (thunderbirdShown && isIconic) { log.Information("Thunderbird is already minimised, hiding now. {@thunderbirdShown}, {@isIconic}.", thunderbirdShown, isIconic); HideThunderbird(); } return(true); }