?
Solved

Never Losing the Focus

Posted on 2001-07-10
10
Medium Priority
?
372 Views
Last Modified: 2013-11-20
Simple question.

I have an MFC Application.

When the user clicks outside the application (for example on another application's window or on the desktop), I want to reactivate my application's main window.

I screwed around a little with SetForegroundWindow() and the WM_ACTIVATE messages, catching them in WindowProc(), etc., but got no luck. In Windows 2000, the best I can do is to have the application's icon BLINK on the taskbar (I can never have it come to the front).

So... Do I suck or does Windows 2000 suck?
Thanks for your help!
0
Comment
Question by:gaggio
[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
10 Comments
 
LVL 2

Expert Comment

by:exelrud
ID: 6270951
0
 
LVL 2

Expert Comment

by:exelrud
ID: 6270953
Do this app is a window or a dialog ? If it is a dialog you can try using a system modal one.
0
 
LVL 4

Expert Comment

by:aponcealbuerne
ID: 6270978
Some code will useful.
but, are you usign
ShowWindow(SW_RESTORE);
before SetForegroundWindow()

hope helps
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 1

Expert Comment

by:tvanceplus
ID: 6271092
Can you use SetCapture() to take away all of the system messages and use ReleaseCapture() when you're finished?

These names may be off a little bit but it's close. They are in the SDK.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6271303
Use 'SetWindowPos()' with the 'WS_EX_TOPMOST' style.

Quote from the docs:

"With Windows versions 3.1 and later, windows can be moved to the top of the Z-order and locked there by setting their WS_EX_TOPMOST styles. Such a topmost window maintains its topmost position even when deactivated. For example, selecting the WinHelp Always On Top command makes the Help window topmost, and it then remains visible when you return to your application.

To create a topmost window, call SetWindowPos with the pWndInsertAfter parameter equal to &wndTopMost, or set the WS_EX_TOPMOST style when you create the window."

See also

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cwnd.3a3a.setwindowpos.asp('CWnd::SetWindowPos')

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcemfc/htm/cwnd_17.asp ('CWnd::CreateEx')

http://support.microsoft.com/support/kb/articles/Q108/3/15.asp ('HOWTO: Keep an MDI Window Always on Top')
0
 
LVL 1

Author Comment

by:gaggio
ID: 6273333
I am sorry jkr, but this is not exactly what I would like to do. So let me refine the question. I got some ideas with the other posts, but could not solve my problem.

I would like my window to be able to be deactivated. Let's say that I catch the deactivation message in WindowProc. At this time, I am launching a thread, which waits 2 seconds and then reactivate the window.

How do I do this?
Currently, it works only the first time. The "bring it back to front" thread does (just and) exactly this:

Sleep(2000);
mywindow->PostMessage(WM_ACTIVATE,WA_ACTIVE,save);

"save" is the LPARAM of the WM_ACTIVATE/WA_INACTIVE that I catch in WindowProc like this:

LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
     if(message==WM_ACTIVATE && wParam==WA_INACTIVE)
     {
          save = lParam;
          mybringbacktofrontthread.Execute();
          return TRUE;
     }
     return CFrameWnd::WindowProc(message, wParam, lParam);
}

when I put a breakpoint on PostMessage in the thread function, I see that the thread is executed each time mywindow looses focus/activation, but mywindow is brought to front only the first time.

any thought?
Thanks a lot for your help.
0
 
LVL 1

Author Comment

by:gaggio
ID: 6277850
I have found something that looks like exactly what I need - it is a function in the platform SDK called "LockSetForegroundWindow."  This function prevents other applications from stealing the foreground status and thereby interrupting user interaction.  The function is documented on the MSDN website, but my compiler does not recognize it.  I think I have included all of the necessary headers and libs, but still no luck.  Has anyone heard of or successfully used this function before?  If so, I'd love to hear about it!
0
 
LVL 1

Author Comment

by:gaggio
ID: 6277915
I have found something that looks like exactly what I need - it is a function in the platform SDK called "LockSetForegroundWindow."  This function prevents other applications from stealing the foreground status and thereby interrupting user interaction.  The function is documented on the MSDN website, but my compiler does not recognize it.  I think I have included all of the necessary headers and libs, but still no luck.  Has anyone heard of or successfully used this function before?  If so, I'd love to hear about it!
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9490293
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

PAQ'd and pts refunded

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Roshan Davis
EE Cleanup Volunteer
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9537279
PAQed, with points refunded (100)

Computer101
E-E Admin
0

Featured Post

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.

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

770 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