Neon Flash
Neon Flash

Reputation: 3233

Perl Tk Active Window

I have created a scheduled task on Windows 7 which runs a Batch File at a specified interval.

This batch file in turn invokes a Perl Script. Inside the Perl Script, based on the result, I use Perl Tk to display a Popup Message which gives an Alert.

Now, while I am doing some other task on my machine, like Browsing Internet, the Browser Window will be the active window for instance. In between, the scheduled task runs and the popup Window is not displayed, instead it is minimized and displayed on the Task Bar.

Please note that I am using, htstart.exe from ntwind to avoid displaying the Console Window when the Scheduled task runs (otherwise, taskeng.exe shows up everytime the scheduled task runs).

In order to make my Perl Tk window as the active window, I am creating the window as shown below:

$mw = MainWindow->new(-background => 'blue');
$mw->focus(-force);
$mw->geometry("200x200");
$mw->title("Message");

I have set the focus option so that this window becomes active.

Now, when the scheduled task runs, the only difference I observed after setting the focus option is that, the Tk Window appears in the Taskbar and it keeps blinking (probably because it is set as the active window now).

How do I make sure, that when it is displayed, it appears in the foreground and does not get minimized to the Task Bar?

Thanks.

Upvotes: 1

Views: 1419

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137807

You can't do this consistently on Windows. The issue is that the underlying call into the OS won't force the focus to be to a particular window in all cases. From that page:

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.

From a user perspective, this makes a bunch of sense. Having pop-up windows steal the focus from you is incredibly annoying, as it can cause misdirected keystrokes. But it does mean that what you were seeking to do won't work (and should not work) so you're going to have to put up with it.

Upvotes: 1

Related Questions