Solved

Maximize a minimized application

Posted on 2001-08-03
6
356 Views
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?
0
Comment
Question by:CArnold
6 Comments
 
LVL 8

Expert Comment

by:Dave_Greene
ID: 6350431
Here is a shortcut to a project that does what you want
http://www.mvps.org/vb/code/TaskList.zip

Here is the site
http://www.mvps.org/vb/index2.html?tips/shellcpl.htm
0
 
LVL 17

Expert Comment

by:smozgur
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 vbapi.com 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
suat

0
 
LVL 3

Expert Comment

by:andysalih
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


Private Type WINDOWPLACEMENT
    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
'     e.com/xq/ASP/txtCodeId.8352/lngWId.1/qx/
'     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)


    If InStr(FIRST_POSITION, Text, APPLCATION_CAPTION, vbTextCompare) > STRING_FOUND Then
        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
Else
    'True to continue receiving data


EnumWindowsProc = True
End If
Exit Function
ErrorHandler:
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
    ErrorHandler:
    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
    ErrorHandler:
    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
        Call Shell(DEFAULT_CONTINGENCY_PROGRAM_LOCATION, vbNormalFocus)
    End If
    Exit Sub
    ErrorHandler:
    Debug.Assert False
    Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Sub

hope it helps
Andy
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 8

Expert Comment

by:glass_cookie
ID: 6350872
Hi!

Here's a file over the net for you:

Download...
http://www.vb-helper.com/HowTo/maxwind.zip

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

That's it!

glass cookie : )
0
 
LVL 8

Accepted Solution

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

Download...
http://www.vb-helper.com/HowTo/maxapp.zip

Description: Maximize another application (3K)

That's it!

glass cookie :)
0
 
LVL 1

Author Comment

by:CArnold
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.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now