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.
The way i solved this was by making a system tray icon that had a cancel option.
I was searching to make my WinForms application "Always on Top" but setting "TopMost" did not do anything for me. I knew it was possible because WinAmp does this (along with a host of other applications).
What I did was make a call to "user32.dll." I had no qualms about doing so and it works great. It's an option, anyway.
First, import the following namespace:
Add a few variables to your class declaration:
Add prototype for user32.dll function:
Then in your code (I added the call in Form_Load()), add the call:
Hope that helps. Reference
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.
Why not making your form a dialogue box:
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.
Here is the SetForegroundWindow equivalent:
I have seen people doing weird things like:
http://blog.jorgearimany.com/2010/10/win32-setforegroundwindow-equivalent-in.html