Jump to content
Sign in to follow this  
dummzeuch

preventing my program from stealing the focus

Recommended Posts

Normally, when a VCL program is started via double click or commandline it shows its main window in the foreground thereby taking the focus from the previously active window.

 

Is there any way how such a program can prevent that? I still want it to show the main window but the focus should stay with the previously active window.

I don't want my program to get into the foreground at all, not just move the window into the background or minimize it after a short time. No flickering, no interrupting the user from the work he is currently doing.

Edited by dummzeuch

Share this post


Link to post

I'd try to override the "show" process and call showwindow with SW_SHOWNOACTIVATE.

Maybe there is already something in VCL. 

Share this post


Link to post
3 hours ago, Attila Kovacs said:

I'd try to override the "show" process and call showwindow with SW_SHOWNOACTIVATE.

Maybe there is already something in VCL. 

As of Delphi 2007, there isn't.

But I found one option:

 

WindowState := wsMinimized;

Application.MainFormOnTaskbar := false;

 

Not quite what I am looking for, because the window is shown minimized rather than normal, but for now that's ok.

Share this post


Link to post

The simplest way, without having to reimplement a couple of things from the original CMShowingChanged is leaving the form on wsMinimized and then

calling a SW_SHOWNOACTIVATE:

private
    procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED;

procedure TForm3.CMShowingChanged(var Message: TMessage);
begin
   inherited;
   ShowWindow(Handle, SW_SHOWNOACTIVATE);
end;

This will also result a "restore" animation, if enabled.

 

  • Thanks 1

Share this post


Link to post

I ended up using the following code:

constructor Tf_MyForm.Create(_Owner: TComponent);
begin
  inherited;
  WindowState := wsMinimized;
  ShowWindow(Handle, SW_SHOWNOACTIVATE);
end;

So far it seems to work fine.

  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×