0

I want to write a program, which allows me to hide open windows.

To achieve this I use GetForegroundWindow() to get and ShowWindowAsync(hWnd, 0) to hide it. This works pretty good on most windows, but there are some problems with fullscreen applictions. For example the VLC Media Player only gets partly hidden: The Fullscreen window its gone, but the original window stays visible. Or the League of Legends Client gets never hidden.

I already tried to solve this by trying to get the fullscreen window by calling WindowFromPoint(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2) if GetForegroundWindow() doesn't return a window, but this didn't solved the problem.

Here is the full class:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HideMyPrograms {

    class Window {

        private bool visible = true;
        private bool wasMax = false;
        private IntPtr window;

        public Window(IntPtr t) {
            this.window = t;
        }

        public int PID {
            get {
                return GetWindowProcessID(window);
            }
        }

        public string Title {
            get {
                return GetActiveApplTitle(window);
            }
        }

        public IntPtr Handle {
            get {
                return window;
            }
        }

        public Image Image {
            get {
                return GetSmallWindowIcon(window);
            }
        }

        public string ProcessName {
            get {
                return GetWindowProcessName(window);
            }
        }

        public string ClassName {
            get {
                return GetWindowClassName(window);
            }
        }

        public Rectangle Rectangle {
            get {
                return GetWindowRectangle(window);
            }
        }

        public bool Visible {
            get {
                return visible;
            }
            set {
                if(value) {
                    if (wasMax) {
                        if (ShowWindowAsync(window, 3)) { //showmax
                            visible = true;
                        }
                    } else {
                        if (ShowWindowAsync(window, 1)) { //shownormal
                            visible = true;
                        }
                    }
                } else {
                    wasMax = IsZoomed(window);
                    if (ShowWindowAsync(window, 0)) { //hide
                        visible = false;
                    }
                }
            }
        }

        public static Window getForegroundWindow() {
            string[] blacklist = new string[] { "Progman", "Shell_TrayWnd", "BaseBar", "SideBar_AppBarWindow", "DV2ControlHost", "Button", "MSTaskListWClass", "SysListView32", "WorkerW" };

            IntPtr window = GetForegroundWindow();
            if (window.Equals(IntPtr.Zero) || IsWindowClassBlacklisted(window, blacklist)) {
                window = GetForegroundWindowByMiddlePoint();
                if (window.Equals(IntPtr.Zero) || IsWindowClassBlacklisted(window, blacklist)) {
                    return null;
                }
            }

            return new Window(window);
        }

        private static Int32 GetWindowProcessID(IntPtr hwnd) {
            Int32 pid;
            GetWindowThreadProcessId(hwnd, out pid);
            return pid;
        }

        private static string GetActiveApplTitle(IntPtr hwnd) {
            const int nChars = 256;
            StringBuilder Buff = new StringBuilder(nChars);

            if (GetWindowText(hwnd, Buff, nChars) > 0) {
                return Buff.ToString();
            }
            return null;
        }

        private static Image GetSmallWindowIcon(IntPtr hWnd) {
            try {
                IntPtr hIcon = default(IntPtr);

                hIcon = SendMessage(hWnd, 0x007f, new IntPtr(2), IntPtr.Zero);

                if (hIcon != IntPtr.Zero) {
                    return new Bitmap(Icon.FromHandle(hIcon).ToBitmap(), 16, 16);
                } else {
                    return new Bitmap(Icon.ExtractAssociatedIcon(Process.GetProcessById(GetWindowProcessID(hWnd)).MainModule.FileName).ToBitmap(), 16, 16); ;
                }
            } catch (Exception) {
                return null;
            }
        }

        private static string GetWindowProcessName(IntPtr hWnd) {
            return Process.GetProcessById(GetWindowProcessID(hWnd)).ProcessName;
        }

        private static bool IsWindowProcessBlacklisted(IntPtr hWnd, string[] blacklist) {
            string name = GetWindowProcessName(hWnd);

            foreach(string s in blacklist){
                if (name.Equals(s)) {
                    return true;
                }
            }

            return false;
        }

        private static string GetWindowClassName(IntPtr hWnd) {
            StringBuilder ClassName = new StringBuilder(256);
            GetClassName(hWnd, ClassName, ClassName.Capacity);
            return ClassName.ToString();
        }

        private static bool IsWindowClassBlacklisted(IntPtr hWnd, string[] blacklist) {
            string name = GetWindowClassName(hWnd);

            foreach (string s in blacklist) {
                if (name.Equals(s)) {
                    return true;
                }
            }

            return false;
        }

        private static Rectangle GetWindowRectangle(IntPtr hWnd) {
            RECT rect = new RECT();
            GetWindowRect(hWnd, out rect);
            return new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
        }

        private static IntPtr GetForegroundWindowByMiddlePoint() {
            return WindowFromPoint(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
        }

        [DllImport("user32.dll")]
        static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll")]
        static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr lpIconName);

        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);

        [DllImport("user32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);

        [DllImport("user32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool IsIconic(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern bool IsZoomed(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetActiveWindow();

        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out Int32 ProcessId);

        [DllImport("user32.dll")]
        private static extern IntPtr AttachThreadInput(IntPtr idAttach, IntPtr idAttachTo, int fAttach);

        [DllImport("user32.dll")]
        private static extern IntPtr GetParent(IntPtr hWnd);

        [DllImport("user32.dll")]
        private static extern IntPtr GetDesktopWindow();

        [DllImport("user32.dll")]
        static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);

        [DllImport("user32.dll")]
        static extern IntPtr WindowFromPoint(int xPoint, int yPoint);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT {
            public int Left;        // x position of upper-left corner
            public int Top;         // y position of upper-left corner
            public int Right;       // x position of lower-right corner
            public int Bottom;      // y position of lower-right corner
        }
    }

}

EDIT: I found the Error with League of Legends. The Problem wasn't my Window class, it was my Hotkey class. It seems that LoL is blocking Hotkey created with RegisterHotkey(). So i swaped to setWindowsHookEx() and now my program is working.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
sidit77
  • 49
  • 5

0 Answers0