Ist zwar jetzt nicht sehr schön umgesetzt, aber funktioniert.
Mach dir ne Klasse CWrapper mit folgendem Code:
Public Class CWrapper
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As _
Integer, ByVal hWndNewParent As Integer) As Integer
Private Declare Function MoveWindow Lib "user32" (ByVal hwnd As Integer, _
ByVal X As Integer, ByVal y As Integer, ByVal nWidth As Integer, ByVal _
nHeight As Integer, ByVal bRepaint As Integer) As Integer
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Integer, _
ByVal nCmdShow As Integer) As Integer
Private Declare Function GetDeskTopWindow Lib "user32" Alias _
"GetDesktopWindow" () As Integer
Private nLhwnd As Integer
Private nRet As Integer
Private nProcess As Diagnostics.Process
Public Function CatchWindow(ByVal sFileName As String, ByVal oCatchingForm _
As Form, Optional ByVal nWaitForStartTimeout As Integer = 2500) As Boolean
Dim ltop As Integer
Dim lleft As Integer
Dim lwidth As Integer
Dim lheight As Integer
' Notepad starten
nProcess = Process.Start(sFileName)
' Warten bis vollstaendig geladen
Threading.Thread.Sleep(nWaitForStartTimeout)
' Handle des neuen Notepad-Fensters feststellen
nLhwnd = nProcess.MainWindowHandle.ToInt32
If nLhwnd <> 0 Then
' Jetzt wird das Fenster geklaut
Call SetParent(nLhwnd, oCatchingForm.Handle.ToInt32)
' Ausmasse festlegen (Scalemode von Form1 muss Pixel sein)
ltop = 0
lleft = 0
lwidth = oCatchingForm.Width
lheight = oCatchingForm.Height - ltop
' Neue Ausmasse setzen
Call MoveWindow(nLhwnd, lleft, ltop, lwidth, lheight, 1)
' Sichtbar machen
nRet = ShowWindow(nLhwnd, 1)
End If
End Function
Public Function ReleaseWindow() As Boolean
Dim dh As Integer
' Handle des Desktop bestimmen
dh = GetDeskTopWindow()
' Programm wieder in den Desktop setzen.
Call SetParent(nLhwnd, dh)
End Function
End Class In deiner Form kannste dann z.B. mit
Dim oWrapper as new CWrapper und den folgenden 2 Befehlen
oWrapper.CatchWindow("DateinameinklPfad.exe", Me)
oWrapper.ReleaseWindow eine Anwendung einfangen und wieder freigeben.
Wenn du nicht willst, das das Fenster automatisch an die Formgröße angepasst wird, dann Klammer einfache die Zeile Call MoveWindow(nLhwnd, lleft, ltop, lwidth, lheight, 1) in der Klasse aus.
Was man noch besser machen könnte, das man wirklich darauf wartet bis eine Anwendung komplett geladen ist und nicht einfach einen Sleep einbaut.
Denn die Anwendung muss komplett geladen sein, damit man sie einfangen kann.
Gruß,
Christian |