Solved

Never Losing the Focus

Posted on 2001-07-10
10
366 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
10 Comments
 
LVL 2

Expert Comment

by:exelrud
Comment Utility
0
 
LVL 2

Expert Comment

by:exelrud
Comment Utility
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
Comment Utility
Some code will useful.
but, are you usign
ShowWindow(SW_RESTORE);
before SetForegroundWindow()

hope helps
0
 
LVL 1

Expert Comment

by:tvanceplus
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

by:gaggio
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
PAQed, with points refunded (100)

Computer101
E-E Admin
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 76
sum67 challenge 35 88
wait notify demo infinite loop 3 80
Unix Command -- Challenging  question 7 79
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: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

744 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now