Solved

Focus for a window

Posted on 2002-05-29
8
636 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

617 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