Never Losing the Focus

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!
LVL 1
gaggioAsked:
Who is Participating?
 
Computer101Connect With a Mentor Commented:
PAQed, with points refunded (100)

Computer101
E-E Admin
0
 
exelrudCommented:
0
 
exelrudCommented:
Do this app is a window or a dialog ? If it is a dialog you can try using a system modal one.
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
aponcealbuerneCommented:
Some code will useful.
but, are you usign
ShowWindow(SW_RESTORE);
before SetForegroundWindow()

hope helps
0
 
tvanceplusCommented:
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
 
jkrCommented:
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
 
gaggioAuthor Commented:
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
 
gaggioAuthor Commented:
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
 
gaggioAuthor Commented:
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
 
Roshan DavisCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.