Solved

Focus for a window

Posted on 2002-05-29
8
633 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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 viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

832 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