Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SetForegroundWindow, ShowWindow problems

3,753 views
Skip to first unread message

David Beardwood

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
my goal is to activate a 2nd delphi application that's already running, and
bring it up as the active window. my code works fine on a Windows Explorer
window, but fails subtly on Delphi applications.(ie. when the window to bring
to front is a delphi application - the calling app is always a delphi app).

my code is this:


var
WinClass: String;
begin
WinClass := 'TMySecondDelphiApp';
// WinClass := 'ExploreWClass'; {activates Windows Explorer app}
WinHandle := FindWindow( PChar(WinClass), nil );
if WinHandle <> 0 then
begin
{ we found an instance }
SetForegroundWindow( WinHandle );
if IsIconic( WinHandle ) then
ShowWindow( Winhandle, SW_RESTORE );
end;
end;


In all cases, the 2nd app is already running. As i said above, if i use the
commented out line for ExploreWClass, everything works perfectly. When the
second app is a delphi application, my problems are these:

SetForeGroundWindow:
Works if not minimized - brings to front and activates properly.
If minimized, does not activate taskbar icon (should show as a 'clicked'
button)

IsIconic
Always returns False, even when minimized.

ShowWindow
Restores the window, but not quite correctly. The window expands and
activates. The minimize button doesn't work on the app title bar, nor does
minimize from its control menu (top left corner). if you right-click its
taskbar icon 'Restore' is in bold and enabled, when it should be disabled (as
it should already be restored). Minimize is disabled. When you click restore
on this menu, everything then works correctly. (i also tried SW_SHOW, with
the same results)

thanks for any help, hints or advice.
dave

???what's wrong??? i'm using delphi 3. i got a lot of the code from "The
Tomes of Delphi 3: Win32 Core API". since it works on the Windows Explorer i
assume the code is correct. what's with the delphi apps/windows? the 2nd
delphi app that i'm trying to activate is just a normal form - nothing fancy,
no child window, etc.


Gary Rinsem

unread,
Mar 4, 1999, 3:00:00 AM3/4/99
to
David Beardwood wrote:

> my goal is to activate a 2nd delphi application that's already running, and
> bring it up as the active window. my code works fine on a Windows Explorer
> window, but fails subtly on Delphi applications.(ie. when the window to bring
> to front is a delphi application - the calling app is always a delphi app).

I didn't study your code that closely, but if it's your own app I would suggest a different method.
In the second app declare a function that responds to messages, specifically one that you created,
and won't be called by any other program. Then send that message to your second app whenever you
want it to do something. This let's you give it many different functions that can be called from
another one of your programs. The same message with different params can have different meanings.

Gary

Daniel P. Stasinski

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to David Beardwood
> my goal is to activate a 2nd delphi application that's
> already running, and bring it up as the active window.

This is the only code I've found that actually works on Windows/98 as
well as 95. Credit for it goes to Yorai Aminov of TeamB and another
nameless soul.

function ForceForegroundWindow(Const hWnd: THandle): Boolean;
var
hCurWnd: THandle;
begin

Result := False;

if not isWindow(hWnd) then Exit;
hCurWnd := GetForegroundWindow;

if (hCurWnd=hWnd) then begin
Result := True;
Exit;
end;

if (GetWindowLong( hWnd, GWL_STYLE ) and WS_MINIMIZE)<>0 then
ShowWindow(hWnd, SW_SHOWNOACTIVATE);

AttachThreadInput(GetWindowThreadProcessId(hCurWnd,
nil),GetCurrentThreadId,True);

Application.ProcessMessages; // !!!

SetForegroundWindow(hWnd);

AttachThreadInput( GetWindowThreadProcessId(hCurWnd,
nil),GetCurrentThreadId, False);

Result := GetForegroundWindow=hWnd;

end;

--
| Daniel P. Stasinski | KareMor International, Inc.
| Software Engineer | 2401 South 24th Street
| Internet Services Dept. | Phoenix, AZ 85034
| dan...@karemor.com | http://www.karemor.com

Peter Below (TeamB)

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
In article <36DEDB2C...@mcmaster.ca>, David Beardwood wrote:
> SetForegroundWindow( WinHandle );
> if IsIconic( WinHandle ) then
> ShowWindow( Winhandle, SW_RESTORE );
>

You are bitten by the peculiar architecture of Delphi apps here. If
you minimize the main window of a Delphi app it does not minimize but
hides itself, the Application window is minimized instead. It is a bit
cumbersome to find the Application window for another process (since
they are all have the TApplication window class), so if one has control
over the code of the app i prefer to send a user message to the main
form found via FindWindow. This form than handles the message like
this:

If IsIconic(Application.Handle) Then
Application.Restore;
BringTofront;

The alternative is to use this

prog2ApplicationWnd := GetWindowLong( WinHandle, GWL_HWNDPARENT );
If IsIconic( prog2ApplicationWnd ) Then
PostMessage( prog2ApplicationWnd, WM_SYSCOMMAND, SC_RESTORE, 0 );


Peter Below (TeamB) 10011...@compuserve.com)
No replies in private e-mail, please, unless explicitely requested!


David Beardwood

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
thanks for the reply.


> We found that this code always work correctly:
> (tested on Win95 and NT 4.0)

well, you'd better test it again (Win95). it doesn't work on delphi apps,
though will work on other apps. other than the BringWindowToTop() call, it's
the same as my code. read Peter Below's response in this thread for excellent
insight.

thanks again,
dave

> procedure ShowRestoreWindow(hWnd: longint);
> begin
> if IsIconic(hWnd) then ShowWindow(hWnd, SW_RESTORE);
> SetForegroundWindow(hWnd);
> BringWindowToTop(hWnd);
> end;
>
> Regards,
>
> Dezso Meszaros
>


David Beardwood

unread,
Mar 5, 1999, 3:00:00 AM3/5/99
to
this works perfectly! thanks!i find that if the 2nd app is already restored,
but not activated this code doesn't make the app the active window so i still
need the SetForegroundWindow which is fine. it seems that passing
SetForegroundWindow the app handle or the window handle both work. does it
matter??

thanks again,
dave

Peter Below (TeamB)

unread,
Mar 6, 1999, 3:00:00 AM3/6/99
to
In article <36E00ABE...@mcmaster.ca>, David Beardwood wrote:
> it seems that passing
> SetForegroundWindow the app handle or the window handle both work. does it
> matter??
>
Probably not, bringing the Application window into foreground automatically
passes the buck to the main window.

Dezo (formerly TeamA)

unread,
Mar 8, 1999, 3:00:00 AM3/8/99
to
Hi David!

> > We found that this code always work correctly:
> > (tested on Win95 and NT 4.0)
>
> well, you'd better test it again (Win95). it doesn't work on delphi apps,
> though will work on other apps.

I never said that it good for apps, it good for a specific window as
the name "ShowRestoreWindow" shows

> other than the BringWindowToTop() call, it's
> the same as my code.

Yes, but we found this order is seems to be important.

Regards,
Dezso Meszaros

David Beardwood

unread,
Mar 9, 1999, 3:00:00 AM3/9/99
to
thanks a lot for the help. your explanations and code have been extremely
helpful.

thanks peter,
dave

0 new messages