Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi application form shows instead of hiding at startup

I have a program where it will not start minimized and shows a very small window on the dekstop.

Image: https://i.sstatic.net/aFc6o.jpg

Code:

program:

program Project4;

uses
  Forms,
  Unit4 in 'Unit4.pas' {Form4};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := false;
  Application.ShowMainForm:=false;
  Application.CreateForm(TForm4, Form4);
  Application.Run;
end.

unit:

unit Unit4;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, AppEvnts, ExtCtrls, Menus;

type
  TForm4 = class(TForm)
    TrayIcon1: TTrayIcon;
    ApplicationEvents1: TApplicationEvents;
    PopupMenu1: TPopupMenu;
    Exit1: TMenuItem;
    procedure TrayIcon1DblClick(Sender: TObject);
    procedure ApplicationEvents1Minimize(Sender: TObject);
    procedure ApplicationEvents1Restore(Sender: TObject);
    procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
  private
    { Private declarations }
    fCanClose: Boolean;
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation

{$R *.dfm}

procedure TForm4.ApplicationEvents1Minimize(Sender: TObject);
begin
  Hide();
  WindowState := wsMinimized;
end;

procedure TForm4.ApplicationEvents1Restore(Sender: TObject);
begin
  Show();
  WindowState := wsNormal;
  application.Bringtofront;
end;

procedure TForm4.Exit1Click(Sender: TObject);
begin
  fcanclose:=true;
  close;
end;

procedure TForm4.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  if not fCanClose then
    begin
      hide;
      windowstate:=wsminimized;
      CanClose:=false;
    end
      else
    CanCLose:=True;
end;

procedure TForm4.FormCreate(Sender: TObject);
begin
  fCanClose:=FALSE;
end;

procedure TForm4.TrayIcon1DblClick(Sender: TObject);
begin
  if (windowstate = wsminimized) then
    begin
      Show;
      windowstate := wsnormal;
      application.BringToFront;
    end
     else
    begin
      hide;
      windowstate:=wsminimized;
    end;
end;

end.
like image 246
Daniel Avatar asked Dec 08 '25 21:12

Daniel


1 Answers

I created your project and had the same problems until I changed the following line of code to True:

Application.MainFormOnTaskbar := True;

Now the app seems to work just fine without an minimizing to the bottom left corner of the desktop before it is hidden.

like image 143
James L. Avatar answered Dec 10 '25 17:12

James L.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!