Bir Programı Form Sınırları İçerisinde Çalıştırmak
(General)(Declaration)
Option Explicit
Private Const GW_HWNDNEXT = 2
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private old_parent As Long
Private child_hwnd As Long
Private Function InstanceToWnd(ByVal target_pid As Long) As Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
Do While test_hwnd <> 0
If GetParent(test_hwnd) = 0 Then
test_thread_id = GetWindowThreadProcessId(test_hwnd, test_pid)
If test_pid = target_pid Then
InstanceToWnd = test_hwnd
Exit Do
End If
End If
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub Form_Resize()
Dim hgt As Single
hgt = ScaleHeight - Picture1.Top
If hgt < 120 Then hgt = 120
Picture1.Move 0, Picture1.Top, ScaleWidth, hgt
End Sub
Private Sub Command2_Click() ' Program serbest bırakılır form dışına çıkar
SetParent child_hwnd, old_parent
Command1.Enabled = True
Command2.Enabled = False
End Sub
Private Sub Command1_Click() ' Program form içerisinde çalıştırır
Dim pid As Long
Dim buf As String
Dim buf_len As Long
pid = Shell("notepad.exe", vbNormalFocus) ‘ NotePad çalıştırılıyor
If pid = 0 Then
MsgBox "Hatalı İşlem"
Exit Sub
End If
child_hwnd = InstanceToWnd(pid)
old_parent = SetParent(child_hwnd, Picture1.hwnd)
Command1.Enabled = False
Command2.Enabled = True
End Sub