tags:

views:

277

answers:

2

I have an application written in .NET. The previous version had no problems: you double-click on the icon or run it from a command line and when it starts up, it's the main window and has focus as you'd expect.

The latest version displays a splash screen before the main window and now the splash screen comes to the foreground ok but the main one does not always end up the main window. Sometimes it does, sometimes it doesn't. (When launched from the command line it invariably doesn't). When the main window does not come to the foreground and get focus, the taskbar icon shows as a steady orange.

I see lots of hits on the net about how MS added a facility to prevent applications stealing focus from others, centered around the ForegroundLockTimeout registry setting and related settings, but the behaviors described above for the different versions happen on the same machine.

I have tried called Activate in the main form when it finally gets created, and also SetForegroundWindow, all to no avail.

Any help is appreciated.

+2  A: 

Sounds like your main window doesn't always get the focus back from the splash screen. Have you tried simply calling SetFocus in the main form's OnLoad handler?MSDN

Paul Sasik
By jove I think you've got it. I thought the Activate method gave a form focus as well but from reading the docs it says "Activating a form brings it to the front if this is the active application, or it flashes the window caption if this is not the active application."It's going to suck if this is all it was - I feel like an utter novice...
Sam
Sadly this didn't work in the end. It might work on my dev machine but not another dev's machine or the tester's system.
Sam
+4  A: 

You should probably have the splash screen set focus to the main app window as it is going away.

As far as Window's knows your splash screen IS your app since it's the first toplevel window shown after the process is started. So that window gets the focus, but any OTHER window trying to grab focus on that same app startup (the icon click/run command) is believed to be a focus thief.

You can get around this by having the window that Window's believes has a right to have focus be the one to transfer focus to a new window.

So you splash should SetFocus on the main window BEFORE the splash is destroyed. If you destroy the focus window, then the focus is nowhere, which is probably what is happening currently in your app.