Re: can't bring "single instance" tray app to foreground

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Frank Cusack" <fcusack@xxxxxxxxxxx> wrote in message
news:m2fy46ia1i.fsf@xxxxxxxxxxxxxxxxxx
I'm using the single instance class found at
<http://www.codeproject.com/threads/simplesingleinstanceapp.asp>.

When the first instance of my app receives the notification that
another instance was launched, it shows a dialog displaying the args
received. The problem is that this dialog does not become the
frontmost/active window.

I'm doing something like this:

void CTrayApp::OnSecondInstance(UINT, LPARAM)
{
m_pMainWnd->SetForegroundWindow();
AfxMessageBox(GetSecondInstanceCmdLine(), IDOK);
}

which throws up the dialog but it's behind the active window
(generally the windows explorer file browser).

I guess it makes sense that SetForegroundWindow() isn't doing
anything since (because this is a trap app) the main window
was hidden.

void CTrayApp::InitInstance()
{
...
m_pMainWnd->ShowWindow(SW_HIDE);
}

Now if, in OnSecondInstance(), I do ShowWindow(SW_RESTORE), my app
does come to the foreground, but I have a big blank main window
there now.

Any hints, or is what I'm trying to do even possible? Normally
when my app is launched with args, it needs to import a file and
that requires interaction with the user. That interaction involves
a dialog, so there is a window besides the main window that I can
bring to the foreground, but currently I use a modal dialog (using
DoModal()). If I used a modeless dialog could I just bring that
to the foreground after it is created? I'd rather stick to the
modal dialog for simplicity.



You need to do two things. First, the second instance of your app needs to
call AllowSetForegroundWindow() so that the first instance (another process)
is allowed to bring windows to the top.

Second, the first instance needs to create dialogs with the "Set Foreground"
style. In the dialog editor, this is a property you can set. For
AfxMessageBox(), the second parameter can be MB_OK | MB_SETFOREGROUND. In
your code, you should replace IDOK with MB_OK.

Cheers,
David


.



Relevant Pages

  • Re: App does not become frontmost?? Delay in InitInstance
    ... > user started it and steal the foreground away from the window the user is ... > so they won't wonder if your app has hung. ... Sure enuf, after a 35 second delay, I could see what they were seeing. ...
    (microsoft.public.vc.mfc)
  • Re: Pass in additional command line arguments to a single instance app
    ... Explorer to send the filename to my app. ... I use the existence of a window with a particular class name as the indicator that a "first instance" is running. ... An instance can not start (create the window), exit, or check for existence of the window unless it can acquire the mutex. ...
    (microsoft.public.vc.language)
  • Re: BringWindowToFront/SetForegroundWindow problem
    ... full-screen window and pushing it to the bottom before trying to bring up the previous app. ... means you no longer have the foreground window status, and thus lose some ability to bring another ... the message loop has activated to handle the Registered Window Message. ...
    (microsoft.public.vc.mfc)
  • Re: cant bring "single instance" tray app to foreground
    ... frontmost/active window. ... anything since (because this is a trap app) the main window ... the second instance of your app needs to ... call AllowSetForegroundWindowso that the first instance ...
    (microsoft.public.vc.mfc)
  • Re: cant bring "single instance" tray app to foreground
    ... your main window is meant to be hidden. ... 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 ...
    (microsoft.public.vc.mfc)