Solved

Never Losing the Focus

Posted on 2001-07-10
10
370 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
Independent Software Vendors: 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!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

726 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