Mirror

Keep other forms visible while minimizing the main form (Views: 100)


Problem/Question/Abstract:

How to keep other forms visible while minimizing the main form

Answer:

Solve 1:

You need to disable Delphi's default minimization behaviour and take over yourself.

In the main forms FormCreate do this:


{ ... }
ShowWindow(Application.handle, SW_HIDE);
SetWindowLong(Application.handle, GWL_EXSTYLE, GetWindowLong(application.handle,
  GWL_EXSTYLE) and not WS_EX_APPWINDOW or WS_EX_TOOLWINDOW);
ShowWindow(Application.handle, SW_SHOW);
{ ... }

That removes the application button from the taskbar.

The main form gets a handler for the WM_SYSCOMMAND message:

{ ... }
private {form declaration}

procedure WMSyscommand(var msg: TWmSysCommand); message WM_SYSCOMMAND;

procedure TForm1.WMSyscommand(var msg: TWmSysCommand);
begin
  case (msg.cmdtype and $FFF0) of
    SC_MINIMIZE:
      begin
        ShowWindow(handle, SW_MINIMIZE);
        msg.result := 0;
      end;
    SC_RESTORE:
      begin
        ShowWindow(handle, SW_RESTORE)
          msg.result := 0;
      end;
  else
    inherited;
  end;
end;

This disables the default minimization behaviour.

To get a taskbar button for the form and have it minimize to the taskbar instead of to the desktop, override the CreateParams method of the main form and also of all the secondary forms:

{ in form declaration }

procedure CreateParams(var params: TCreateParams); override;

procedure TForm1.CreateParams(var params: TCreateParams);
begin
  inherited CreateParams(params);
  params.ExStyle := params.ExStyle or WS_EX_APPWINDOW;
end;


Solve 2:

You must override the CreateParams() method and add the following code:

procedure TForm2.CreateParams(var params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := params.ExStyle or WS_EX_APPWINDOW
end;

Something to keep in mind is that when you restore a minimized form, the entire application will be brought to the front If you don't want that to happen, do this:

procedure TForm3.CreateParams(var params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.ExStyle := params.ExStyle or WS_EX_APPWINDOW;
  Params.WndParent := GetDesktopWindow; {add this line}
end;

Now the forms will brought to the front independently. If you're launching forms from other form, you'll have to handle the paranting issues accordingly so that modal forms don't appear behind your other forms.

<< Back to main page