🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #1325

Re: How to bring window (TForm) front

#1325
From: Dennis Jones
Date: Mon, 17 Sep 2007 19:37
53 lines
3441 bytes
"Jyrki Holopainen" <New.Address@spammers.com> wrote in message 
news:46eecb19@newsgroups.borland.com...
>
> I have an application that is divided into two separate executable files. 
> The first one writes data to a file and starts the second one. When the 
> second exe starts, it reads the data from a file and displays it. During 
> the startup the second exe must check if the program is already running. 
> In that case it sends a command to the running program which reads the 
> data and displays it. The running program must bring it's window to the 
> front, but that seems to be difficult under Windows XP.
>
> How can this be so difficult, or am I missing something completely 
> trivial?

It's not difficult, but there is a trick to it.  Once you know that there is 
another instance already running (and it sounds like you've already solved 
that part of it, so I won't give an explanation here), then you just need to 
know how to correctly access the running instance's main window.  The 
following code is pulled directly from one of my applications (written in 
BCB6, runs on XP).  The key to doing this is knowing that the main form in a 
VCL app is *not* the same as the main window of a Windows application:

         // The main "form" in a C++Builder/Delphi application is not the
         // application's main window (as far as Windows is concerned).
         // TApplication creates a hidden window that acts as the true main
         // window for the application.  Thus, whenever we want to refer to 
the
         // application's main window, we must use the handle of the window
         // created by TApplication, not the handle of the main "form".  The
         // TApplication's window handle is the parent window handle for the
         // main form, so it is easy to find:

         // get the window handle of the other instance's main form 
(according to its class name)
         HWND MainFormHWnd = FindWindow( "TMyMainForm", NULL );

         // get the main form's parent window handle (we'll assume 
MainFormHWnd
         // is not NULL, since we wouldn't even be here unless another 
instance
         // of the application were actually running)
         HWND hAppWnd = (HWND)GetWindowLong( MainFormHWnd, GWL_HWNDPARENT );

         if ( IsIconic( hAppWnd ) )
         {
            ShowWindow( hAppWnd, SW_RESTORE );
         }
         SetForegroundWindow( hAppWnd );

HTH,

- Dennis 
Technical Details
Message-ID: <46eed78e@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!eweka.nl!lightspeed.eweka.nl!194.134.4.77.MISMATCH!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <46eecb19@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,67bd98c5f67e7407
X-Google-Attributes: gid10cc67,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Dennis Jones" <nospam@nospam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <46eecb19@newsgroups.borland.com>
Subject: Re: How to bring window (TForm) front
Date: Mon, 17 Sep 2007 12:37:52 -0700
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3138
X-RFC2646: Format=Flowed; Response
NNTP-Posting-Host: 71.245.109.58
Message-ID: <46eed78e@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1190057870 71.245.109.58 (17 Sep 2007 12:37:50 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!eweka.nl!lightspeed.eweka.nl!194.134.4.77.MISMATCH!news2.euro.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail