Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Restoring a Window -- single instance problem

696 views
Skip to first unread message

Don

unread,
Jun 24, 2001, 4:08:35 PM6/24/01
to
I have an APP that I want to only run once. I have done the usual stuff and
can force the second instance to send a message to the first instance of the
APP. However, I can't get the focus to go back to the first instance.

Below is my code for the reception of the SpecialFocus message that I
defined. I have verified that I get the message by using the message box.
And EVERYTHING works correctly when the MessageBox is displayed. However
when the message box is commented out (as shown), the APP never recieves
focus nor is it brought to the top. I have tried several other combinations
of the commands below. One that I tried is setting the window to
"&wndTopMost" instead of "&wndTop". When I do that, the window will become
the topmost window when it gets clicked on. So I KNOW the code is being
executed. I'd really appreciate any ideas on what I am doing wrong.

Thanks

void CMyAppDlg::OnSpecialFocusMsg(WPARAM wParam, LPARAM lParam)
{
// ::AfxMessageBox("Receiving Focus from Duplicate App -- This message
is temporary");
ShowWindow(SW_SHOWNORMAL);
BringWindowToTop();
SetWindowPos(&wndTop, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE |
SWP_NOMOVE);
SetForegroundWindow();
}

--
Don
This is a SPAMproof address. Please address all replies to the newsgroup

Christian McArdle

unread,
Jun 25, 2001, 5:35:25 AM6/25/01
to
>I have an APP that I want to only run once. I have done the usual stuff
and
>can force the second instance to send a message to the first instance of
the
>APP. However, I can't get the focus to go back to the first instance.

SetForegroundWindow in the previous instance will often only flash the
program, rather than bring the window to the front. You could try this
sequence in the new instance (that is about to quit):

SetForegroundWindow(); // to ensure that you are allowed to call the next
function
LockSetForegroundWindow(LSFW_UNLOCK); // turns off SetForegroundWindow
locking
(Now other applications, such as the previous instance) can call
SetForegroundWindow. Send your private message now.

I've never tried this, but the documentation seems to imply that it would
work.

Christian.


0 new messages