471,594 Members | 2,171 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,594 software developers and data experts.

Preventing a form from getting focus

Is there a way to prevent a form from getting focus?

Thanks!
- Pete
Nov 16 '05 #1
5 9941
"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
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
"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
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
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.

Similar topics

5 posts views Thread by Darren DeCoste | last post: by
8 posts views Thread by Susan Bricker | last post: by
2 posts views Thread by Sid Price | last post: by
3 posts views Thread by sravan_reddy001 | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.