Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-04

Program without window but messages

14 messages · 7 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jürgen Hestermann

I wanted to write a program that has no window but may show messages to the user.
I have lots of such programs written with Virtual Pascal but I am struggling
to do the same with Free Pascal/Lazarus.

A simple test program would look like this:

--------------------------------
program Test;
{$mode objfpc}{$H+}
uses  Interfaces,Forms,Windows,SysUtils,Dialogs;
{$R *.res}
begin
ShowMessage('TEST');
end.
--------------------------------

When I create a new project and select to create an "Application", copy the above into the main program (ignoring Unit1.pas) and run the program I get an exception "Fensterklasse nicht gefunden" (in german, although I setup Lazarus in english!) and I am put into Win32WSControls.

When I try the same with a new "Program" and use the same test file (just omitting Interfaces and Forms units, because Lazarus cannot find it. Why? I did find it when creating an "Application") I cannot even compile it because I get:

project1.lpr(10,12) Error: Identifier not found "ShowMessage"

The wiki ( http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage ) says that this function is definied in Dialogs which I included in my uses clause. Still it cannot find it. Why is ShowMessage not available?

Creating a console program is not an option because it will create a console window which I don't need.

Anybody who knows why it is like that?

Howard Page-Clark
to Jürgen Hestermann
Jürgen Hestermann wrote 23 lines
I wanted to write a program that has no window but may show messages to the user. I have lots of such programs written with Virtual Pascal but I am struggling to do the same with Free Pascal/Lazarus. A simple test program would look like this: -------------------------------- program Test; {$mode objfpc}{$H+} uses Interfaces,Forms,Windows,SysUtils,Dialogs; {$R *.res} begin ShowMessage('TEST'); end. -------------------------------- Creating a console program is not an option because it will create a console window which I don't need. Anybody who knows why it is like that?

A GUI program requires a "main window" since that is the avenue for user interaction. You can hide that window (not "ignore" it) as in the following program, which compiles and runs. However, I can't myself see the use of such truncated GUI functionality. Once the main form is hidden, I see no way for the user ever to get back to it.

program Project1;
{$mode objfpc}{$H+}
uses
   Interfaces, Forms, Unit1, Dialogs;
{$R *.res}
begin
   Application.Initialize;
   Application.CreateForm(TForm1, Form1);
   Form1.Hide;
   ShowMessage('TEST');
   Form1.Close;
   Application.Run;
end.
JC Chu
to Jürgen Hestermann: I wanted to write a program that has no window but may show messages to the user. I have…

Try this.

Jürgen Hestermann wrote 45 lines
I wanted to write a program that has no window but may show messages to the user. I have lots of such programs written with Virtual Pascal but I am struggling to do the same with Free Pascal/Lazarus. A simple test program would look like this: -------------------------------- program Test; {$mode objfpc}{$H+} uses Interfaces,Forms,Windows,SysUtils,Dialogs; {$R *.res} begin ShowMessage('TEST'); end. -------------------------------- When I create a new project and select to create an "Application", copy the above into the main program (ignoring Unit1.pas) and run the program I get an exception "Fensterklasse nicht gefunden" (in german, although I setup Lazarus in english!) and I am put into Win32WSControls. When I try the same with a new "Program" and use the same test file (just omitting Interfaces and Forms units, because Lazarus cannot find it. Why? I did find it when creating an "Application") I cannot even compile it because I get: project1.lpr(10,12) Error: Identifier not found "ShowMessage" The wiki ( http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage ) says that this function is definied in Dialogs which I included in my uses clause. Still it cannot find it. Why is ShowMessage not available? Creating a console program is not an option because it will create a console window which I don't need. Anybody who knows why it is like that? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Best Regards, JC Chu
Sven Barth
to Jürgen Hestermann: I wanted to write a program that has no window but may show messages to the user. I have…
Jürgen Hestermann wrote 24 lines
I wanted to write a program that has no window but may show messages to the user. I have lots of such programs written with Virtual Pascal but I am struggling to do the same with Free Pascal/Lazarus. A simple test program would look like this: -------------------------------- program Test; {$mode objfpc}{$H+} uses Interfaces,Forms,Windows,SysUtils,Dialogs; {$R *.res} begin ShowMessage('TEST'); end. -------------------------------- When I create a new project and select to create an "Application", copy the above into the main program (ignoring Unit1.pas) and run the program I get an exception "Fensterklasse nicht gefunden" (in german, although I setup Lazarus in english!) and I am put into Win32WSControls.

This specific exception could be the result of a
SysErrorMessage(GetLastOSError) which on Windows uses the routines of Windows to translate the error and thus will be in the language your OS is setup for. (Note: this is a guess, but maybe a very valid one ;) )

Show quoted text 16 lines
When I try the same with a new "Program" and use the same test file (just omitting Interfaces and Forms units, because Lazarus cannot find it. Why? I did find it when creating an "Application") I cannot even compile it because I get: project1.lpr(10,12) Error: Identifier not found "ShowMessage" The wiki ( http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage ) says that this function is definied in Dialogs which I included in my uses clause. Still it cannot find it. Why is ShowMessage not available? Creating a console program is not an option because it will create a console window which I don't need. Anybody who knows why it is like that?

As you are using LCL functions it would be better if you ask such things on the Lazarus list.

Regards,
Sven

leledumbo
to Jürgen Hestermann: I wanted to write a program that has no window but may show messages to the user. I have…

You need to call Application.Initialize prior to anything if you want to use LCL features (including its dialogs). Your uses clause seems overkill. Only Interfaces, Forms and Dialogs units required for ShowMessage function.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Program-without-window-but-messages-tp5662570p5664268.html Sent from the Free Pascal - General mailing list .
Jürgen Hestermann
to Sven Barth: This specific exception could be the result of a SysErrorMessage(GetLastOSError) which on…
Show quoted text 6 lines
> When I create a new project and select to create an "Application", > copy the above into the main program (ignoring Unit1.pas) and run > the program I get an exception "Fensterklasse nicht gefunden" (in > german, although I setup Lazarus in english!) This specific exception could be the result of a

SysErrorMessage(GetLastOSError)

which on Windows uses the routines of Windows to translate the error

and thus

will be in the language your OS is setup for. (Note: this is a guess,

but maybe a very valid one ;) )

Yes, meanwhile this came to my mind too. It makes sense because my Windows is in german. ;-)

As you are using LCL functions it would be better if you ask such

things on the Lazarus list.

You are right. I will do so. Thanks for the hint.

Jürgen Hestermann
to leledumbo: You need to call Application.Initialize prior to anything if you want to use LCL features…
You need to call Application.Initialize prior to anything if you want

to use

LCL features (including its dialogs).

Is "ShowMessage" part of the LCL? I thought that it comes with Free Pascal directly
because the wiki
http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage , which explains this message, is located on wiki.freepascal.org. But if it's realy from the LCL then it makes sense that I cannot use this function without a form.
Still very missleading to explain LCL functions under a Free Pascal web site.
How should someone know where these functions come from?

Jürgen Hestermann
to JC Chu: Try this. JC Chu
Show quoted text 12 lines
Try this. program Test; {$MODE DELPHI} {$APPTYPE GUI} uses {$IF Defined(UNIX) and Defined(UseCThreads)}cthreads,{$ENDIF} Interfaces, Forms, Dialogs; begin Application.Initialize; ShowMessage('Test'); end.

Yes, this works!
Now I only need to understand why. ;-)

I was expecting the "application.initialize" to create a window so I omitted it.
What does the initialize routine?
Why is it needed?

Tomas Hajny
to Jürgen Hestermann: to use Is "ShowMessage" part of the LCL? I thought that it comes with Free Pascal directly
Jürgen Hestermann wrote 15 lines
leledumbo schrieb: > You need to call Application.Initialize prior to anything if you want to use > LCL features (including its dialogs). Is "ShowMessage" part of the LCL? I thought that it comes with Free Pascal directly because the wiki http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage , which explains this message, is located on wiki.freepascal.org. But if it's realy from the LCL then it makes sense that I cannot use this function without a form. Still very missleading to explain LCL functions under a Free Pascal web site. How should someone know where these functions come from?

The Wiki is common for both projects
(http://wiki.lazarus.freepascal.org/Dialog_Examples/de#ShowMessage shows the same article). Looking up the documentation or reference to the unit location for a particular function from your IDE (Lazarus in your case) is probably better source of information where it belongs to than the Wiki. In general, visual components are provided by LCL (unless talking about general GUI elements which are available in the respective interface units like unit Windows for Win32/Win64 or Xlib, etc.). Also the page mentioned above contains several references to LCL and Lazarus.

Tomas

Jürgen Hestermann
to Howard Page-Clark: A GUI program requires a "main window" since that is the avenue for user interaction. You…
A GUI program requires a "main window" since that is the avenue for

user interaction.

You can hide that window (not "ignore" it) as in the following

program, which compiles

and runs. However, I can't myself see the use of such truncated GUI

functionality.

Once the main form is hidden, I see no way for the user ever to get

back to it.

Show quoted text 12 lines
program Project1; {$mode objfpc}{$H+} uses Interfaces, Forms, Unit1, Dialogs; {$R *.res} begin Application.Initialize; Application.CreateForm(TForm1, Form1); Form1.Hide; ShowMessage('TEST'); Form1.Close; Application.Run; end.

I also thought about this but wanted to avoid any flickering window coming up on program start when it's created with CreateForm and directly hidden again afterwards.

JC Chu gave an example by omitting most of the above and only leaving:

begin
Application.Initialize;
ShowMessage('TEST');
end;

This works. Though I thought that ShowMessage is not part of the LCL so the 1.5 MB added to the program could be avoided. But okay, if it is from the LCL then I will use the Windows API messagebox function instead.

Thanks everybody for the hints!
I think I know now how to get on the road.

Jürgen Hestermann
to Jonas Maebe: http://wiki.freepascal.org is exactly the same as http://wiki.lazarus.freepascal.org.…
> Is "ShowMessage" part of the LCL? I thought that it comes with Free

Pascal directly

> because the wiki

http://wiki.freepascal.org/Dialog_Examples/de#ShowMessage , which

> explains this message, is located on wiki.freepascal.org. http://wiki.freepascal.org is exactly the same as

http://wiki.lazarus.freepascal.org. There is only one wiki for both projects.

That would be ok if it does not matter much where a function is located. But this seems to be very important so making readers believe ShowMessage comes with plain Free Pascal by explaining it under a Free Pascal link is not so good.

JC Chu
to Jürgen Hestermann: Yes, this works! Now I only need to understand why. ;-) I was expecting the…

As leledumbo has pointed out, you need to call Application.Initialize() before using LCL features. The code for TApplication.Initialize() shows that it initializes the currently active widget set, which is maintained by the Interfaces unit. ShowMessage() relies ultimately upon TWidgetSet.PromptUser(), and that is why TApplication.Initialize() is needed.

Jürgen Hestermann wrote 25 lines
JC Chu schrieb: > Try this. > program Test; > {$MODE DELPHI} > {$APPTYPE GUI} > uses > {$IF Defined(UNIX) and Defined(UseCThreads)}cthreads,{$ENDIF} > Interfaces, Forms, Dialogs; > > begin > Application.Initialize; > ShowMessage('Test'); > end. Yes, this works! Now I only need to understand why. ;-) I was expecting the "application.initialize" to create a window so I omitted it. What does the initialize routine? Why is it needed? _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Best Regards, JC Chu
Sven Barth
to Jürgen Hestermann: I wanted to write a program that has no window but may show messages to the user. I have…
Jürgen Hestermann wrote 19 lines
I wanted to write a program that has no window but may show messages to the user. I have lots of such programs written with Virtual Pascal but I am struggling to do the same with Free Pascal/Lazarus. A simple test program would look like this: -------------------------------- program Test; {$mode objfpc}{$H+} uses Interfaces,Forms,Windows,SysUtils,Dialogs; {$R *.res} begin ShowMessage('TEST'); end. --------------------------------

A different (maybe not completly serious ;) ) idea is the following: If you only want to show error messages and your application should only work on Windows then you can do the following:

=== example begin ===

program Test;
{$apptype gui} // only to illustrate that this is a GUI application
begin
   Writeln(StdErr, 'TEST'); // alternatively you can use StdOut as well
end.

=== example end ===

This way an error dialog that is created using basic Windows API calls is created which you can confirm using Ok. This only works on Windows and only if the application is compiled as a GUI application (which is by default the case in Lazarus). There is only one caveat: The dialog is not immediately displayed, but only if enough data has accumulated and Flush(StdErr) doesn't work either in that case to force the output... thus the following code will show one dialog which contains both written lines (as seperate lines) - that's why this idea isn't a really serious suggestion:

=== example begin ===

program winerrortest;
{$apptype gui}
begin
   Writeln(StdErr, 'Hello World');
   Flush(StdErr);
   Writeln(StdOut, 'Hello World 2');
   Flush(StdOut);
end.

=== example end ===

Regards,
Sven