Jan Turoň
Jan Turoň

Reputation: 32922

Child process steals focus. How to get it back without bothering user?

I created child process from console application

CreateProcess(NULL, command, NULL, NULL, TRUE, 0, NULL, NULL, &in, &out);

Where

I tried to give the pictView some time and then kindly take the focus back

Sleep(1000);
SwitchToThisWindow(hwnd,TRUE);

I got the focus (the cursor is blinking again in my console), but the icon in the task bar flashes several times and the input is not allowed until I hit Alt+Tab.

I also tried to set the z-order, to launch the pictView minimized, even to suspend it, but he just doesn't want to give up easily. The only way how to get the focus back is to kill the pictView.

Is there any way how to get the focus back without bothering the user with Alt+Tab?

Note: there was no focus-stealing problem with simpView, but I'd like to stay with pictView for performance reasons.

Upvotes: 4

Views: 679

Answers (1)

Jan Turoň
Jan Turoň

Reputation: 32922

As Remy Lebeau points out, SetForegroundWindow() outlines the requirements for setting a foreground to the foreground. pictView played dirty, so I used dirty tricks too:

LockSetForegroundWindow(LSFW_LOCK);

executed from the console application prevented pictView to steal its focus. Everything run smoothly now.

Upvotes: 5

Related Questions