VB 讓外部程式在VB程式的窗體裡面執行

2021-05-26 18:05:54 字數 2404 閱讀 3277

option explicit

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

private const gw_hwndnext = 2

private m_hwnd as long

private sub form_load()

dim dblpid as long

call lockwindowupdate(getdesktopwindow)

dblpid = shell("c:\windows\notepad.exe", vbnormalfocus)

m_hwnd = instancetownd(dblpid) '根據程序pid找視窗控制代碼

setparent m_hwnd, me.hwnd

putfocus m_hwnd                 '記事本設定焦點

call lockwindowupdate(0)

end sub

function instancetownd(byval target_pid as long) as long

dim i as long, lhwnd as long, lpid as long, lthreadid as long

lhwnd = findwindow(byval 0&, byval 0&)   '查詢第乙個視窗

do while lhwnd <> 0

i = i + 1

if i mod 20 = 0 then doevents

'判斷視窗是否沒父視窗

if getparent(lhwnd) = 0 then

'獲取該視窗的執行緒id

lthreadid = getwindowthreadprocessid(lhwnd, lpid)

if lpid = target_pid then '找到pid所在視窗控制代碼

instancetownd = lhwnd

exit do

end if

end if

'繼續查詢下乙個兄弟視窗

lhwnd = getwindow(lhwnd, gw_hwndnext)

debug.print hex$(lhwnd)

loop

end function

private sub form_unload(cancel as integer)

call destroywindow(m_hwnd)

'terminateprocess getcurrentprocess, 0    '野蠻了些

set form1 = nothing

end sub

vb 打包程式

visual basic 安裝程式製作 安裝程式製作是做專案必不可少的一道工序,網上的安裝軟體很多,可以用五花八門來開形容了 在此筆者介紹乙個最簡單的安裝方法,就是用 自帶的打包程式進行打包,雖然比較普通,不過內部卻有不少竅門,相信這一點知道的人可能不多吧!請大家一定看到最後,好戲在後頭 好了,不廢...

VB程式除錯

程式除錯就是對程式進行測試,查詢程式中的錯誤,並將這些錯誤修正或排除。一 在vb程式設計中有三類錯誤 第一類 語法錯誤 由於違反了語言 違反有關語句形式或使用規則而產生的錯誤。系統可以自動檢查,如 輸入非法字元,缺少括號等。第二類 執行錯誤 由於試圖執行乙個不可進行的操作而引起的錯誤,比如引用乙個不...

VB使用shell函式開啟外部exe程式的實現方法

本文例項主要實現了vb呼叫外部exe程式來執行的功能,這裡主要是使用shell函式來執行,shell函式主要用來開啟乙個外部的exe可執行檔案,例如,在sub模組內 shell notepad vbnor程式設計客棧malfocus,代表以正常模式執行記事本程式,vbnormalfocus是shel...