By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,389 Members | 2,010 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,389 IT Pros & Developers. It's quick & easy.

Preventing a form from getting focus

P: n/a
Is there a way to prevent a form from getting focus?

Thanks!
- Pete
Nov 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Petec" <x@x.x> wrote:
Is there a way to prevent a form from getting focus?


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.
Nov 16 '05 #2

P: n/a
Paul E Collins wrote:
"Petec" <x@x.x> wrote:
Is there a way to prevent a form from getting focus?


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.


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.

- Pete
Nov 16 '05 #3

P: n/a
"Petec" <x@x.x> wrote:
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.


Hovering involves use of the mouse, so if the user then clicks on
something else, that click will give focus back to the main form
anyway.

If that's not good enough, I suppose you could make the balloon
'always on top' (TopMost property) and automatically restore focus to
the main form as soon as the balloon loads.

P.
Nov 16 '05 #4

P: n/a
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

Nov 16 '05 #5

P: n/a
Thank you, that worked great!

- Pete

Eric wrote:
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


Nov 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.