6

So yes, I find myself in the dubious position of implementing a SwitchToThisWindow call to force my window to the front. I agree, its not ideal, but its not always possible to argue against product "features" that others deem necessary.

Now, I consider SwitchToThisWindow to be a win over the AttachThreadInput hack to do a forced window switch as its less likely to deadlock, and should SwitchToThisWindow be removed, or cease to function I won't complain.

However, SwitchToThisWindow has the unfortunate side effect of pushing the current foreground window to the bottom of the z-order in addition to bringing the target window to the top when FALSE is passed for the fAltTab parameter, and not doing anything if TRUE is passed.

How can I avoid this 'push current active to z-bottom' behavior without resorting to AttachThreadInput?

Alternatively, MS can just remove AttachThreadInput as a viable workaround and I can just tell my manager that the impossible, is in fact, actually, impossible.

bluish
  • 26,356
  • 27
  • 122
  • 180
Chris Becke
  • 34,244
  • 12
  • 79
  • 148
  • I assume you can't simply use [`SetForegroundWindow`](http://msdn.microsoft.com/en-us/library/ms633539.aspx)? It's not clear exactly what product feature you need to implement, except to bring a window to the front. `SetForegroundWindow` even works for windows that are attached to another thread than the one you're currently on, but it does require your application to be currently in the foreground. I can't tell if that's a problem here. – Cody Gray - on strike Feb 02 '11 at 07:57
  • That is the problem: Coming to the foreground even when the user has a different app active. – Chris Becke Feb 02 '11 at 10:20
  • 1
    I see. You want to be one of *those* apps. No wonder you describe your position as "not ideal". Is there no wiggle room to do things the "right" way and show a notification icon or toast slider? Show your boss: "[Well-designed programs use the notification area appropriately, without being annoying or distracting.](http://msdn.microsoft.com/en-us/library/aa511448.aspx)" :-) – Cody Gray - on strike Feb 02 '11 at 11:49
  • he's read it. he doesn't buy it. In my favor I have convinced them to have it as an option that is ... perhaps not on by default. Basically, a competing package does this, and we have to compete, apparently on flaws as well as features :/ – Chris Becke Feb 02 '11 at 12:56

3 Answers3

6

I don't know if this helps, but the only way i found out to bring my window to top reliably is to make the following 2 calls:

ShowWindow(myhwnd, SW_MINIMIZE);
ShowWindow(myhwnd, SW_RESTORE);

Obviously these calls only should be made, when your window currently is not the topmost one in order to avoid flickering. But this also should not have the side effect of bringing the current front window to the bottom of the z order.

RED SOFT ADAIR
  • 12,032
  • 10
  • 54
  • 92
2

When passing fAltTab=FALSE you are actually emulating Alt+Esc. So you could reverse this z-order change with SetWindowPos and its hWndInsertAfter after the SwitchToThisWindow call, but then you are back in ugly hacky-land IMHO.

The question is, do you really need keyboard focus?

Let me suggest another alternative:

  1. If your window is minimized, restore it
  2. Set your window to be topmost, when your window is activated remove the style again.
  3. Call SetForegroundWindow to flash the taskbar button (Or FlashWindowEx)

This should avoid the scenario where a user is typing and ends up performing some action in your UI without even looking at the screen.

Edit:

HWND hwndFgnd=GetForegroundWindow();
SetWindowPos(hwnd,hwndFgnd,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE);
SetWindowPos(hwndFgnd,hwnd,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);

..will probably work if you don't want to set the topmost bit at any point (Even if your window is at the top of the z-order, you still can't legally get the focus with SetForegroundWindow)

bluish
  • 26,356
  • 27
  • 122
  • 180
Anders
  • 97,548
  • 12
  • 110
  • 164
0

This is a bad problem I faced too. See my solution here. It works both for Show() and ShowDialog().

Community
  • 1
  • 1
bluish
  • 26,356
  • 27
  • 122
  • 180