Search code examples
delphidelphi-xe2

How to bring my application to the front?


I know all the reasons why it is a bad idea. I dislike it if an application steals input focus, but this is for purely personal use and I want it to happen; it will not disturb anything.

(for the curious: I am running unit tests in NetBeans, which generate a log file. When my background application sees the log file's timestamp change I want it to parse the log file and come to the front to show the results).

This question did not help, nor did googling. It seems that BringToFront() hasn't worked for a long time and I can't find any alternative that does.

Any ideas?


Solution

  • You can't do this reliably. Windows XP had a workaround that would allow it, but versions since then prohibit it for the most part (see note below). This is expressly stated in the remarks section of the MSDN documentation on SetForegroundWindow:

    The system restricts which processes can set the foreground window. A process can set the foreground window only if one of the following conditions is true:

    • The process is the foreground process.
    • The process was started by the foreground process.
    • The process received the last input event.
    • There is no foreground process.
    • The foreground process is being debugged.
    • The foreground is not locked (see LockSetForegroundWindow).
    • The foreground lock time-out has expired (see SPI_GETFOREGROUNDLOCKTIMEOUT in SystemParametersInfo).
    • No menus are active.

    An application cannot force a window to the foreground while the user is working with another window. Instead, Windows flashes the taskbar button of the window to notify the user.

    Note the final paragraph of the quoted documentation, especially the first sentence.

    The problem with

    this is for purely personal use and I want it to happen; it will not disturb anything.

    is that any application could try to do the same thing. How does "purely personal use" tell it's you personally and not just any application? :-)

    Note: I've tried everything I can think of to get the IDE's documentation to show in the foreground when I click the help toolbutton, but all I can get is the flashing taskbar button (and I as the user want it to do so).