Tutorial :Creating a new form with the same z-order as another form



Question:

Windows Forms, .net 2.0

My main application thread has a form (A). I have a background thread which operates off a timer to determine that an activity has taken place - when it does, it causes the main application form to hide (using BeginInvoke), and creates a new form (B) in its own thread (seperate message pump).

The two threads thing sounds complicated, but it is needed unfortunately.

The new form, on the new message pump is shown at the top of the non-topmost forms (as you would expect!). However, if the original application wasn't on top of the desktop this is very annoying.

So, I would like to determine what the z-order of the original form (A) is before hiding it; then create my new form (B) mirroring the z-order of the original form.

Any ideas?

[This also has the side effect of form B popping up over a screen-saver if it is on the current users desktop (ie non passworded), which if I can't fix it how I would like above, I would like to avoid this...]


Solution:1

I resolved this by using the Win32 GetWindow function to determine which the previous window was for my main form. Then when I create my new form on the second thread I can use SetWindowPos to set the z-order to the value that I obtained in the first step.

I also use nobugz suggestion to determine if the application was the focused application, and if not I use the ShowWithoutActivation override to ensure that my new form does not start activated


Solution:2

AFAIK, you can call Form.Activate() to bring it to the front, but I don't know of a Windows-level z-order in WinForms. You may have better luck in Win32.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »