永发信息网

VB 如何获取外部程序的窗口位置

答案:2  悬赏:50  手机版
解决时间 2021-02-10 03:27
  • 提问者网友:像風在裏
  • 2021-02-09 05:19
外部程序可能是游戏之类的全屏程序
最好还要判断是否是全屏运行的
最佳答案
  • 五星知识达人网友:逐風
  • 2021-02-09 05:26
1 利用findwindow函数,通过窗体的标题(caption)来获取窗体的句柄
2 再利用SetWindowPos函数,利用窗体句柄来设置一个窗体的位置。。。

你可以把一个外部窗口移动摆放在任何位置。。。。。自然就知道它的位置了
全部回答
  • 1楼网友:往事埋风中
  • 2021-02-09 06:33
我有个实例:打开记事本+获得记事本的句柄+向记事本中写入字符串+更改记事本的标题+获得所有运行的程序的标题。代码如下: version 5.00 begin vb.form form1 caption = "form1" clientheight = 5325 clientleft = 60 clienttop = 450 clientwidth = 7560 linktopic = "form1" lockcontrols = -1 'true scaleheight = 5325 scalewidth = 7560 startupposition = 2 '屏幕中心 begin vb.listbox list1 height = 4380 left = 360 tabindex = 4 top = 480 width = 4095 end begin vb.commandbutton command4 caption = "获得所有程序标题" height = 495 left = 4800 tabindex = 3 top = 2400 width = 2535 end begin vb.commandbutton command3 caption = "打开写入记事本+改标题" height = 495 left = 4800 tabindex = 2 top = 1800 width = 2535 end begin vb.commandbutton command2 caption = "运行记事本" height = 495 left = 4800 tabindex = 1 top = 1200 width = 2535 end begin vb.commandbutton command1 caption = "打开写入记事本" height = 495 left = 4800 tabindex = 0 top = 600 width = 2535 end end attribute vb_name = "form1" attribute vb_globalnamespace = false attribute vb_creatable = false attribute vb_predeclaredid = true attribute vb_exposed = false '1 private declare function winexec lib "kernel32" (byval lpcmdline as string, byval ncmdshow as long) as long '========================================================== '3 private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as string, byval lpwindowname as string) as long private declare function findwindowex lib "user32" alias "findwindowexa" (byval hwnd1 as long, byval hwnd2 as long, byval lpsz1 as string, byval lpsz2 as string) as long private declare function sendmessage lib "user32" alias "sendmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) as long private const wm_paste = &h302 '=========================================================== '4 private declare function getdesktopwindow lib "user32" () 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 const gw_child = 5 private const gw_hwndnext = 2 ' private declare function setwindowtext lib "user32" alias "setwindowtexta" (byval hwnd as long, byval lpstring as string) as long private sub command1_click() '1 打开 写入 记事本 dim appid as integer appid = shell("c:\windows\notepad.exe", 1) appactivate appid sendkeys "-2.356e+012年后光荣感如果好了" '"sendkeys--字符有误,速度慢" end sub private sub command2_click() winexec "notepad.exe", 5 end sub private sub command3_click() '3 打开 写入 记事本+改标题 dim temphwnd as long appid = shell("c:\windows\notepad.exe", 1) temphwnd = findwindow("notepad", vbnullstring) '-------------------------------------------- setwindowtext temphwnd, "welcome to vb" ' '-------------------------------------------- temphwnd = findwindowex(temphwnd, 0, "edit", vbnullstring) if temphwnd then 'debug.print temphwnd vb.clipboard.settext "-2.356e+012年后光荣感如果好了" '"sendmessage--字符正确,速度快" sendmessage temphwnd, wm_paste, 0, byval 0& end if end sub private sub command4_click() '4 获得所有程序标题 dim ret as long dim hwnd as long dim str as string * 256 ret = getdesktopwindow() hwnd = getwindow(ret, gw_child) do while hwnd <> 0 getwindowtext hwnd, str, len(str) hwnd = getwindow(hwnd, gw_hwndnext) if left$(str, 1) <> vbnullchar then form1.list1.additem str end if loop end sub
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯