tags:

views:

579

answers:

3

We have a Win32 application that hosts the .NET runtime and opens up .NET managed forms from the Win32 portion of the application.

These windows are always opened as modal windows.

On some machines, when these windows are closed, the Win32 window that lies behind does not get focus, but gets sent behind Word, Outlook, or whatever else you might have open.

Also, sometimes, if we open such a .NET form, then alt-tab to Word or some other application, and then click on the taskbar icon for our app, the Win32 window appear. This is of course still waiting for the modal .NET window to close, so it is of course unusable. If we alt-tab to something else and just minimize that other thing, then our .NET window reappears.

The inconsistent part is that this occurs on only some machines, and not all. On many machines, mine included, it works exactly as expected. Focus to the right window works every time.

I don't doubt we have done something wrong, but I can't figure out what the problem is.

Does anyone have any idea what I should be looking for? We've looked at the .NET runtimes installed, and since two such machines where it works on one but not the other are both developer machines, they contain the same service packs for .NET and so on.


Edit: Well, @sam, you were right in that we had some different setups in this lane. Both machines run Windows XP SP3, but mine was running classic windows theme, and the other was running the new XP theme. Changing theme on that other computer to classic removed the problem, but changing it back to the XP theme did not make it reappear.

So now we have two machines where it work, and the customer still has the problem, even though the customer apparently runs classic theme.

+2  A: 

Lately I noticed that window focus is set differently on windows vista Aero as it used to be on XP. Instead of the last activated window of an app receiving the focus, the main window will receive focus on re-activation.
Funny thing is, changing vista to the old style windows theme (classic or basic), the focus will be set to the child window which last had it, just like it is on XP.

Your problem might be very different, but maybe you got different OS versions?

Sam
+1  A: 

This problem reminds me that WPF has the same issue with showing dialogs from a parent window.

I will assume you are refering to the version 2.0 of the .NET framework and that you have a native module that calls the .NET assembly.

Any control from windows forms (which includes also the forms), implements an interface called IWin32Window, which exposes the Win32 Handle of that window.

What you could do, is to use a NativeWindow to listen for WM_ACTIVATEAPP or any event that would signal that your app has focus and send a signal to the .NET window to get the focus.

Other option would be to use a native dialog that hosts a .net user control instead of winforms (if you can).

I've did this before without too much hustle, using C++\CLI, but right now doesn't come to my mind. Maybe this would be a good starting point for you.

Bogdan Maxim
I'll look into this, thanks!
Lasse V. Karlsen
If you find a solution, please post it, as it might help others too
Bogdan Maxim
A: 

I recall helping a friend with the same problem (can't remember if it where in .net 2.0 or 3.5)

I assume that on your main form you create new forms and use

Form2 form2 = new Form2();
form2.ShowDialog();

To display the modal popup, now apparently it seems that the ShowDialog method does not use the form calling the ShowDialog as its handler per default so you need to do

form2.ShowDialog(this);

this way the application to my knowledge knows what handle to return to, and thus always returns to your main from.

Additional info on ShowDialog form msdn: http://msdn.microsoft.com/en-us/library/w61zzfwe.aspx

thmsn
Thanks, we know about that one, but when the .NET form is shown from unmanaged code, it gets a bit hairier.
Lasse V. Karlsen