Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Focus for a window

Posted on 2002-05-29
8
Medium Priority
?
642 Views
Last Modified: 2008-02-01
I want to set a window to be in focus (selected). I've tried SetFocus() (both global scope and CWnd::), BringWindowToTop, ShowWindow(SW_NORMAL), none work. There is also SetForgroundWindow() which doesn't work but I think it's because it requiers AllowSetForgroundWindow() to be called first (the latter works only on 2000/XP and ME so no good for me).

the best I could do is using SetWindowPos (BTW the flag wndTop does NOT work either) with wndTopMost and the again with wndNoTopMost to pop the window up but the result is that the window becomes top most but not selected. (Is there something that our buddies at MS wrote that actually works the way you'd expect???)

The other positive change I could manage was using showWindow() to restore a previously minimized window (in which case it ALSO recieves focus).

Please help me out here

reagrds
Ira

0
Comment
Question by:Krip
8 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 7042019
>>There is also SetForgroundWindow() which doesn't
>>work but I think it's because it requiers
>>AllowSetForgroundWindow()

You do not need to call 'AllowSetForgroundWindow()' - just use

SetForegroundWindow ( hMyWnd);
0
 
LVL 86

Expert Comment

by:jkr
ID: 7042033
BTW - see also

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/winui/windows_1eev.asp ("SetForegroundWindow")

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q97925 ("INFO: SetActiveWindow() and SetForegroundWindow() Clarification (Q97925)")
0
 
LVL 86

Expert Comment

by:jkr
ID: 7042038
Another relevant link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/CommCtls/winui/windows_08tv.asp ("Foreground and Background Windows")

It seems that there really is an XP issue about 'AllowSetForgroundWindow()'
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 49

Expert Comment

by:DanRollins
ID: 7042554
hi Krip,
Are you trying to bring a top-level window to the front?  Is this a mdeless dialog owned by your own application?  Is it a child window and you just want keyboard focus?  

There are separate issues involved in the various scenarios.

-- Dan
0
 

Author Comment

by:Krip
ID: 7043910
I know very little of the API and MFC but I think The window is top level, and the main class of the app itself inherits CDialog (BTW does this make it a child or not?).

the application should be ontop and in focus whenever the user is prompted for action.

Does this help or do I need to specify other things?

also if it wasn't clear from my original question I need this to work regardless of windows version.

0
 
LVL 49

Accepted Solution

by:
DanRollins earned 600 total points
ID: 7045354
The problem is related to a security feature added to Windows as of the second release of Win98.  It prevents programs from 'stealing' the active status without the user's consent.  

For instance, a program could grab the keyboard focus as you type in a password.  It also prevents the irritation of, for instance, if you have just done a large 'cut' in preparation of paste, when another window takes focus, you paste, and then wonder where everything went.  Now put in the mix those popup Internet ads and ask yourself, do I really want to allow that window to take control of my computer for even a nanosecond?

The point is that if your application is the foreground application, then any of the above techniques will put your window front-and-center.  But if the user is actively working in some other application, then your taskbar button will flash to indicate that you need attention (but your window will remain in the background).  The user will click that taskbar button and you are now in front.  If it is real important, you can start a timer and keep your taskbar flashing and/or play some sounds to get the user's attention.

-- Dan
0
 
LVL 3

Expert Comment

by:CoolBreeze
ID: 7047784
Yeah Dan is right.

Your code is only ran if your window is in focus in the first place.

maybe you should try this, whenever your program went out of focus, set it back on focus
0
 
LVL 11

Expert Comment

by:griessh
ID: 7178858
Dear Krip

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "DanRollins"

comment(s) as an answer.

If you think your question was not answered at all, you can post a request in Community support (please include this link) to refund your points. The link to the Community Support area is: http://www.experts-exchange.com/commspt/


PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month12 days, 14 hours left to enroll

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question