?
Solved

Focus for a window

Posted on 2002-05-29
8
Medium Priority
?
639 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
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

719 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