This example puts the "Notepad" program into its own form.

xiaoxiao2021-03-06  113

'This example puts the "Notepad" program into your own form.

Private Declare Function FindWindow Lib "User32" Alias ​​"FindWindowa" (Byval Lpwindown As Long) AS Long

Private Declare Function GetParent Lib "User32" (Byval Hwnd As Long) AS Long

Private Declare Function SetParent Lib "User32" (Byval HwndNewParent As Long) As long

Private Declare Function GetWindowThreadProcessid Lib "User32" (Byval HWND As Long) As Long

Private Declare Function GetWindow Lib "User32" (Byval Hwnd As Long) As long

Private Declare Function LockwindowUpdate Lib "User 32" (Byval Hwndlock As Long) AS LONG

Private Declare Function GetDesktopWindow Lib "User32" () AS Long

Private Declare Function DestroyWindow Lib "User32" (Byval HWnd As Long) As Long

Private Declare Function TerminateProcess LIB "Kernel32" (Byval HProcess As Long) AS Long

Private Declare Function GetCurrentProcess LIB "kernel32" () AS Long

Private Declare Function Putfocus LIB "User32" Alias ​​"setfocus" (Byval Hwnd As long) As long

Const GW_HWndNext = 2

DIM MWND AS Long

Function instancetownd (Byval Target_PID As Long) As long

DIM TEST_HWND As Long, Test_PID As Long, Test_thread_id As Long

'Find the first window

Test_hWnd = FindWindow (Byval 0 &, Byval 0 &)

Do While Test_hwnd <> 0

'Check if the window isn't a child

IF getparent (test_hwnd) = 0 THEN

'Get the window's thread

Test_thread_id = getWindowThreadProcessid (Test_hWnd, Test_PID)

If Test_PID = Target_PID THEN

Instancetownd = Test_hwnd

Exit do

END IF

END IF

'retrieve the next windowtest_hwnd = getWindow (test_hwnd, gw_hwndnext)

Loop

END FUNCTION

Private sub flow_load ()

DIM PID AS Long

'Lock The Window Update

LockWindowUpdate getDesktopWindow

'Execute Notepad.exe

PID = shell ("c: /windows/notepad.exe", vbnormalfocus)

If PID = 0 Then Msgbox "Error Starting THE APP"

'Retrieve the Handle of the Window

MWND = Instancetownd (PID)

'Set the notepad's parent

SetParent MWND, Me.hwnd

'Put The Focus on Notepad

PUTFOCUS MWND

'Unlock windowupdate

LockWindowUpdate False

End Sub

Private Sub Form_Unload (Cancel AS Integer)

'Unload notepad

DestroyWindow MWND

'End this Program

TerminateProcess GetCurrentProcess, 0

End Sub

转载请注明原文地址:https://www.9cbs.com/read-100766.html

New Post(0)