/// <summary> /// Attaches to the specified window handle. /// </summary> /// <param name="hWnd">The handle to the window to attach to.</param> /// <returns>true if the window is now attached; otherwise, false.</returns> public bool DoAttach(IntPtr hWnd) { // Failsafe if (!UpdateStatus()) return false; if (hWnd == IntPtr.Zero) return false; // Detach old window if (!DoDetach()) return false; // Tray the window TrayMeClass trayMe = new TrayMeClass(); if (!trayMe.HookTrayWindow(hWnd, Icon.Handle)) MessageBox.Show(this, "Could not hook the window with the provided handle.", "TrayMe Error", MessageBoxButtons.OK, MessageBoxIcon.Error); // Update status UpdateStatus(); return true; }
/// <summary> /// Updates the status and returns true if this was successful. /// </summary> private bool UpdateStatus() { TrayMeClass trayMe = new TrayMeClass(); try { if (trayMe.IsHooked()) { if (!isWindowTrayed) { buttonTrayMe.Text = "Un-&Tray Me"; isWindowTrayed = true; } } else { if (isWindowTrayed) { buttonTrayMe.Text = "&Tray Me!"; isWindowTrayed = false; } } if (!buttonTrayMe.Enabled) buttonTrayMe.Enabled = true; return true; } catch (DllNotFoundException) { if (buttonTrayMe.Enabled) { buttonTrayMe.Enabled = false; buttonTrayMe.Text = "Missing Dll"; } } catch (EntryPointNotFoundException) { if (buttonTrayMe.Enabled) { buttonTrayMe.Enabled = false; buttonTrayMe.Text = "Bad Dll"; } } return false; }
/// <summary> /// Determines whether a window is currently attached. /// </summary> /// <returns>true, if there is no window attached or the attached window is now detached; otherwise, false.</returns> public bool DoDetach() { // Check for trayed window TrayMeClass trayMe = new TrayMeClass(); if (trayMe.IsHooked()) { if (trayMe.HookTrayWindow(IntPtr.Zero, IntPtr.Zero) == true) { MessageBox.Show(this, "Could not unhook currently trayed window.", "TrayMe Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return false; } UpdateStatus(); } return true; }