Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bring my application to the front?

Tags:

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?

like image 602
Mawg says reinstate Monica Avatar asked Oct 18 '12 02:10

Mawg says reinstate Monica


People also ask

How do I bring all windows forward?

Press and hold Alt and start presssing Tab. Each time you press Tab the next window will open. When you release Alt, the current window will remain on the screen. The second option is to use Windows Logo Key+Tab to switch between windows.


2 Answers

Here's something simple that seems to work, tested with multiple boxes consisting of XP, Server2003, Vista, Server2008, W7. Test application ran with a standard (or admin) account, stole the input focus from notepad while writing in the foreground.

var   Input: TInput; begin   ZeroMemory(@Input, SizeOf(Input));   SendInput(1, Input, SizeOf(Input)); // don't send anyting actually to another app..   SetForegroundWindow(Handle); 

You can tweak it further f.i. for a minimized app or such if required.

like image 185
Sertac Akyuz Avatar answered Oct 14 '22 23:10

Sertac Akyuz


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).

like image 32
Ken White Avatar answered Oct 14 '22 22:10

Ken White