本例把「記事本」程式放進自己的窗體裡

2021-03-31 08:56:31 字數 2526 閱讀 8819

'本例把「記事本」程式放進自己的窗體裡

private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as long, byval lpwindowname as long) as long

private declare function getparent lib "user32" (byval hwnd as long) as long

private declare function setparent lib "user32" (byval hwndchild as long, byval hwndnewparent as long) as long

private declare function getwindowthreadprocessid lib "user32" (byval hwnd as long, lpdwprocessid as long) as long

private declare function getwindow lib "user32" (byval hwnd as long, byval wcmd as long) as long

private declare function lockwindowupdate lib "user32" (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, byval uexitcode 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 window

test_hwnd = getwindow(test_hwnd, gw_hwndnext)

loop

end function

private sub form_load()

dim pid as long

'lock the window update

lockwindowupdate getdesktopwindow

'execute notepad.exe

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

'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