Search code examples
c#.netblockingaccess-control

How can I prevent windows from showing in .NET?


I'm currently working on an access control program in C# and I've come across the problem of blocking windows. The original idea I've come up with is rendering a plain black form over the position given by the IntPtr window handle of the process. The problem within that is updating the form's position smoothly and z-index of the position (being I don't want it topmost). I've also noticed a ridiculously high resource use with my solution as I was using a loop to constantly check position.

Thus why I ask: What would be the best solution for this without eating major resources? The entry point is merely the name of the running process.

Presently the idea is only blocking off browsers (IE: a school application to prevent distraction when a lecture is active).

More Information:

  • I'm not looking to close a window in my own application, I'm trying to obscure windows from other processes.

  • My application is not a virus/annoying program, it's essentially meant to prevent uses of potentially distracting applications in a school environment. It's made for lectures in a school computer lab.

  • I'm presently pulling the main window from the process caught by the process name of browsers.

  • I can't completely disable the computers either.


Solution

  •    [DllImport("user32.dll")]
       static extern bool ShowWindow(IntPtr hWnd, int nCmdShow); 
    
       static void MinimizeProcess(string procName)
        {
            foreach (Process p in Process.GetProcesses())
            {
                if (p.ProcessName == procName)
                {
                    ShowWindow(p.MainWindowHandle,11);
    
                }
            }
        }
    

    If you have an array of process names you'd obviously want to refactor this to take in an array so that you're not looping through every process for everything you want to minimize, but you get the idea.