Solved

Focus for a window

Posted on 2002-05-29
8
630 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
 
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 200 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now