CArnold
asked on
Maximize a minimized application
I want to be able to maximize and bring focus to a minimized application already running.
How would I do that?
How would I do that?
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
Good luck
suat
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.835 2/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.Le ngth = Len(WindowPlacementPositio n)
WindowPlacementPosition.fl ags = WPF_RESTORETOMAXIMIZED
WindowPlacementPosition.sh owCmd = SW_SHOWMAXIMIZED
WindowPlacementPosition.pt MinPositio n = MinPoint
WindowPlacementPosition.pt MaxPositio n = MaxPoint
WindowPlacementPosition.rc NormalPosi tion = 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_LO CATION, 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
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.835
' 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.Le
WindowPlacementPosition.fl
WindowPlacementPosition.sh
WindowPlacementPosition.pt
WindowPlacementPosition.pt
WindowPlacementPosition.rc
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)
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_
End If
Exit Sub
ErrorHandler:
Debug.Assert False
Call MsgBox("Error in Procedure " & SUBROUTINE & " of " & MODULE & Err.Description)
End Sub
hope it helps
Andy
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 : )
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 : )
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
http://www.mvps.org/vb/code/TaskList.zip
Here is the site
http://www.mvps.org/vb/index2.html?tips/shellcpl.htm