Search code examples
delphifocuswindow

Delphi: Show window without activation


I struggle to show a second form above the main form without losing focus.

I have tried ShowWindow(second.handle, SW_SHOWNOACTIVATE), but the mainform loses focus. If I set Visible := false on the second window, the call to ShowWindow does not activate the second form, but the windows is empty when shown...

Does anyone have a good recipe for this?

UPDATE: What I'm trying to do, is showing a notify-window at a given event. It's crucial that the main form does not lose focus at any time.


Solution

  • There has to be something wrong with your code.

    I tested this code, it works:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
      ShowWindow(Form2.Handle, SW_SHOWNOACTIVATE);
      Form2.Visible := True;
    end;
    

    Be careful to use Visible, not Show ! Otherwise it'll override the SW_SHOWNOACTIVATE.