🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.misc
Article #2622

Re: Enabling the first and only window in MFC

#2622
From: Chris Marriott
Date: Fri, 08 Jan 1993 19:34
56 lines
2225 bytes
In article <C0Fyw0.3pq@pcuf.fi> turre@pcuf.fi writes:

>I want user not to be able to run many instances of my application. So if
>he tries to run another instance my program should bring the first
>instance to top. I've been trying it with this following code:
> 
>BOOL CMyApp::InitInstance()
>{
>    If( m_hPrevInstance != NULL )
>    {
>        m_pMainWindow->BringWindowToTop();
>        return FALSE;
>    }
>    else
>    {
>        m_pMainWindow = new CMyWindow;
>        m_pMainWindow->ShowWindow( m_nCmdShow );
>        m_pMainWindow->UpdateWindow();
>        return TRUE;
>    }
>}
> 
>The first instance runs fine and no other instances are displayed or created
>but the first windows is not brought to the top.
>

No - your code is wrong! This code:

>    If( m_hPrevInstance != NULL )
>    {
>        m_pMainWindow->BringWindowToTop();
>        return FALSE;
>    }

brings *your* window to the top, not that of the previous instance.
Sorry I don't know MFC or whatever it is you're using here, but what
you want to do is:

    if (m_hPrevInstance!=NULL)
    {
        hWnd = FindWindow( "MyAppClassName", NULL );
        BringWindowToTop( hWnd );
        return FALSE;
    }

That will do the trick.

Chris
-- 
--------------------------------------------------------------------------
| Chris Marriott                           | chris@chrism.demon.co.uk    |
| Warrington, UK                           | BIX: cmarriott              |
| (Still awaiting inspiration              | CIX: cmarriott              |
|  for a witty .sig .... )                 | CompuServe: 100113,1140     |
--------------------------------------------------------------------------
 
Technical Details
Message-ID: <726521688snz@chrism.demon.co.uk>
Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!demon!chrism.demon.co.uk!chris
References: <C0Fyw0.3pq@pcuf.fi>

Show Full Headers
Complete Article Headers
Newsgroups: comp.os.ms-windows.programmer.misc
From: chris@chrism.demon.co.uk (Chris Marriott)
Path: arc1-ca.pugleaf.net!sparky!uunet!pipex!demon!chrism.demon.co.uk!chris
Subject: Re: Enabling the first and only window in MFC 
Distribution: world
References: <C0Fyw0.3pq@pcuf.fi>
Organization: None
Reply-To: chris@chrism.demon.co.uk
X-Mailer: Simple NEWS 1.90 (ka9q DIS 1.19)
Lines: 56
Date: Fri, 8 Jan 1993 19:34:48 +0000
Message-ID: <726521688snz@chrism.demon.co.uk>
Sender: usenet@demon.co.uk