求教vb外部窗口调用问题

来源:百度知道 编辑:UC知道 时间:2024/05/17 07:22:52
我用vb的shell启动外部程序1.exe后hide自身窗口form1,10秒后form1.show.然后再次hide.
这时我想焦点落到1.exe.但实际上1.exe是最小化到任务栏无法获得焦点.求教如何解决?

'SHELL外部程序:
Shell "1.exe"
form1.hiden
lngHand=GetForegroundWindow() '获得1.EXE的窗口句柄

'十秒钟后:
form1.show
form1.hide
Call ShowWindow(ByVal lngHand, SW_SHOWNORMAL) '将1.EXE窗口还原显示
Call SetForegroundWindow(ByVal lngHand) '设置焦点

这里用到了GetForegroundWindow,ShowWindow,SetForegroundWindow三个API函数以及一个常量SW_SHOWNORMAL,我想你应该知道怎么调用吧。
Public Declare Function GetForegroundWindow Lib "user32" Alias "GetForegroundWindow" () As Long
Public Declare Function ShowWindow Lib "user32" Alias "ShowWindow" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function SetForegroundWindow Lib "user32" Alias "SetForegroundWindow" (ByVal hwnd As Long) As Long
Public Const SW_SHOWNORMAL = 1

首先需要得到 1.exe 创建的窗口句柄。lngWnd1,可以用FindWindow等方法查找到。

激活lngWnd1可以使用 SetForegroundWindow 激活焦点,如果是最小化的可以使用 ShowWindow 函数将其弹起。判断窗口的状态可