Article View: borland.public.cppbuilder.nativeapi
Article #1323How to bring window (TForm) front
From: Jyrki Holopainen
Date: Mon, 17 Sep 2007 18:43
Date: Mon, 17 Sep 2007 18:43
61 lines
2861 bytes
2861 bytes
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.
The following function calls (called from TForm context) do not have
(any) effect:
VCL:
Show();
Visible = true;
BringToFront();
Application->BringToFront(); //Flashes the status bar icon three times?
SetFocus();
Windows API:
BringWindowToTop(Handle);
SetForegroundWindow(Handle);
EnableWindow(Handle, TRUE);
SetActiveWindow(Handle);
::SetFocus(Handle);
The following works, but the look and feel is bad especially if window
open/close animation is turned on (which is the default setting in Windows).
Application->Minimize();
Application->Restore();
The following is the best solution that I have managed to make. The
problem is that if the window is not in a minimized state, it is not
activated, but the focus stays in the original application. The
'minimized' is a boolean member that is updated in the Application event
handlers OnMinimize and OnRestore.
if (minimized)
{
Application->Restore();
}
else
{
SetWindowPos(Handle,
HWND_TOPMOST,
0,0,0,0, // Dummy size & position
SWP_NOSIZE | SWP_NOMOVE);
SetWindowPos(Handle,
HWND_NOTOPMOST,
0,0,0,0,
SWP_NOSIZE | SWP_NOMOVE);
}
How can this be so difficult, or am I missing something completely trivial?
/Jyrki Holopainen
Technical Details
Message-ID:
<46eecb19@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news2.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-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,67bd98c5f67e7407,start X-Google-Attributes: gid10cc67,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Date: Mon, 17 Sep 2007 21:43:57 +0300 From: Jyrki Holopainen <New.Address@spammers.com> Reply-To: "JyrkiBorland AT halo.pp.fi.remove" User-Agent: Thunderbird 2.0.0.6 (Windows/20070728) MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: How to bring window (TForm) front Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 91.152.173.240 Message-ID: <46eecb19@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1190054681 91.152.173.240 (17 Sep 2007 11:44:41 -0700) Lines: 62 Path: arc1-ca.pugleaf.net!g2news2.google.com!news2.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