Re: can't bring "single instance" tray app to foreground
On Tue, 03 Jul 2007 13:56:30 GMT "David Ching" <dc@remove-this.dcsoft.com> wrote:
"Frank Cusack" <fcusack@fcusack.com> wrote in message
news:m2ir91zy2o.fsf@sucksless.local...
I meant I added it to AfxMessageBox().
I guess I'm not understanding what you want to have happen when the second
instance detects the first instance. You want some window to display, but
your main window is meant to be hidden. You keep mentioning some "dialog"
but then your code has AfxMessageBox(). So could you please clarify what
exactly you want to come to the foreground?
I want a dialog, from the first instance, to come to the foreground.
I am using AfxMessageBox() for simplicity and just to prove the concept
of being able to switch foregound apps/windows.
Yes, the second instance must be the one to call ASFW(), as it is the one
with the focus, and is the only one that can then allow other apps to
"steal" it.
I still cannot get this to work. Here, I hope, is enough code to
see what I'm doing wrong.
====
CTrayApp::CTrayApp()
: CSingleInstanceApp(TEXT("{...}"))
{
// if this is the 2nd instance, allow the 1st instance to come to fg
if (!AllowSetForegroundWindow(ASFW_ANY))
throw 1;
}
CTrayApp theApp;
BOOL
CTrayApp::InitInstance()
{
if (!AmITheFirst()) {
// wait just a bit so ASFW() in 2nd instance works
// (don't exit before 2nd instance calls SFW())
Sleep(500);
return FALSE;
}
CMainFrame *pMainFrame = new CMainFrame;
m_pMainWnd = pMainFrame;
if (!pMainFrame->Create(NULL, "foo"))
return FALSE;
m_pMainWnd->ShowWindow(SW_HIDE);
m_pMainWnd->UpdateWindow();
return TRUE;
}
void CTrayApp::OnSecondInstance(UINT, LPARAM)
{
m_pMainWnd->SetForegroundWindow(); // no effect (but main wnd was hidden)
AfxMessageBox("hello world!", MB_OK|MB_SETFOREGROUND); // not in fg
}
====
In the dialog version, instead of AfxMessageBox(), I would create the
dialog and expect it to be in the foreground when I call DoModal(),
since I had just set the main window to the foreground.
-frank