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

VB6 Restore Minimized Window - External Application

722 views
Skip to first unread message

Andrew Dunn

unread,
Sep 30, 2004, 12:02:02 PM9/30/04
to
Hi Group

I am trying to use a 'navbar' to allow easy access to a series of programs.

The intention when an user clicks one of the buttons on the NavBar is to
enumerate windows to see if the required exe is already running, if it is,
then I want to restore the window of that exe back to normal. If the program
is not running then it will be started.

If the required window is not minimised then my API code works fine - the
screen is brought to the front :

'
WinPlace.Length = Len(WinPlace)
lzRet = GetWindowPlacement(pzHwnd, WinPlace)
'
lzLeft = WinPlace.rcNormalPosition.Left
lzTop = WinPlace.rcNormalPosition.Top
lzRight = WinPlace.rcNormalPosition.Right
lzBottom = WinPlace.rcNormalPosition.Bottom
lzWidth = lzRight - lzLeft
lzHeight = lzBottom - lzTop
'
'
lzFlags = SWP_SHOWWINDOW Or SWP_NOSIZE
'
lzResult = SetWindowPos(pzHwnd, HWND_TOP, lzLeft, lzTop, lzWidth,
lzHeight, lzFlags)
lzResult = SetForegroundWindow(pzHwnd)
'
My problem is that this has no effect if the window has been minimised. I
have searched Google and have not found a working solution.

My current attempts are based on variations of the following :

'
If WinPlace.showCmd = SW_SHOWMINIMIZED Then
WinPlace.Length = Len(WinPlace)
WinPlace.flags = 0
WinPlace.showCmd = SW_SHOWNORMAL
Call SetWindowPlacement(pzHwnd, WinPlace)
End If
'
' OR - HAVE ALSO TRIED
'
WinPlace.Length = Len(WinPlace)
WinPlace.showCmd = SW_RESTORE
Call SetWindowPlacement(pzHwnd, WinPlace)
'
'
NOTE : The test for WinPlace.ShowCMD returns 1 = Normal and never returns 2
= Minimized

If this is possible can someone please advise - If it is not possible,
please put me out of my misery !

tia

Andrew

Andrew Dunn

unread,
Sep 30, 2004, 12:09:09 PM9/30/04
to
Just a point which might make a difference ?

When I enumerate the windows to get the required handle, I am finding a
handle based on the application.Title as opposed to the actual form caption.
The title is known whereas the caption varies and is not known.

The applicaton.title will return the handle for a visible, parent window of
type ThunderRT6Main
The form.caption will return the handle for a visible, parent window of type
ThunderRT6FormDC

Is this a issue ?


"Andrew Dunn" <and...@cclXXXmenzies.com> wrote in message
news:415c2e55$1...@127.0.0.1...

Nayan

unread,
Sep 30, 2004, 12:47:03 PM9/30/04
to
Hi Andrew,

This is a well known issue of SetForegroundWindow API. Please visit the
following like for the solution to your problem.

http://binaryworld.net/Main/CodeDetail.aspx?CodeId=3666

Here is the link for a huge API collection with examples.
http://binaryworld.net/main/api.aspx

Let me know if you still have the same problem

Tom Esh

unread,
Sep 30, 2004, 1:04:53 PM9/30/04
to
On Thu, 30 Sep 2004 17:02:02 +0100, "Andrew Dunn"
<and...@cclXXXmenzies.com> wrote:

>I am trying to use a 'navbar' to allow easy access to a series of programs.
>
>The intention when an user clicks one of the buttons on the NavBar is to
>enumerate windows to see if the required exe is already running, if it is,
>then I want to restore the window of that exe back to normal. If the program
>is not running then it will be started.
>
>If the required window is not minimised then my API code works fine - the
>screen is brought to the front :
>
>'
> WinPlace.Length = Len(WinPlace)
> lzRet = GetWindowPlacement(pzHwnd, WinPlace)
>'
> lzLeft = WinPlace.rcNormalPosition.Left
> lzTop = WinPlace.rcNormalPosition.Top
> lzRight = WinPlace.rcNormalPosition.Right
> lzBottom = WinPlace.rcNormalPosition.Bottom
> lzWidth = lzRight - lzLeft
> lzHeight = lzBottom - lzTop
>'
>'
> lzFlags = SWP_SHOWWINDOW Or SWP_NOSIZE
>'
> lzResult = SetWindowPos(pzHwnd, HWND_TOP, lzLeft, lzTop, lzWidth,
>lzHeight, lzFlags)
> lzResult = SetForegroundWindow(pzHwnd)
>'
>My problem is that this has no effect if the window has been minimised. I
>have searched Google and have not found a working solution.

You really don't need to muck with SetWindowPlacement here at all.
ShowWindow should do just fine:
If IsIconic(pzHwnd) <> 0 Then 'minimized
ShowWindow pzHwnd, SW_RESTORE
Else
ShowWindow pzHwnd, SW_ SHOWNORMAL
End If

The only time I've found Get/SetWindowPlacement useful is on app
shutdown or startup to save or set the normal/restored position and
size when the window is minimized or maximized.


-Tom
MVP - Visual Basic
(please post replies to the newsgroup)

Andrew Dunn

unread,
Oct 1, 2004, 5:15:46 AM10/1/04
to
Thanks but still no luck

The IsIconic call returns 0 regardless of the window state - This appears to
be a problem other users have had.

Strange that such obvious functionallity is proving so difficult to provide
:(

I have also followed up the posting made by Nayan, however that does not
work any better.


"Tom Esh" <tjeshGi...@earthlink.net> wrote in message
news:74fol0loo8qsnqj38...@4ax.com...

Andrew Dunn

unread,
Oct 1, 2004, 5:18:25 AM10/1/04
to
Thanks Nayan - but I still cannot restore a mimimized window

The example (following the link) is for a program to attach its own thread
to the thread of the foreground window. I need to use Program A (my NavBar)
to either run or bring to the front Program B (any of the options on the
NavBar)


"Nayan" <nayan_s...@yahoo.com> wrote in message
news:317866ED-E53F-4F83...@microsoft.com...

Andrew Dunn

unread,
Oct 1, 2004, 6:13:01 AM10/1/04
to
Scrap that - it doesn't work !!

I hadn't noticed that my program was failing to find the minimised window
and was actually firing a new copy of the exe each time

"Andrew Dunn" <and...@cclXXXmenzies.com> wrote in message

news:415d27fa$1...@127.0.0.1...
> My problem is because I am using the wrong window handle.
>
> If I find the handle using the form.caption then code can bring that
window
> to the front.
> If I find the handle for the application.title then the window cannot be
> brought to the front
> '
> Once I have established the handle based on the form.caption, the
following
> code works fine.


> '
> '
> lzFlags = SWP_SHOWWINDOW Or SWP_NOSIZE
> '
> lzResult = SetWindowPos(pzHwnd, HWND_TOP, lzLeft, lzTop, lzWidth,
> lzHeight, lzFlags)
> lzResult = SetForegroundWindow(pzHwnd)
> '
>

> I will look around to try and find out, my problem now is that the form
> caption varies all the time whereas I know the application that I want to
> run / bring to the front. Similar to a problem with AppActivate :
>
> How do I get the handle of the window for the form.caption when I only
know
> the handle of the application ?


>
>
>
>
> "Andrew Dunn" <and...@cclXXXmenzies.com> wrote in message

> news:415c3001$1...@127.0.0.1...

Andrew Dunn

unread,
Oct 1, 2004, 6:57:13 AM10/1/04
to
I have a sort of working solution

Having established the handle, I just need to do the following :

'
If isIconic(pzHwnd) = 1 Then
Call ShowWindow(pzHwnd, SW_RESTORE)
End If
Call SetForegroundWindow(pzHwnd)
lzBool = (GetForegroundWindow = pzHwnd)
Exit Sub
'
** Thanks to ' Copyright ©1998-2000 Karl E. Peterson - All Rights
Reserved, http://www.mvps.org/vb **
'
Examined code used in AltTab.zip
'
'---------------------------------------------------------------------------
--------------------------
'
I still have a problem because the handle has to be that of the form - i.e.
I am finding the handle by enumming windows, looking for the form.caption.

However I do not know what the caption will be !!! - I only know the
application that needs to be brought to the front.
'
'


Tom Esh

unread,
Oct 1, 2004, 12:28:42 PM10/1/04
to
On Fri, 1 Oct 2004 11:57:13 +0100, "Andrew Dunn"
<and...@cclXXXmenzies.com> wrote:
>...

>I still have a problem because the handle has to be that of the form - i.e.
>I am finding the handle by enumming windows, looking for the form.caption.
>
>However I do not know what the caption will be !!! - I only know the
>application that needs to be brought to the front.

It's not clear exactly what you mean by "know the application". What
do you know about the app? Exename, proc ID, part of the titlebar
text, etc?

Maybe have a look at the "FindPart" example at the same site you
visited earlier: http://www.mvps.org/vb/index2.html

Andrew Dunn

unread,
Oct 5, 2004, 5:51:38 AM10/5/04
to
Tom

I know the EXE name of the application that I want to call. I have no method
of predicting what the form.caption will be.

I have worked around this problem for now by amending the applications that
will be called so that they write their current caption to a registry key.
My calling program now uses the EXE name to retreive the registry key and
then I can find my window handle.

Knowing the EXE name and knowing that only one copy of the EXE will be
running, given that I can identify the process, is there anyway of finding
out the caption ?

I will look at the FindPart example - though I suspect that I already have
most of the code - i.e. I can already find a partial caption.

Regards

Andrew

"Tom Esh" <tjeshGi...@earthlink.net> wrote in message

news:nb1rl05e1ncg48sir...@4ax.com...

Tom Esh

unread,
Oct 5, 2004, 7:58:02 AM10/5/04
to
On Tue, 5 Oct 2004 10:51:38 +0100, "Andrew Dunn"
<and...@cclXXXmenzies.com> wrote:

>
>I know the EXE name of the application that I want to call. I have no method
>of predicting what the form.caption will be.
>
>I have worked around this problem for now by amending the applications that
>will be called so that they write their current caption to a registry key.
>My calling program now uses the EXE name to retreive the registry key and
>then I can find my window handle.
>
>Knowing the EXE name and knowing that only one copy of the EXE will be
>running, given that I can identify the process, is there anyway of finding
>out the caption ?
>
>I will look at the FindPart example - though I suspect that I already have
>most of the code - i.e. I can already find a partial caption.

Offhand the only way I know would be to enumerate the running
processes with...
CreateToolhelp32Snapshot
Process32First
Process32Next
...until you find one with the correct exename. Stash the process ID
from that, then enumerate the top-level windows with EnumWindows until
you get one who's PID matches.

Of course if your app is launching the exe, VB's shell or several Api
methods can provide the PID at that time, and you could skip the
process enumeration part.

0 new messages