views:

98

answers:

5

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.

A: 

You can do:

this.Hide()

this.Visible = false;

Or if this is the main for you dont want to show, simply dont run the forum when you start the app.

EKS
I'm not exactly looking to hide my application's windows, but selective processes in a process list containing names of applications I wish to obscure from view.
JonnyLitt
The problem is for other application's windows, not the current one.
ChrisF
+2  A: 

If you are looking to hide your application window, I can suggest 3 things. First thing, try setting your form's visible property true and calling the hide() method. Second thing is to set your form's transparency to 100%, which will hide it. The third is, maybe consider your application should be a windows service instead of a windows form application.

If you are looking to hide other windows so that your application is always on top, set the TopMost property to true in your form: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.topmost(VS.71).aspx

This might help too: http://www.codeproject.com/KB/cs/windowhider.aspx

icemanind
Aye, I don't exacly want to cover everything with a form, and I also don't need to hide my application. I mentioned such in the question. Sorry XD
JonnyLitt
Try this: http://www.codeproject.com/KB/cs/windowhider.aspx
icemanind
Actually that does look promising, I'll try it.
JonnyLitt
Obviously, you will have to modifiy it to suit your needs. But basically, in a nutshell, what you need to do is enumerate through all the windows (which it shows you how to do) and then call ShowWindow() API on each one you want to hide (which the article also shows you how to do). Good luck!
icemanind
Works like a dream, thanks mate, this was the solution I essentially was looking for.
JonnyLitt
+1  A: 

You could use the windows api to hide the offending window. user32.showwindow

Not exactly what you've asked for, but might be simpler.

chilltemp
Actually that may be very useful as most browsers keep tabs in one window, it's a good bet no one has multiple windows. Thank you for the first real possible answer that isn't a flame mate.
JonnyLitt
Your welcome. Just don't forget to unhide. I'd recommended that you store a list of hidden windows in a file, updating at every action you take. That way you can easily recover from a crash. Otherwise you'll simply eat up resources with zombie applications.
chilltemp
+4  A: 

I really hate hate hate any application trying to mess around with other application's windows. Sorry, this comes from very deep.

The only thing I can think of that is somewhat sensible is to lock the current user session, and swith the computer to another desktop belonging to another account with no rights to do anything except what's required under the circumstances.

Or if it is acceptable to disable the use of the computers entirely, you could put all the monitors on a single power switch on the teacher's desk.

jdv
Sorry you feel that way mate. I'm not the first to make an application as so, and I'm not the first to be used for making one.As far has having a single power switch on the instructors desk, well: I'm not looking to lock down everything and completely disable the computer, I'm merely trying to block browsers. Multiple sessions wouldn't help much either; there are only 20 computers, all being used.
JonnyLitt
+1  A: 
   [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.

kekekela
I like the simpler approach there.
JonnyLitt