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/miscct...arnotifier.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" <x@x.x> wrote in message
news:d7****************@newsread1.news.pas.earthli nk.net...
Is there a way to prevent a form from getting focus?
Thanks!
- Pete