Maximize a minimized application

Posted on 2001-08-03
Last Modified: 2007-12-19
I want to be able to maximize and bring focus to a minimized application already running.

How would I do that?
Question by:CArnold

Expert Comment

ID: 6350431
Here is a shortcut to a project that does what you want

Here is the site
LVL 17

Expert Comment

by:Suat Ozgur
ID: 6350438
You should find its hwnd first. You can use the caption or a little part of the caption, or just a specific text in the caption to use it with FindWindow API function. Than you can do lots of thing with its hwnd. I am sorry because i have no vb in this computer so i dont want to drive you through the wrong way with codes i remembered. Actually visit and look for the FindWindow API (also some others to get hwnd and activating it). It is the good way to do what you need.

Good luck


Expert Comment

ID: 6350551
this will find an application already running, you can state what the window api is if you wish and maximize it and return focus to it,

i believe this should do the trick

'Windows API/Global Declarations for :De
'     termin if Another Program is running Bas
'     ed on Portion of Window Caption
'API functions

Private Declare Function BringWindowToTop Lib "user32" (ByVal hwnd As Long) As Long

Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Const MODULE As String = "modWindowsAPI"
    Private Const WPF_RESTORETOMAXIMIZED As Long = &H2
    Private Const SW_SHOWMAXIMIZED As Long = 3
    Private Const HWND_TOP As Long = 0
    Private Const SWP_SHOWWINDOW As Long = &H40

Private Type POINTAPI
    x As Long
    y As Long
    End Type

Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type

    Length As Long
    flags As Long
    showCmd As Long
    ptMinPosition As POINTAPI
    ptMaxPosition As POINTAPI
    rcNormalPosition As RECT
    End Type

' Name: Determin if Another Program is r
'     unning Based on Portion of Window Captio
'     n
' Description:This piece of code will lo
'     ok at each of the windows and return eac
'     h windows caption.
If you know the window / program you are looking for based on this caption this code will ring it to the front of the Z order and maximize it, even if the application is minimized.
If the application is not currently running you can then call the shell() command to launch the needed application at that time.
' By: Aaron Ralls
' Inputs:The only input would be the str
'     ing that you are looking for in the desi
'     red caption of the application you are i
'     n search for.
APPLCATION_CAPTION = "the text you are looking for"
I do an instr() to see if this string is inside of the forms caption.
' Returns:EnumAllWindows returns a TRUE
'     if the Application/window was never foun
'     d and a FALSE if it was found.
'Assumes:I'm currently working on a way
'     to convert a string you are looking for
'     to a long to be placed as the second par
'     amter of the EnumWindows and EnumWindows
'     Proc (the call back function).
'Side Effects:none that I know of. If an
'     y are found please contact me.
'This code is copyrighted and has limite
'     d warranties.
'Please see http://www.Planet-Source-Cod
'     vb/scripts/ShowCode.htm
'for details.

Private Const MODULE As String = "modWindowsAPI"
'NOTE this function must be in a bas mod
'     ule.
'the second parameter can be used for an
'     ything
'it is used in conjunction with the seco
'     nd parameter of EnumWindows
'ie you could change it to be byval List
'     View as ListView
'then the call back function could fill
'     that control.

Private Function EnumWindowsProc(ByVal hwnd As Long, ByVal NotUsed As Long) As Boolean
    'Callback function for 'EnumWindows'
    On Error GoTo ErrorHandler
    Const SUBROUTINE As String = "EnumWindowsProc"
    'change the name of the caption you are
    '     looking for.
    Const APPLCATION_CAPTION As String = "SPP Reserve"
    Const FIRST_POSITION As Long = 1
    Const STRING_FOUND As Long = 0
    Dim Text As String 'The text of the new item
    Dim WindowPlacementPosition As WINDOWPLACEMENT
    Dim MinPoint As POINTAPI
    Dim MaxPoint As POINTAPI
    Dim Rectangle As RECT
    Dim ReturnValue As Long
    Text = WindowText(hwnd)

        MinPoint.x = 0
        MinPoint.y = 0
        MaxPoint.x = 0
        MaxPoint.y = 0
        Rectangle.Bottom = Screen.Height
        Rectangle.Left = 0
        Rectangle.Right = Screen.Width
        Rectangle.Top = 0
        WindowPlacementPosition.Length = Len(WindowPlacementPosition)
        WindowPlacementPosition.flags = WPF_RESTORETOMAXIMIZED
        WindowPlacementPosition.showCmd = SW_SHOWMAXIMIZED
        WindowPlacementPosition.ptMinPosition = MinPoint
        WindowPlacementPosition.ptMaxPosition = MaxPoint
        WindowPlacementPosition.rcNormalPosition = Rectangle
        ReturnValue = SetWindowPlacement(hwnd, WindowPlacementPosition)
        ReturnValue = ShowWindow(hwnd, SW_SHOWMAXIMIZED)
        ReturnValue = SetWindowPos(hwnd, HWND_TOP, 0, 0, Screen.Width, Screen.Height, SWP_SHOWWINDOW)
        ReturnValue = SetForegroundWindow(hwnd)

EnumWindowsProc = False
    'True to continue receiving data

EnumWindowsProc = True
End If
Exit Function
Debug.Assert False
Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Function

Public Function EnumAllWindows() As Boolean

EnumAllWindows = False
    On Error GoTo ErrorHandler
    Const SUBROUTINE As String = "EnumAllWindows"

EnumAllWindows = EnumWindows(AddressOf EnumWindowsProc, 0)
    Exit Function
    Debug.Assert False
    Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Function

Private Function WindowText(ByVal hwnd As Long) As String
    On Error GoTo ErrorHandler
    Const SUBROUTINE As String = "WindowText"
    Const DEFAULT_BUFFER_SIZE As Long = 255
    Dim ReturnValue As Long'The return value of GetWindowText.
    'It return the length of the text withou
    '     t the ending
    'null zero (chr(0))
    Dim Caption As String 'The class name of the Window that has the handle 'hwnd'
    Caption = Space(DEFAULT_BUFFER_SIZE) 'To retrive the name, you need to create a buffer.
    ReturnValue = GetWindowText(hwnd, Caption, Len(Caption))
    WindowText = Left(Caption, ReturnValue) 'Return the text of the window without the ending zero
    Exit Function
    Debug.Assert False
    Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Function
'this is placed in a class or on a form
'this is a sample calling function.

Private Sub LoadContingencyProgram()
    On Error GoTo ErrorHandler
    Const SUBROUTINE As String = "LoadContingencyProgram"
    Const WINDOW_NOT_FOUND As Boolean = True

    If EnumAllWindows = WINDOW_NOT_FOUND Then
    End If
    Exit Sub
    Debug.Assert False
    Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Sub

hope it helps
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Expert Comment

ID: 6350872

Here's a file over the net for you:


Description: Search for a particular minimized program and maximize it (3K)

That's it!

glass cookie : )

Accepted Solution

glass_cookie earned 100 total points
ID: 6350873
Here's something 'extra' to the previous comment:


Description: Maximize another application (3K)

That's it!

glass cookie :)

Author Comment

ID: 6360972
Sorry it took so long to get back, guys.  I wanted to examine everyone's suggestions and codes to determine the best possible solution to my problem.
Some code worked well with non-VB apps, but were very unpredictable and inconsistent with compiled VB .exe's.
Other links took me to well written code, but code that was heck to weed out the necessary code from the unnecessary.
glass_cookie's last link provided clean simple code of what I needed.

Thank you, to everyone, for your responses.  All were excellent.

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question ( in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question