tags:

views:

1614

answers:

5

I have a C# winforms app that runs a macro in another program. The other program will continually pop up windows and generally make things look, for lack of a better word, crazy. I want to implement a cancel button that will stop the process from running, but I cannot seem to get the window to stay on top. How do I do this in C#?

Edit: I have tried TopMost=true; , but the other program keeps popping up its own windows over top. Is there a way to send my window to the top every n milliseconds?

Edit: The way I solved this was by adding a system tray icon that will cancel the process by double-clicking on it. The system tray icon does no get covered up. Thank you to all who responded. I read the article on why there is not a 'super-on-top' window... it logically does not work.

+4  A: 

Set Form.TopMost

Reed Copsey
I tried, this... do I need to be continually doing it? The 'crazy program' takes over immediately...
jle
No - if you set your form.TopMost = true, it should work. The "crazy" program must have it's dialogs set to TopMost as well, in which case, you can't override it.
Reed Copsey
Not a fair fight. Thank you.
jle
+2  A: 

Set the form's .TopMost property to true.

You probably don't want to leave it this way all the time: set it when your external process starts and put it back when it finishes.

Joel Coehoorn
+10  A: 

Form.TopMost will work unless the other program is creating topmost windows. There is no way to create a window that is not covered by new topmost windows of another process. Raymond Chen explained why.

RossFabricant
+1  A: 

What is the other application you are trying to suppress the visibility of? Have you investigated other ways of achieving your desired effect? Please do so before subjecting your users to such rogue behaviour as you are describing: what you are trying to do sound rather like what certain naughty sites do with browser windows...

At least try to adhere to the rule of Least Surprise. Users expect to be able to determine the z-order of most applications themselves. You don't know what is most important to them, so if you change anything, you should focus on pushing the other application behind everything rather than promoting your own.

This is of course trickier, since Windows doesn't have a particularly sophisticated window manager. Two approaches suggest themselves:

  1. enumerating top-level windows and checking which process they belong to, dropping their z-order if so. (I'm not sure if there are framework methods for these WinAPI functions.)
  2. Fiddling with child process permissions to prevent it from accessing the desktop... but I wouldn't try this until the othe approach failed, as the child process might end up in a zombie state while requiring user interaction.
Pontus Gagge
+1  A: 

The way i solved this was by making a system tray icon that had a cancel option.

jle