How to Activate a Window API to Show in VB6

When developing an application in Visual Basic 6, if you want to activate another Windows application or window, you can do so easily. If, however, you want to make that window appear to the front and be shown, it requires a lot more code that you may initially realize. This process is very useful as it allows you to interact with other programs on your computer. Note that you cannot use this process to launch another application that has been created in VB6.

Instructions

    • 1

      Open your VB6 script in your preferred VB editor.

    • 2

      Copy and paste the following code into the declarations code:
      Private Declare Function FindWindow Lib "user32" _
      Alias "FindWindowA" _
      (ByVal lpClassName As String, _
      ByVal lpWindowName As String) As Long

      Private Declare Function GetClassName Lib "user32" _
      Alias "GetClassNameA" _
      (ByVal hWnd As Long, _
      ByVal lpClassName As String, _
      ByVal nMaxCount As Long) As Long

    • 3

      Copy and paste the following code into the procedures section:
      Public Sub GetClassNameFromTitle()
      Dim sInput As String
      Dim hWnd As Long
      Dim lpClassName As String
      Dim nMaxCount As Long
      Dim lresult As Long
      ' pad the return buffer for GetClassName
      nMaxCount = 256
      lpClassName = Space(nMaxCount)
      ' Note: must be an exact match
      sInput = InputBox("Enter the exact window title:")
      ' No validation is done as this is a debug window utility
      hWnd = FindWindow(vbNullString, sInput)
      ' Get the class name of the window, again, no validation
      lresult = GetClassName(hWnd, lpClassName, nMaxCount)
      Debug.Print "Window: " & sInput
      Debug.Print "Class name: " & Left$(lpClassName, lresult)
      End Sub

    • 4

      Click "GetClassNameFromTitle" in the debug window and click "Run". This should display the class name of the window. This provides the basic structure for the process.

    • 5

      Add the following script if you want to include the process in a wrapper:
      Public Function fActivateWindowClass(psClassname As String) As Boolean
      Dim hWnd As Long
      hWnd = FindWindow(psClassname, vbNullString)
      If hWnd > 0 Then
      ' ShowWindow returns True if the window was previously hidden.
      ' I don't care so I use the sub style
      ' ShowWindow and SW_SHOW declared elsewhere
      ' SW_SHOW will display the window in its current size and position
      Call ShowWindow hWnd, SW_SHOW
      fActivateWindowClass = True
      Else
      ' FindWindow failed, return False
      fActivateWindowClass = False
      End If
      End Function

Related Searches:

References

Comments

Related Ads

Featured