3

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.

Deanna
  • 23,876
  • 7
  • 71
  • 156
Up_Up
  • 33
  • 1
  • 6
  • Similar to http://stackoverflow.com/questions/10832022/trouble-with-vb6-app-bringing-a-dialog-to-front-and-focus-on-windows-7 but not an exact duplicate – MarkJ Sep 25 '12 at 12:02

2 Answers2

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.

Community
  • 1
  • 1
MarkJ
  • 30,070
  • 5
  • 68
  • 111
0

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.

Community
  • 1
  • 1
Arvo
  • 10,349
  • 1
  • 31
  • 34
  • Hi, I did try using SetForegroundWindow API.But still the application just flashes and goes back to minimized state.I even tried SetFocus API, but no use. – Up_Up Sep 25 '12 at 07:53
  • 1
    Well, this is different issue then. If you write DoEvents (and optionally some wait, 1 second or so) before/after setting window state, does that change anything? Does it behave this way under debugger in W7/64? – Arvo Sep 25 '12 at 10:00
  • In Windows 7 and later (and Vista AFAICR) there are limitations in when an application is allowed to take the focus using SetForegroundWindow. As explained in the [documentation](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx). If you've shelled another application, and given it the focus, you've probably given up the ability to take the focus back. Windows doesn't allow just any old program to come to the front and grab the focus. – MarkJ Sep 25 '12 at 10:29
  • Hi Arvo, DoEvents did the work for me.But calling DoEvents and giving control to the operating system,does it cause any problem and while debugging in vb6 is there a way to skip DoEvents? – Up_Up Sep 26 '12 at 05:08
  • Should not have any difference, at least for your case. – Arvo Sep 26 '12 at 06:22