Example #1
0
        private static void EditWindowByHandle(IntPtr windowHandle, ShowWindowCommand action, bool waitForShowingUp = false)
        {
            if (action == ShowWindowCommand.SW_HIDE)
                _hiddenWindowHandles.Add(windowHandle);

            //Do this in a sepparate Thread because in case of minimizing windows the thread needs to wait for the window to show up
            ThreadPool.QueueUserWorkItem(
                delegate
                {
                    try
                    {
                        //If you want to minimize the window you have to wait for it to show up
                        if (action == ShowWindowCommand.SW_SHOWMINIMIZED && waitForShowingUp)
                            Thread.Sleep(500);
                        //ShowWindow is the standard way of editing the window status
                        if (!ShowWindowAsync(windowHandle, (int) action))
                        {
                            //If ShowWindowAsync failes (attention: it returns a value != zero if the state has been changed, if the state has remained unchained it returns false)
                            //Do it the hard way
                            if (action == ShowWindowCommand.SW_SHOWMINIMIZED)
                            {
                                SendMessage(windowHandle, 274, (IntPtr) SC_MINIMIZE, IntPtr.Zero);
                                Logger.AddInformation(String.Format("Window {0} minimized",
                                    windowHandle.GetWindowTitle()));
                            }
                            else if (action == ShowWindowCommand.SW_HIDE)
                            {
                                //This is a drastic action! If an application cannot be handled by the previous actions, for example if it has administrator privileges, then it gets closed!
                                SendMessage(windowHandle, 274, (IntPtr) SC_CLOSE, IntPtr.Zero);
                                Logger.AddInformation(String.Format(
                                    "Window {0} closed, because i was unable to hide it", windowHandle.GetWindowTitle()));
                            }
                            else if (action == ShowWindowCommand.SW_SHOWMAXIMIZED)
                            {
                                SendMessage(windowHandle, 274, (IntPtr) SC_MAXIMIZE, IntPtr.Zero);
                                Logger.AddInformation(String.Format("Window {0} Maximized",
                                    windowHandle.GetWindowTitle()));
                            }
                        }
                        else
                        {
                            Logger.AddInformation(String.Format("Window {0} {1}",
                                    windowHandle.GetWindowTitle(), action.ToString()));
                        }
                    }
                    catch
                    {
                        try
                        {
                            //If ShowWindowAsync failes (attention: it returns a value != zero if the state has been changed, if the state has remained unchained it returns false)
                            //Do it the hard way
                            if (action == ShowWindowCommand.SW_SHOWMINIMIZED)
                            {
                                SendMessage(windowHandle, 274, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
                                Logger.AddInformation(String.Format("Window {0} minimized", windowHandle.GetWindowTitle()));
                            }
                            else if (action == ShowWindowCommand.SW_HIDE)
                            {
                                //This is a drastic action! If an application cannot be handled by the previous actions, for example if it has administrator privileges, then it gets closed!
                                SendMessage(windowHandle, 274, (IntPtr)SC_CLOSE, IntPtr.Zero);
                                Logger.AddInformation(String.Format("Window {0} closed, because i was unable to hide it", windowHandle.GetWindowTitle()));
                            }
                            else if (action == ShowWindowCommand.SW_SHOWMAXIMIZED)
                            {
                                SendMessage(windowHandle, 274, (IntPtr)SC_MAXIMIZE, IntPtr.Zero);
                                Logger.AddInformation(String.Format("Window {0} Maximized", windowHandle.GetWindowTitle()));
                            }
                        }
                        catch (Exception)
                        {
                        }
                    }
                });
        }