tags:

views:

874

answers:

5

I have written an application which has a modal form. How can I ensure that this form does not lose the focus even when an other application is started?

+10  A: 

Actually, this is exactly the sort of thing you shouldn't be doing.

There's too many programs around that assume they control the computer they're installed on. It is the user of your application that should be in control.

That's why later versions of Windows disallowed stealing of focus instead insisting on just blinking the entry in the task list bar.

You may well find a way to do it (though I doubt it), but I urge you to rethink it. I'd be interested in knowing why you thought it was necessary.

paxdiablo
I wrote an application to hear to web radio stations while I am on my bicycle ergometer. I can change the stations by pressing a button on the game pad that is fixed on the ergometer. When the focus of the application is lost I am no longer able to use the game pad.
Why? How do you read the data from the game pad?
OregonGhost
I use the directX-Framework. When the focus of the application is lost I can't read the data any more.
Actually I retract my earlier comment: if it's software for your own use, you have a valid reason for keeping focus. Perhaps a question you should be asking is "how can I stop other apps from stealing my focus?"I would have thought that, while you're on your bike, no other apps would start.
paxdiablo
There are different actions, for example i can start you tube videos with the application. some videos cause script errors and because of that the browser control shows a pop up and the focus of the main form is lost.
You can use the DirectX (DirectInput I assume you are using) to read the data in a "promiscous" mode from the devices. I wrote a little app that let you use a game pad as a mouse, no matter which application had focus. If that is what you want, use SetCooperativeLevel with CooperativeLevelFlags.NonExclusive | CooperativeLevelFlags.Background as the flags.
Erich Mirabal
+3  A: 

You can set the "Topmost" property to true to keep the form in front of all others but that doesn't make it keep focus.

Niklas Winde
A: 

You must make the dialog system modal.

Horcrux7
"You must the dialog system modal." needs to be re-worded to be clearer.
John
A: 

I use

SetForegroundWindow(Me.Handle)

Me.Handle is the handle of your form.

You need to declare the following somewhere in your class or winform, but not inside a function

Declare Unicode Function SetForegroundWindow Lib "user32.dll" (ByVal hWnd As IntPtr) As Boolean

You might need to initiate a timer and call SetForegroundWindow on every tick of say 10 seconds, depending on your preference.

EDIT: It works for me, if it doesn't add the following

    Declare Unicode Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoW" (ByVal uiAction As Int32, ByVal uiParam As Int32, ByRef pvParam As Int32, ByVal fWinIni As Int32) As Int32

And surround SetForegroundWindow with these

    Dim _timeout As Int32
    SystemParametersInfo(&H2000, 0, _timeout, 0)
    SystemParametersInfo(&H2001, 0, 0, 3)
    SetForegroundWindow(Me.Handle)
    SystemParametersInfo(&H2001, 0, _timeout, 2)

That's the last resort

faulty
Won't work since XP, thankfully.
Simon Buchan
A: 

I have to use an application that grabs focus when it pops up an alarm. I hate it! (If I had any choice, I would not use the damn thing; sadly, I have to use it. Ugh!) Other parts of the program are far too keen on maintaining focus. Some forms do not let you cut'n'paste text from other pages. It is an abomination when a program does that. If you want your software used by people who have a choice, then DO NOT grab and retain focus.

Jonathan Leffler