Imports System.Runtime.InteropServices
Module HandleWindow

    Dim name3 As String
    Dim name4 As String
    Dim n As Integer = 0
    Dim CreoWindowNameM As String
    Public Declare Function EnumWindows Lib "User32.dll" (ByVal WNDENUMPROC As EnumWindowDelegate, ByVal lparam As IntPtr) As Boolean
    Public Declare Auto Function GetWindowText Lib "User32.dll" (ByVal Hwnd As IntPtr, ByVal Txt As Byte(), ByVal Lng As Integer) As Integer
    Public Declare Function IsWindowVisible Lib "User32.dll" (ByVal hwnd As IntPtr) As Boolean
    Public Declare Function GetWindowTextLengthA Lib "User32.dll" (ByVal hwnd As IntPtr) As Integer
    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Public Declare Function SetWindowPos Lib "user32" (ByVal hwnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Public Const SWP_SHOWWINDOW = &H40
    Public Const SWP_NOMOVE = &H2
    Public Const SWP_NOSIZE = &H1
    Delegate Function EnumWindowDelegate(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean
    Public Callback As EnumWindowDelegate = New EnumWindowDelegate(AddressOf EnumWindowProc)

    Public Function FocusWindowM(name1 As String, name2 As String)
        name3 = name1
        name4 = name2

        Dim Result As String = HandleWindow.EnumWindows(Callback, IntPtr.Zero)
        n = n + 1
        FocusWindow()
        Return True

    End Function
    Public Function EnumWindowProc(ByVal hWnd As IntPtr, ByVal Lparam As IntPtr) As Boolean

        If IsWindowVisible(hWnd) Then
            Dim TheLength As Integer = GetWindowTextLengthA(hWnd)
            Dim TheReturn(TheLength * 2) As Byte '2x the size of the Max length
            GetWindowText(hWnd, TheReturn, TheLength + 1)
            Dim TheText As String = ""
            For x = 0 To (TheLength - 1) * 2
                If TheReturn(x) <> 0 Then
                    TheText &= Chr(TheReturn(x))
                End If
            Next

            If TheText.Contains(name3) And TheText.Contains(name4) Then

                CreoWindowNameM = TheText
            Else

            End If

        End If

        Return True

    End Function
    Public Sub FocusWindow()
        Dim dbHwnd As IntPtr = FindWindow(Nothing, CreoWindowNameM)

        Debug.Print(dbHwnd)
        If dbHwnd = 0 Then
            ' MsgBox("Can't find creo window")
        Else
            '   MsgBox("Creo window found")
            SetWindowPos(dbHwnd, 0, 0, 0, 0, 0, SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE)

        End If
    End Sub




End Module


