P
Petec
Is there a way to prevent a form from getting focus?
Thanks!
- Pete
Thanks!
- Pete
Petec said:Is there a way to prevent a form from getting focus?
Paul said:Why do you want to do this? There is probably a better solution than
preventing the user from doing what he wants to do.
P.
Petec said:I have a main form with a grid of items, and when
the user hovers over an item a ballon with a detailed
description of the item pops up.
I'd like to prevent the balloon from getting focus, so
that the main form still has focus.
I could be wrong, but I don't think there's any managed code to do
this unfortunately (I've been looking for a managed solution to this
problem for a long time.)
What you could do is make an API call to ShowWindow.
You first have to bring in ShowWindow in your source with something
that looks like this:
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(IntPtr hWnd,Int32 nCmdShow);
Now you can use it. Instead of showing your form the conventional
way, show it like this:
ShowWindow(this.Handle, 4);
Now it will be shown without stealing focus.
I got this code from John O'Byrne from an excellent article on
CodeProject.com for an MSN-style skinnable popup window. You can find
the full source here:
http://www.codeproject.com/cs/miscctrl/taskbarnotifier.asp
The only change that I'd recommend to stay consistent with Win32
programming and keeping your code more elegant is to declare
SW_SHOWNOACTIVATE as a constant somewhere in your code instead of
hardcoding "4" for the show state.
ShowWindow(this.Handle, SW_SHOWNOACTIVATE);
Looks nicer. Otherwise that guy did a great job on his component and
you should check it out whether you need something like that or not.
Calling ShowWindow is a much better solution than setting focus back
to the main form, by the way. I've tried that. You get some problems
with the user's input being momentarily intercepted and flashing in
some cases and some obvious quirks. But the above works beautifully.
I REALLY hope they will provide a better solution for this in .NET
2.0, or perhaps wrap ShowWindow() in WinFX.
Petec said:Is there a way to prevent a form from getting focus?
Thanks!
- Pete