views:

247

answers:

2

On a DDE event my program loads a file in and should pop to the foreground. I was using

show;

It only seems to be working like then when i have hidden the window.

So i added

bringtofront;

Again this worked if it was hidden, closed, minimized or in the icon tray but not if it was open just under another window.

I even tried some hacky stuff like

windowstate:=wsminimized;
windowstate:=wsnormal;

to try force it to show, but to no avail. I know the code is running as i have printouts on either side of this block and there are no conditional statements that it could be stuck in.

+2  A: 

Did you try SetForegroundWindow and/or SetActiveWindow? Also see MSDN info on this subject.

devio
+1  A: 

SetForegroundWindow won't always work. In fact, these days it's quite unlikely to work, because applications aren't supposed to grab focus, they can only give it away. MS has been trying for years to prevent applications stealing the input focus. Read the comments on this blog entry from Raymond Chen for some history.

So, if you are the foreground app yourself, you can SetForegroundWindow to another app and everything will be peachy. If you're not the foreground app, chances are the only thing likely to happen is a taskbar button will start flashing.

Just to add some confusion, the precise behaviour is Windows version-dependent, so what happens depends on what breed of Windows you're running - they've been messing around with this for a long time :-).

Bob Moore
The program sending the DDE event needs to co-operate by calling AllowSetForegroundWindow. I expect Explorer would do that, but other programs probably neglect that detail.
Rob Kennedy
It's a good thing too. I really don't like when other programs grab the focus.
Kibbee