Imports System.Runtime.InteropServices

Public Module ManageWindow


   <DllImport("user32.dll")>
   Public Function FindWindow(ByVal className As String, ByVal windowTitle As String) As IntPtr
   End Function
   <DllImport("user32.dll")>
   Private Function ShowWindow(ByVal hWnd As IntPtr, ByVal flags As ShowWindowEnum) As Boolean
   End Function
   <DllImport("user32.dll")>
   Private Function SetForegroundWindow(ByVal hwnd As IntPtr) As Integer
   End Function
   <DllImport("user32.dll")>
   Private Function GetWindowPlacement(ByVal hWnd As IntPtr, ByRef lpwndpl As Windowplacement) As Boolean
   End Function

   Private Enum ShowWindowEnum
      Hide = 0
      ShowNormal = 1
      ShowMinimized = 2
      ShowMaximized = 3
      Maximize = 3
      ShowNormalNoActivate = 4
      Show = 5
      Minimize = 6
      ShowMinNoActivate = 7
      ShowNoActivate = 8
      Restore = 9
      ShowDefault = 10
      ForceMinimized = 11
   End Enum

   Private Structure Windowplacement
      Public length As Integer
      Public flags As Integer
      Public showCmd As Integer
      Public ptMinPosition As System.Drawing.Point
      Public ptMaxPosition As System.Drawing.Point
      Public rcNormalPosition As System.Drawing.Rectangle
   End Structure

   Public Sub BringWindowToFront(wdwIntPtr As IntPtr)
      'Dim wdwIntPtr As IntPtr = FindWindow(Nothing, "0016 - EgtBEAMWALL")
      Dim placement As Windowplacement = New Windowplacement()
      GetWindowPlacement(wdwIntPtr, placement)
      ' se minimizzata
      If placement.showCmd = 2 Then
         ' riporto in vista non minimizzata
         ShowWindow(wdwIntPtr, ShowWindowEnum.Restore)
      End If
      ' porto avanti la finestra rendendola quella attiva
      SetForegroundWindow(wdwIntPtr)
   End Sub

End Module
