The ShowWindow function can be used to show, hide, minimize or maximize a window.
1 2 3 |
Public Declare Function ShowWindow Lib "user32" _ (ByVal lHwnd As Long, _ ByVal lCmdShow As Long) As Boolean |
The lHwnd argument is the handle that you get using the FindWindow function. The lCmdShow argument is a Long that tells ShowWindow what to do. Some common numbers used for lCmdShow are:
Action | Value | Typical Constant Name |
---|---|---|
Hide | 0 | SW_HIDE |
Show | 5 | SW_SHOW |
Minimize | 2 | SW_MINIMIZE |
Maximize | 3 | SW_MAXIMIZE |
These examples show ShowWindow in action to hide and show the main Excel window, respectively.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
Sub HideMainXlWindow() Dim lHwnd As Long Const SW_HIDE As Long = 0 lHwnd = FindWindow("XLMAIN", Application.Caption) ShowWindow lHwnd, SW_HIDE End Sub Sub ShowMainXlWindow() Dim lHwnd As Long Const SW_SHOW As Long = 5 lHwnd = FindWindow("XLMAIN", vbNullString) ShowWindow lHwnd, SW_SHOW End Sub |
Private Declare Function ShowWindow Lib “user32” (ByVal hwnd As Long, ByVal nCmdShow As Long)
Private Sub NetName_Click()
Dim lHwnd As Long
If vncinstall = True And NetName.Caption “” Then
Shell “C:Program FilesRealVNCVNC4vncviewer “ & NetName.Caption, vbNormalFocus
Application.Wait (Now + TimeValue(“0:00:01”))
SendKeys “*****~”
Application.Wait (Now + TimeValue(“0:00:02”))
lHwnd = FindWindow(vbNullString, NetName.Caption)
If lHwnd 0 Then
ShowWindow lHwnd, 1
‘Every time it gets to here I get a Runtime Error 49
‘Bad DLL calling convention
Else
Beep
End If
End If
End Sub
Runtime Error 49 occurs if a dll isn’t called correctly. In this case, the ShowWindow function wasn’t declared correctly – The return value wasn’t specified: As Boolean