Imports System.Runtime.InteropServices Imports System.Threading Imports System.Text Public Class Form1 #Region "API Calls" _ Private Shared Function GetForegroundWindow() As IntPtr End Function _ Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr End Function _ Private Shared Function SetWindowText(ByVal hwnd As IntPtr, ByVal lpString As String) As Boolean End Function Private Declare Function SendMessageByInt Lib "user32.dll" Alias "SendMessageA" _ (ByVal hWnd As IntPtr, _ ByVal uMsg As Int32, _ ByVal wParam As Int32, _ ByVal lParam As Int32) As Int32 Private Declare Function SendMessageByString Lib "user32.dll" Alias "SendMessageA" _ (ByVal hWnd As IntPtr, _ ByVal uMsg As Int32, _ ByVal wParam As Int32, _ ByVal lParam As StringBuilder) As Int32 Public Const WM_SETFOCUS As Int32 = &H7 Public Const WM_GETTEXT As Int32 = &HD Public Const WM_GETTEXTLENGTH As Int32 = &HE Public Const WM_SETTEXT As Int32 = &HC Public Const WM_WINDOWPOSCHANGING As Int32 = &H46 Private Shared Function GetText(ByVal hwnd As IntPtr) As String If (Not hwnd.Equals(IntPtr.Zero)) Then Dim length As Int32 = SendMessageByInt(hwnd, WM_GETTEXTLENGTH, 0, 0) If length > 0 Then Dim SB As New StringBuilder(length) Dim Result As Int32 = SendMessageByString(hwnd, WM_GETTEXT, length + 1, SB) If Result <> 0 Then Return SB.ToString() Else Return String.Empty End If Else Return String.Empty End If Else Return String.Empty End If End Function _ Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer End Function _ Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer End Function Private Shared Function GetWindowTitle(ByVal hWnd As IntPtr) As String Dim length As Integer If hWnd.ToInt32 <= 0 Then Return "" End If length = GetWindowTextLength(hWnd) If length = 0 Then Return "" End If Dim sb As New System.Text.StringBuilder("", length + 1) GetWindowText(hWnd, sb, sb.Capacity) Return sb.ToString() End Function _ Public Structure RECT Public left As Integer Public top As Integer Public right As Integer Public bottom As Integer End Structure _ Private Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef rect As RECT) As IntPtr End Function _ Private Shared Function MoveWindow(ByVal hWnd As IntPtr, ByVal x As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal bRepaint As Boolean) As Boolean End Function #End Region #Region "Variables" Public IsRun As Boolean = False Public StoreLoadDialogRect As RECT Dim tWait As Thread #End Region #Region "Delegates" Delegate Sub StoreLoadRectDelegate(ByVal a As RECT) Public Sub StoreLoadRect(ByVal a As RECT) StoreLoadDialogRect = a End Sub Delegate Sub IsRunDelegate(ByVal a As Boolean) Public Sub IsRunSub(ByVal a As Boolean) IsRun = a End Sub Delegate Sub WriteTextDelegate(ByVal a As String) Public Sub WriteText(ByVal a As String) TextBox1.AppendText(a) End Sub #End Region #Region "Buttons" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Try tWait = New Thread(AddressOf WaitForDialog) tWait.Start(IsRun) If (OpenFileDialog1.ShowDialog) = DialogResult.OK Then TextBox1.AppendText(OpenFileDialog1.FileName.ToString & vbNewLine) End If If tWait.IsAlive Then tWait.Interrupt() End If Catch ex As Exception End Try End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Try tWait = New Thread(AddressOf WaitForDialog) tWait.Start(IsRun) If (OpenFileDialog2.ShowDialog) = DialogResult.OK Then TextBox1.AppendText(OpenFileDialog2.FileName.ToString & vbNewLine) End If If tWait.IsAlive Then tWait.Interrupt() End If Catch ex As Exception End Try End Sub #End Region #Region "Thread" Private Sub WaitForDialog(ByVal IsRun As Boolean) Try Dim rNew As RECT Dim rOld As RECT = StoreLoadDialogRect Dim hWnd As IntPtr = IntPtr.Zero Dim i As Integer = 0 While hWnd.Equals(IntPtr.Zero) And i < 100 Try Threading.Thread.Sleep(100) hWnd = GetForegroundWindow() Dim act_hwnd_title As String = GetWindowTitle(hWnd) If act_hwnd_title.Trim.ToLower.StartsWith("öffnen") Then If IsRun Then MoveWindow(hWnd, rOld.left, rOld.top, rOld.right - rOld.left, rOld.bottom - rOld.top, True) Me.Invoke(New WriteTextDelegate(AddressOf WriteText), New Object() {"move " & rOld.left & " " & rOld.top & " " & rOld.right & " " & rOld.bottom & vbNewLine}) Else Me.Invoke(New IsRunDelegate(AddressOf IsRunSub), New Object() {True}) GetWindowRect(hWnd, rNew) Me.Invoke(New StoreLoadRectDelegate(AddressOf StoreLoadRect), New Object() {rNew}) Me.Invoke(New WriteTextDelegate(AddressOf WriteText), New Object() {"init " & rNew.left & " " & rNew.top & " " & rNew.right & " " & rNew.bottom & vbNewLine}) End If Else hWnd = IntPtr.Zero End If Catch ex As Exception End Try i += 1 End While While (Not hWnd.Equals(IntPtr.Zero)) Try Dim act_hwnd_title As String = GetWindowTitle(hWnd) If act_hwnd_title = String.Empty Then hWnd = IntPtr.Zero Else Me.Invoke(New IsRunDelegate(AddressOf IsRunSub), New Object() {True}) GetWindowRect(hWnd, rNew) Me.Invoke(New StoreLoadRectDelegate(AddressOf StoreLoadRect), New Object() {rNew}) Me.Invoke(New WriteTextDelegate(AddressOf WriteText), New Object() {"new " & rNew.left & " " & rNew.top & " " & rNew.right & " " & rNew.bottom & vbNewLine}) End If Catch ex As Exception End Try Threading.Thread.Sleep(50) End While Catch ex As ThreadInterruptedException End Try End Sub #End Region End Class