Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Visual Basic Programming Code Examples

Visual Basic > Applications VBA Code Examples

Bringing an application to the foreground and altering it's window state

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
Bringing an application to the foreground and altering it's window state If your application uses many processes eg. you shell Excel or Notepad, then it may often be useful to bring a different process (application) to the foreground and alter it's window state. The following code demonstrates two methods to achieve this. Option Explicit 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 Private Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long Private Declare Function SetWindowPlacement Lib "user32" (ByVal hwnd As Long, lpwndpl As WINDOWPLACEMENT) As Long Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long Public Const SW_MINIMIZE = 6, SW_NORMAL = 1, SW_MAXIMIZE = 3, SW_RESTORE = 9 '-------METHOD 1-------------- 'Purpose : Brings the specified application to foreground and alters it's window state. 'Inputs : [sFormCaption] The caption of the dialog to bring to the foreground. ' OR ' [lHwnd] The window handle of the dialog to bring to the foreground. ' [lWindowState] Specifies the windowstate of the application (defaults to normal) 'Outputs : Returns True on success 'Notes : Specify either the application caption or it's windows handle 'Revisions : Function AppToForeground(Optional sFormCaption As String, Optional lHwnd As Long, Optional lWindowState As Long = SW_NORMAL) As Boolean Dim tWinPlace As WINDOWPLACEMENT If lHwnd = 0 Then lHwnd = DialogGetHwnd(sFormCaption) End If If lHwnd Then tWinPlace.Length = Len(tWinPlace) 'Get the windows current placement Call GetWindowPlacement(lHwnd, tWinPlace) 'Set the windows placement tWinPlace.showCmd = lWindowState 'Change window state Call SetWindowPlacement(lHwnd, tWinPlace) 'Bring to foreground AppToForeground = SetForegroundWindow(lHwnd) End If End Function '-------METHOD 2-------------- 'Purpose : Brings the specified application to foreground and alters it's window state. 'Inputs : [sApplicationCaption] The application caption. ' [lHwnd] The handle to the application. ' [lWindowState] Specifies the windowstate of the application (defaults to normal) 'Outputs : Returns True on success 'Notes : Specify either the application caption or it's windows handle 'Revisions : Function AppToForeground2(Optional sApplicationCaption As String, Optional lHwnd As Long, Optional lWindowState As Long = SW_NORMAL) As Boolean If lHwnd = 0 Then lHwnd = DialogGetHwnd(sApplicationCaption) End If If lHwnd Then 'Alter window state AppToForeground2 = ShowWindow(lHwnd, lWindowState) 'Bring to foreground AppToForeground2 = SetForegroundWindow(lHwnd) End If End Function 'Demonstration routine, brings Excel to foreground and maximises it Sub Test() Dim lHwnd As Long 'Get Excel's handle lHwnd = DialogGetHwnd(, "XLMAIN") If lHwnd Then If AppToForeground(, lHwnd, SW_MAXIMIZE) Then MsgBox "Maximized Excel", vbSystemModal Else MsgBox "Failed to Maximised Excel", vbSystemModal End If Else MsgBox "Please open Excel before trying this demonstration" End If End Sub 'Purpose : Returns the Windows Handle of a Dialog based on its caption. 'Inputs : [sDialogCaption] The dialog caption. ' [sClassName] The class name of the dialog. If unknown, do not specify ' this parameter. 'Outputs : The Dialogs Window Handle 'Notes : Specify either a dialog caption or class name or both. 'Revisions : Function DialogGetHwnd(Optional ByVal sDialogCaption As String = vbNullString, Optional sClassName As String = vbNullString) As Long On Error Resume Next DialogGetHwnd = FindWindowA(sClassName, sDialogCaption) On Error GoTo 0 End Function