Restoring a window after waiting for a process to

2019-05-13 23:08发布

问题:

I am using VB6 in win7 64bit OS. This application is migrated from xp..

Me.WindowState = vbMinimized
WaitForProcess Shell(launchapp, vbNormalFocus)
Me.WindowState = vbNormal

Before launching the launchapp, my code minimizes the main application and will launch an exe. Once the exe is closed by the user, my main application has to come back from minimized state to normal. This works fine in xp, but in win 7 my main app which is minimized just flashes and goes back to minimized state again.

Any ideas?

Thanks.

回答1:

Windows 7 will not allow apps to grab the focus using SetForegroundWindow, as explained in the documentation. See the remarks.

One workaround is to temporarily AttachThreadInput to the thread that does have the focus, give yourself the focus, and then detach again. Karl E Peterson provides a drop-in module to do this here with accompanying magazine article.

Disclaimer: Windows guru Raymond Chen points out that this workaround can cause your program to stop responding in some circumstances. However I've never encountered these bugs myself. YMMV.



回答2:

We are using Win32 API function SetForegroundWindow to solve similar problems (some windows, esp out-of-process ones, will remain behind our main application window on W7).

Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) As Long

This is API declaration, search google for usage. Some info here: VBA interaction with Internet Explorer. If you need to control windows in different process, you need another API - AllowSetForegroundWindow - too.