C# - Allow only specific windows on top of application?

107 views Asked by At

I'm trying to create a fullscreen shell application, that allows only specific other windows on top.

My first approach to this problem was actually the other way arround. What I did was to list all current processes and hide every processes main windows, except for the ones specified (for demonstrating purposes I use a command application here):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace HideWindows
{
    class Program
    {
        private const int SW_HIDE = 0;

        [DllImport("User32")]
        private static extern int ShowWindow(int hwnd, int nCmdShow);

        static void Main(string[] args)
        {
            Process[] processesRunning = Process.GetProcesses();
            string[] allowedProcs = { "cmd" };
            bool found = false;
            int hwnd;

            foreach (Process pr in processesRunning)
            {
                found = false;
                foreach (string s in allowedProcs)
                {
                    if (pr.ProcessName.ToLower() == s.ToLower())
                        found = true;
                }
                if (found)
                {
                    hwnd = pr.MainWindowHandle.ToInt32();
                    ShowWindow(hwnd, SW_HIDE);
                }
            }
        }
    }
}

This method seems to work quite well, given that all processes are able to be hidden. Problem is, that some applications do not seem to react, which makes me think if it is the right approach.

Is there some different method to hide windows? Especially older applications seem not to react to the being set hidden.

Or is there even a completely different approach? I also thought about decide actively on which windows my window is shown topmost, but have no idea if that is even possible.

best regards, Michael

0

There are 0 answers