There's a way you can prove to yourself the SetForegroundWindow call did work, and you can't do better. Check what hwnd GetForegroundWindow returns.
So do this:
Code:
DECLARE INTEGER SetForegroundWindow IN WIN32API INTEGER
DECLARE INTEGER GetForegroundWindow IN WIN32API
SetForegroundWindow(thisform.HWnd)
lnHWnd = GetForegroundWindow()
CLEAR DLLS SetForegroundWindow
CLEAR DLLS GetForegroundWindow
If Inlist(lnHWnd,thisform.HWnd,_screen.HWnd,_vfp.HWnd)
Messagebox("You set the foreground window.")
Endif
But, what happens even just a millisecond later is not in your control. And so finally, something else can have the foreground state anyway. It's not your programming fault, but you expect too much of setting the foreground window. It's not set forever. It's not even set until the user changes it. Any process can change it. So it's not in control of the user, or your code. At the start of Windows a lot of processes get started and one of them, by chance, not always the same, will be the last process and get the foreground state.
And the timespan for which Windows is chaotic about the state of all starting processes of the system can take quite a while, even a few minutes. The start of Windows was optimized since Windows 95 or XP to show the desktop earlier, even before many important system and systray processes still are starting up. Windows seems to already be there for you, the user, but if you start anything, even just notepad, it takes "forever" until it actually starts. Because there is still a lot of activity.
So, you have no control over it, you can't program something better, too.
But notice, this shows you a general idea if you doubt your code works, think there's something incomplete about it, you usually write code that verifies it, in this case using GetForegroundWindow to verify SetForegroundWindow. So also take that idea with you. You can answer your own question with this, often. Did my code do what I expect it to do. Well, verify it. By code.
And, by the way, I don't just check whether the foreground window handle returned by GetForegroundWindow is thisform.Hwnd, which you set. Because I know more about how Windows works internally, it may redirect the state of the foreground window to the main process window. So it may look for parent windows of the hwnd you specify. So in the end, not "thisform", but _screen becomes the foreground window. Or, as I also showed above in one post, _vfp becomes that, and you get _vfp.Hwnd returned. The foreground state is a state of a process more than of a window, indeed.
Chriss