Recent

Author Topic: Can't hide main window  (Read 5802 times)

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Can't hide main window
« on: January 09, 2021, 04:20:40 pm »
I'm trying to create a project in Windows where the main window is hidden. It's converted from Delphi where the usual advice worked. However, trying to replicate this in Lazarus gives me a visible window that looks like a console window except that it has my application icon, as shown. The obvious cause would be writing to the console but I've searched through my code for references to Write and Writeln followed immediately by (' and there are none.

Code: Pascal  [Select][+][-]
  1. My project source file ends as follows:
  2.  
  3. begin
  4.      Application.Initialize;
  5.      Application.ShowMainForm:= False;
  6.      Application.Title:= 'My Title';
  7.      Application.CreateForm(THSForm, HSForm);
  8.      Application.Run;
  9. end.
     

There is a lot of code in FormCreate that I tried commenting out completely (leaving just the header and Begin and End) but, after recompiling, the form still showed. I have no OnShow or OnActivate event. Has anybody any idea why the application would be visible despite this?

My application is a Windows GUI one and does not use the Console.
« Last Edit: January 09, 2021, 04:39:11 pm by TyneBridges »
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

dseligo

  • Hero Member
  • *****
  • Posts: 1417
Re: Can't hide main window
« Reply #1 on: January 09, 2021, 04:39:34 pm »
I just tried this to put this line "Application.ShowMainForm:=False;" in some test project and form isn't shown - Lazarus 2.0.10 on Windows. So I guess generally Lazarus is working fine.

Try to create new project, put "Application.ShowMainForm:=False;" in it and see if it works.

If it works, then make some other form main in your project - see if it is related to the main form.

You don't create anything else (forms, data modules) in your project source? Code you've shown between "begin" and "end." is all?
« Last Edit: January 09, 2021, 04:41:06 pm by dseligo »

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Can't hide main window
« Reply #2 on: January 09, 2021, 05:01:28 pm »
Thanks dseligo. Yes, the code I quoted is everything apart from the preamble below. I'll try your suggestion with a blank project. (DiscFrm referenced below is a disclaimer that I do show after the main form is initialised and, ironically, that isn't displaying correctly either - it shows only as a title bar; but I'll tackle that when I get this issue fixed.)

Code: Pascal  [Select][+][-]
  1. program Logr;
  2.  
  3. {$MODE Delphi}
  4.  
  5. uses
  6.   Forms, Interfaces,
  7.   hsave in 'hsave.pas' {HSForm},
  8.   discfrm in 'discfrm.pas' {Disclaimer},
  9.   Windows;
  10.  
  11. {$R *.res}  
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Can't hide main window
« Reply #3 on: January 09, 2021, 05:20:32 pm »
The blank project test worked. The application was invisible and could only be seen in Windows' task list, as I would expect.

Would my problem be related to having a tray icon? The following was in the code I had commented out but my original application window still showed even when this wasn't invoked.
Code: Pascal  [Select][+][-]
  1.      
  2. With TrayIconData do
  3.         begin
  4.             cbSize:= SizeOf(TrayIconData);
  5.             hWnd:= Handle; // Note: different from Delphi which was just Wnd
  6.             uID:= 0;
  7.             uFlags:= NIF_MESSAGE + NIF_ICON + NIF_TIP;
  8.             uCallBackMessage:= WM_ICONTRAY;
  9.             hIcon:= Application.Icon.Handle;
  10.             StrPCopy(szTip, Application.Title);
  11.         end;
  12.         Shell_NotifyIconW(NIM_ADD, @TrayIconData); //Note: needed amendment from Delphi
  13.         If Debugging then
  14.         begin
  15.             Writeln(DL, 'Added Tray Icon ' + DateTimeToStr(Now));
  16.             Flush(DL);
  17.         end;    

Maybe that's a red herring: I've just commented out the above again and recompiled, and my application still showed as in my original image. 

I don't think I can change my main form without messing up the project badly, since there's a lot of initialisation (opening of files behind the scenes) in the initial form's OnCreate event. %)

Any further ideas/help from anyone would be welcome. Thanks.
« Last Edit: January 09, 2021, 08:50:26 pm by TyneBridges »
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: Can't hide main window
« Reply #4 on: January 10, 2021, 11:30:07 am »
Any further ideas/help from anyone would be welcome. Thanks.

Is Project Settings -> Compiler Settings -> Configuration and Targets -> Win32-GUI-Application enabled in your project?

Alternatively you could display the value of the System.IsConsole variable in your application to see whether the RTL considered your application to be a console one.

TyneBridges

  • Full Member
  • ***
  • Posts: 150
    • Personal blog
Re: Can't hide main window
« Reply #5 on: January 10, 2021, 01:50:37 pm »
Is Project Settings -> Compiler Settings -> Configuration and Targets -> Win32-GUI-Application enabled in your project?

Alternatively you could display the value of the System.IsConsole variable in your application to see whether the RTL considered your application to be a console one.
Thanks PascalDragon! I have ticked this in the Project Settings and it seems to have fixed the problem.

I find this a confusing option. I'm using the 64 bit compiler and TargetOS is set to Win64 - will my application still be a true 64 bit one if "Win 32 GUI application" is selected"? I'll be scanning for running processes and need to include 64 bit ones in the list.
John H, north east England
Lover of the old Delphi, still inexperienced with FPC/Lazarus and not an instinctive programmer

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Can't hide main window
« Reply #6 on: January 10, 2021, 03:31:26 pm »
I find this a confusing option. I'm using the 64 bit compiler and TargetOS is set to Win64 - will my application still be a true 64 bit one if "Win 32 GUI application" is selected"?

Yeah, it's a bit confusing and you'll find some other places like this--probably a historical artifact. But don't worry: in this (and those other cases) "Win32" really means generic "Windows".
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5759
  • Compiler Developer
Re: Can't hide main window
« Reply #7 on: January 11, 2021, 02:09:34 pm »
I find this a confusing option. I'm using the 64 bit compiler and TargetOS is set to Win64 - will my application still be a true 64 bit one if "Win 32 GUI application" is selected"? I'll be scanning for running processes and need to include 64 bit ones in the list.

That option can be worded better (you can open a bug report for that if you want), but what the -WG option does is to change the subsystem type of the generated executable from Console to GUI, it doesn't change anything regarding the targeted OS. The same can be achieved by using {$apptype gui} in the main project file (see here). Console type applications always have a console associated with them so Windows starts one if it isn't started from a console while GUI applications (usually) have none, so they don't get a console. Delphi by default assumes that the application is a GUI application while for FPC the default is Console.

Ed78z

  • New Member
  • *
  • Posts: 35
Re: Can't hide main window
« Reply #8 on: September 28, 2023, 07:38:06 pm »
I just tried this to put this line "Application.ShowMainForm:=False;" in some test project and form isn't shown - Lazarus 2.0.10 on Windows. So I guess generally Lazarus is working fine.

Try to create new project, put "Application.ShowMainForm:=False;" in it and see if it works.

If it works, then make some other form main in your project - see if it is related to the main form.

You don't create anything else (forms, data modules) in your project source? Code you've shown between "begin" and "end." is all?


I tried this but didn't work for me.

Application.Initialize;
Application.ShowMainForm:=False;
Application.CreateForm(TForm1, Form1);

Any idea why this is not working? (Lazarus 2.2.6 on Win10 x64)

zeljko

  • Hero Member
  • *****
  • Posts: 1668
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Can't hide main window
« Reply #9 on: September 28, 2023, 08:03:00 pm »
Works ok here, Linux Mint 20.1 64bit, Qt4, Qt5 ,Qt6, gtk2. Does not work with gtk3 (shows main form), but probably such stuff isn't implemented in gtk3 yet.
All tested with trunk lazarus.

Ed78z

  • New Member
  • *
  • Posts: 35
Re: Can't hide main window
« Reply #10 on: September 28, 2023, 09:07:34 pm »
Works ok here, Linux Mint 20.1 64bit, Qt4, Qt5 ,Qt6, gtk2. Does not work with gtk3 (shows main form), but probably such stuff isn't implemented in gtk3 yet.
All tested with trunk lazarus.

I just solved the issue:

1)
  Application.CreateForm(TForm1, Form1);
  Application.ShowMainForm:=False;   <-- IN THIS LOCATION
  Application.Run;

2)
  procedure TForm1.FormCreate(Sender: TObject);
  begin
     Form1.Visible:=False;   <-- ADD THIS


With both 1&2 applied to the program, the program starts in hidden status!

zeljko

  • Hero Member
  • *****
  • Posts: 1668
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: Can't hide main window
« Reply #11 on: September 29, 2023, 08:16:40 am »
It's bug in win32 ws and should be reported.

 

TinyPortal © 2005-2018