tags:

views:

124

answers:

3

Hey guys, I have a rather complex series of applications which depend on the ability to switch applications in the foreground.

My problem is, every 5 or 6 times of switching the applications in the foreground, it simply fails to bring the application forward. GetLastError does not report any issues. Often times I see the correct application flash in the foreground for a moment then the previous application is visible.

I have a Manager application which I have source for, it spawns and controls about 4 applications which I do not have source for. one of the applications it spawns/controls is also a manager which spawns/controls about 5 applications.

This is a sort of kiosk design so the user wont even have a keyboard or mouse, just a touch screen.

I have tried every combination of the Win32 calls to control them I am just out of ideas.

My first attempt was:

SetWindowPos(hApp, HWND_TOPMOST, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE);
SetWindowPos(hApp, HWND_NOTOPMOST, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE);

My second attempt was:

SetForegroundWindow(hApp);
SetActiveWindow(hApp);
SetFocus(hApp);

my third attempt: DWORD dwThreadID = GetWindowThreadProcessId(hApp, NULL); AttachThreadInput( dwThreadID, GetCurrentThreadId(), true);

SetForegroundWindow(hApp);
SetActiveWindow(hApp);
SetFocus(hApp);

AttachThreadInput( dwThreadID, GetCurrentThreadId(), false);

my forth attempt:

DWORD dwThreadID = GetWindowThreadProcessId(hApp, NULL);
AttachThreadInput( dwThreadID, GetCurrentThreadId(), true);

SetWindowPos(hApp, HWND_TOPMOST, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE);
SetWindowPos(hApp, HWND_NOTOPMOST, NULL, NULL, NULL, NULL, SWP_NOMOVE | SWP_NOSIZE);

SetForegroundWindow(hApp);
SetActiveWindow(hApp);
SetFocus(hApp);

 AttachThreadInput( dwThreadID, GetCurrentThreadId(), false);

I feel like I am missing an important gotcha when it comes to window switching. I know that only the foreground process can switch windows around but as my main Manager program is spawning and starting all the other processes which I need to control, I feel like it should be capable of moving these windows around. I am really under the gun to finish this (up all night all week), any suggestions or advice is greatly appreciated. Thank you!

A: 

Your AttachThreadInput() hack is (I think) a known way to defeat the focus stealing counter-measures in Windows. You are using the wrong handle though, you want to attach to the thread that currently has the focus. Which won't be hApp, you wouldn't need this code otherwise.

Use GetForegroundWindow() to get the handle to the window with the focus.

AttachThreadInput(
    GetWindowThreadProcessId(GetForegroundWindow(), NULL),
    GetCurrentThreadId(), TRUE
);

Although I think the 2nd argument needs to be thread ID of hApp. Because you don't want to shove your own window if I understood correctly. Not sure if that can work.

Hans Passant
This is definitely a hack that I am using as a last resort, I am unsure why I would need to attach to the Foreground process since everything in the Foreground is a child of my Manager application. Perhaps something I am doing is causing to lose focus. Thank you for identifying the error, I really hope this was the issue!
micheal blunning
I think that this mostly solved my problem, I still have an occasional issue but what ended up doing was something like: for i 0 to 3 and !done attach to thread set as top most set as top detach if getforegroundwindow = app done = true
micheal blunning
A: 

Try pushing the other application windows to the background first.

Also its a bit strange that you use SetWindowPos (SWP) to push a window to the foreground then push it out of the forgreound before using SetForegroundWindow to bring it back foward. Personally I've always used the SWP method without any issue ... but I've always pushed the other windows to the bottom as well.

Goz
Are you referring to the NOTOPMOST followed by TOPMOST?
micheal blunning
+1  A: 

We had a similar problem a couple of years ago. We could solve it by the following function call:

SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE);

Give it a try. See the documentation here.

Bill
I am setting ForegroundLocktimeout in the registry, is it the same thing?
micheal blunning
I have just called `SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, 0, SPIF_UPDATEINIFILE)` and it has set `HKEY_CURRENT_USER\Control Panel\Desktop\ForegroundLockTimeout` to zero.
Bill