Windows programming tutorials for FPC

classic Classic list List threaded Threaded
31 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Windows programming tutorials for FPC

Zaaphod
I've been programming for decades with Pascal, starting with Turbo Pascal, and for a few years now with Freepascal, and even wrote really complicated console windows programs with Freepascal that do windows function calls... But now I find that I would like to write a few windows GUI programs,  and well... I'm clueless... I never learned windows GUI programming and don't have a clue about how it's done, it's always been faster and easier to just keep doing what I already understand, but now I have a few applications to write what would be much better suited to a native windows application, so,   I am wondering if there are any tutorials out there, hopefully specific to Freepascal and/or Lazarus.  I need really basic stuff like how to open a message box, or how to use windows file open, or save-as dialog boxes.. etc.. even a hello world tutorial would be helpful... ok, so ZERO windows programming experience here...   Any advice on where to start?
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Marcos Douglas B. Santos
On Fri, Nov 2, 2018 at 7:13 AM James <[hidden email]> wrote:
>
> I've been programming for decades with Pascal, starting with Turbo Pascal, and for a few years now with Freepascal, and even wrote really complicated console windows programs with Freepascal that do windows function calls... But now I find that I would like to write a few windows GUI programs,  and well... I'm clueless... I never learned windows GUI programming and don't have a clue about how it's done, it's always been faster and easier to just keep doing what I already understand, but now I have a few applications to write what would be much better suited to a native windows application, so,   I am wondering if there are any tutorials out there, hopefully specific to Freepascal and/or Lazarus.  I need really basic stuff like how to open a message box, or how to use windows file open, or save-as dialog boxes.. etc.. even a hello world tutorial would be helpful... ok, so ZERO windows programming experience here...   Any advice on where to start?
>

I've just search on Google:
https://www.win.tue.nl/~wstomv/edu/lazarus/dev_gui_app.html

Looks useful.

regards,
Marcos Douglas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Bo Berglund
In reply to this post by Zaaphod
On Fri, 2 Nov 2018 06:13:40 -0400, "James"
<[hidden email]> wrote:

>I am wondering if there are any tutorials out there, hopefully specific to Freepascal and/or Lazarus.
>I need really basic stuff like how to open a message box, or how to use windows file open, or save-as
>dialog boxes.. etc.. even a hello world tutorial would be helpful...
>ok, so ZERO windows programming experience here...   Any advice on where to start?

What did the programs you say you have written for so long do?
Seems hard to believe you have not encountered reading and writing
files etc.

Programming for Windows is basically no different than programming for
say Linux or any other operating system, the same code applies but the
compiler will translate to the proper syntax to call the operating
system without you having to bother....

In order to do graphic programming you should look at FPC sidekick
Lazarus, which handles Rapid Application Development by letting you
design your forms interactively. And it does not limit the target to
Windows, same code can be used for Linux and other operating systems.

Programming:

Message box:
ShowMessage('Message text to show');

File handling:
var
 F: file;
FileName: string;
begin
 FileName := <filename on disk>;
 AssignFile(F, Filename);
 Rewrite(F);
 Writeln(F, 'text to write');
 CloseFile(F);
end;

etc, etc, etc

--
Bo Berglund
Developer in Sweden

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Tomas Hajny-2
On Sat, November 3, 2018 00:00, Bo Berglund wrote:

> On Fri, 2 Nov 2018 06:13:40 -0400, "James"
> <[hidden email]> wrote:
>
>>I am wondering if there are any tutorials out there, hopefully specific
>> to Freepascal and/or Lazarus.
>>I need really basic stuff like how to open a message box, or how to use
>> windows file open, or save-as
>>dialog boxes.. etc.. even a hello world tutorial would be helpful...
>>ok, so ZERO windows programming experience here...   Any advice on where
>> to start?
>
> What did the programs you say you have written for so long do?
> Seems hard to believe you have not encountered reading and writing
> files etc.
 .
 .

The original poster mentioned not having used GUI dialog boxes for file
selection; I'm pretty sure reading and writing files is no problem for
him.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod

That is correct, I have only ever done console programming, but now I find I'm lost trying to do any kind of GUI programming.    I have a very simple program that works as a console application, but what I would like to do is have it use the Windows "Save AS' Dialog to allow the user to save the file using the windows GUI interface, so the user can navigate through directory structures and save the file.

 

I looked at a few tutorials and see how to make a form and put some buttons on it, but I'm still trying to figure out how to get the save-as box to come up and how to then use the given file name and path in the program for the actual write operation..  Here’s my console program.. it’s pretty simple, but I really don’t know where to even start to convert it into a GUI program.  On line 51, if the output file has not been defined yet, I want to launch the save-as dialog, then on line 54, assign whatever save-as returns to my OutputFileName Variable. 

 

 

Program JobsList;

Uses CRT,Classes,Sysutils;

Var

   TapFileRemainder,TapFileHeader,TapFileJobsList : tstrings;

   TapFileName,TapFileData,OutputTapFileName      : String;

   TapFile                                        : Text;

   TapFileHeaderActive                            : Boolean;

   StringCount                                    : LongInt;

Begin

   If ParamStr(1)<>'' Then

      Begin

         TapFileName:=ParamStr(1);

         If FileExists(TapFileName) Then

            Begin

               TapFileHeaderActive:=True;

               Assign(TapFile,TapFileName);

               Reset(TapFile);

               TapfileHeader:=TStringlist.Create;

               TapfileJobsList:=TStringlist.Create;

               TapfileRemainder:=TStringlist.Create;

               While not(EOF(TapFile)) do

                  Begin

                     Readln(Tapfile,TapFileData);

                     If TapfileHeaderActive then

                        Begin

                           If TapFileData='Call [Subroutines]' Then

                              Begin

                                 Writeln('Subroutine Section Found');

                                 TapFileHeaderActive:=False

                              End

                           Else

                          If Copy(TapFileData,1,15)='Tap File Name =' Then

                              Begin

                                 OutputTapFileName:=Copy(TapFileData,16,Length(TapFileData)-15);

                                 Writeln('Saving to: '+OutputTapFileName);

                              End

                           Else

                              TapfileHeader.Add(TapFileData)

                        End

                     Else

                        Begin

                           If Copy(TapFileData,1,6)='[Job #' Then

                              Begin

                                 Writeln(TapFileData);

                                 TapFileJobsList.Add('Call '+TapFileData);

                              End;

                           TapfileRemainder.Add(TapFileData)

                        End;

                  End;

               Close(TapFile);

               If OutputTapFileName='' Then

                  Begin

                     {Do something to get filename from windows Save-As dialog}

                     OutputTapFileName:= Whatever-was-received-by-Windows-Save-As-dialog;

                  End;

               If OutputTapFileName<>'' Then

                  Begin

                     Writeln('Writing ',TapFileHeader.count+TapFileJobsList.count+TapFileRemainder.count,' Lines to: '+OutputTapFileName);

                     Assign(TapFile,OutputTapFileName);

                     ReWrite(TapFile);

                     If TapFileHeader.count > 1 then

                     For StringCount:=0 to TapFileHeader.count-1 do

                        Writeln(TapFile,TapFileHeader[StringCount]);

                     If TapFileJobsList.count > 1 then

                     For StringCount:=0 to TapFileJobsList.count-1 do

                        Writeln(TapFile,TapFileJobsList[StringCount]);

                     If TapFileRemainder.count > 1 then

                     For StringCount:=0 to TapFileRemainder.count-1 do

                        Writeln(TapFile,TapFileRemainder[StringCount]);

                     Close(TapFile);

                  End

               Else

                  Begin

                     Writeln ('No Output Tap File Specified in Program');

                     Readln;

                  End;

               TapFileHeader.Free;

               TapFileJobsList.Free;

               TapFileRemainder.Free;

            End

         Else

            Begin

               Writeln (TapFileName,' Not Found');

               Readln;

            End;

      End

   Else

      Begin

         Writeln ('No File Name Specified');

         Readln;

      End;

End.

 

James

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Tomas Hajny
Sent: Friday, November 2, 2018 8:16 PM
To: [hidden email]; FPC-Pascal users discussions <[hidden email]>
Subject: Re: [fpc-pascal] Windows programming tutorials for FPC

 

On Sat, November 3, 2018 00:00, Bo Berglund wrote:

> On Fri, 2 Nov 2018 06:13:40 -0400, "James"

> <[hidden email]> wrote:

> 

>>I am wondering if there are any tutorials out there, hopefully

>>specific  to Freepascal and/or Lazarus.

>>I need really basic stuff like how to open a message box, or how to

>>use  windows file open, or save-as dialog boxes.. etc.. even a hello

>>world tutorial would be helpful...

>>ok, so ZERO windows programming experience here...   Any advice on where

>> to start?

> 

> What did the programs you say you have written for so long do?

> Seems hard to believe you have not encountered reading and writing

> files etc.

.

.

 

The original poster mentioned not having used GUI dialog boxes for file selection; I'm pretty sure reading and writing files is no problem for him.

 

Tomas

 

 

_______________________________________________

fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Ewald-2
On 11/03/2018 03:00 PM, James wrote:

> That is correct, I have only ever done console programming, but now I
> find I'm lost trying to do any kind of GUI programming.    I have a very
> simple program that works as a console application, but what I would
> like to do is have it use the Windows "Save AS' Dialog to allow the user
> to save the file using the windows GUI interface, so the user can
> navigate through directory structures and save the file.
>
>  
>
> I looked at a few tutorials and see how to make a form and put some
> buttons on it, but I'm still trying to figure out how to get the save-as
> box to come up and how to then use the given file name and path in the
> program for the actual write operation..  Here’s my console program..
> it’s pretty simple, but I really don’t know where to even start to
> convert it into a GUI program.  On line 51, if the output file has not
> been defined yet, I want to launch the save-as dialog, then on line 54,
> assign whatever save-as returns to my OutputFileName Variable.

For the simple stuff like displaying a message box or acquiring a
filename, you could use the the common dialog boxes of windows
(comdlg32), for example:

https://docs.microsoft.com/en-us/windows/desktop/api/commdlg/nf-commdlg-getsavefilenamea

I now see that this API has in fact been superseded by something
different, but it should give you an idea.

I do have to mention that things never stay "simple" though. Sooner
rather than later you'll find yourself in a situation where you need
more than just that basic functionality, and than you need to start
using something different altogether (unless handling the event loop
manually and fixing every last detail for every last use case of a user
out there is your thing :-) ). For that purpose I would recommend the
usage of some GUI toolkit: lazarus has been mentioned, similar things
include fpGui and MSE, others possible exist as well. If you like to
keep GUI and functionality apart from one another and have no problem
with the GUI being written for a large part in a different language,
have a look at Qt (possible with Qt4pas, if you insist on using pascal
for the GUI).

Anyway, enough on the toolkits out there, a google search will quickly
yield you a lot more than I can mention in this mail :-)

--
Ewald
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Ralf Quint
In reply to this post by Zaaphod
On 11/3/2018 7:00 AM, James wrote:

That is correct, I have only ever done console programming, but now I find I'm lost trying to do any kind of GUI programming.    I have a very simple program that works as a console application, but what I would like to do is have it use the Windows "Save AS' Dialog to allow the user to save the file using the windows GUI interface, so the user can navigate through directory structures and save the file.

 

I looked at a few tutorials and see how to make a form and put some buttons on it, but I'm still trying to figure out how to get the save-as box to come up and how to then use the given file name and path in the program for the actual write operation..  Here’s my console program.. it’s pretty simple, but I really don’t know where to even start to convert it into a GUI program.  On line 51, if the output file has not been defined yet, I want to launch the save-as dialog, then on line 54, assign whatever save-as returns to my OutputFileName Variable. 

The main thing to keep in mind that the main program loop in a GUI program is to handle all the (internal) GUI stuff, not your console program loop. A RAD tool like Lazarus, will conveniently handle that for you.

And you can't just pop up a dialog window without having a window/form in the first place.

In general, the logic of a GUI based program (regardless if Windows, macOS, Linux, etc) simply is different from a console program. Your console program main loop simply  pretty much just becomes a procedure within the GUI main loop.

Ralf


Virus-free. www.avast.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
In reply to this post by Ewald-2

Thanks for the gersavefilenamea idea, I have used windows API function calls before in my Windows Console programs, so I thought I would try to get getsavefilenamea or ifilesavedialog to work in my console program.  So I thought I would start small and get message boxes to work on my console program, this was actually pretty easy, I just added the windows unit to my program and then changed my writeln's to windows.messagebox(0,pchar(TapFileName+' Not Found'),pchar('Error'),MB_OK);

And poof I get a message box when I encounter the error, and the program waits until I hit OK to continue.  Neat! 

 

So it's looking promising, that perhaps I can just keep it a console program that launches the save-as dialog somehow when needed.    This idea appeals to me for several reasons, first, I need to write the status of things somewhere,  it's easy to just have a writeln in a console application, but in a windows application, I have no idea how I would make some kind of text box to display this information.   Also, I want this program to start executing immediately, and if no user intervention is needed, I want it to launch, perform all tasks, and exit.  I just don't have anything to put on a form because the intent is that the user would only interact with this program if it encountered an error, or if the user needed to specify the output file name.

 

So my question is, how can I use Ifilesavedialog with just FreePascal in a console application?  I tried just accessing it the same as I did messagebox, but I just get an error stating the function is not found.   It seems like I ran across this before, I wanted to use a Windows API function that was not included in the windows unit and I was somehow able to add access to it on my own,  but I just can't recall now what function that was, or what program I was working on that needed it, or how it was accomplished.   Perhaps it is in the windows unit, or another unit, but I'm just not calling it correctly.   Current version of my program that uses message boxes for errors is below.

 

James

 

Program JobsList;

Uses CRT,Classes,Sysutils,windows;

Var

   TapFileRemainder,TapFileHeader,TapFileJobsList : tstrings;

   TapFileName,TapFileData,OutputTapFileName      : AnsiString;

   TapFile                                        : Text;

   TapFileHeaderActive                            : Boolean;

   StringCount                                    : LongInt;

Begin

   If ParamStr(1)<>'' Then

      Begin

         TapFileName:=ParamStr(1);

         If FileExists(TapFileName) Then

            Begin

               TapFileHeaderActive:=True;

               Assign(TapFile,TapFileName);

               Reset(TapFile);

               TapfileHeader:=TStringlist.Create;

               TapfileJobsList:=TStringlist.Create;

               TapfileRemainder:=TStringlist.Create;

               While not(EOF(TapFile)) do

                  Begin

                     Readln(Tapfile,TapFileData);

                     If TapfileHeaderActive then

                        Begin

                           If TapFileData='Call [Subroutines]' Then

                              Begin

                                 Writeln('Subroutine Section Found');

                                 TapFileHeaderActive:=False

                              End

                           Else

                           If Copy(TapFileData,1,15)='Tap File Name =' Then

                              Begin

                                 OutputTapFileName:=Copy(TapFileData,16,Length(TapFileData)-15);

                                 Writeln('Saving to: '+OutputTapFileName);

                              End

                           Else

                              TapfileHeader.Add(TapFileData)

                        End

                     Else

                        Begin

                           If Copy(TapFileData,1,6)='[Job #' Then

                              Begin

                                 Writeln(TapFileData);

                                 TapFileJobsList.Add('Call '+TapFileData);

                              End;

                           TapfileRemainder.Add(TapFileData)

                        End;

                  End;

               Close(TapFile);

               If OutputTapFileName='' Then

                  Begin

                     {Do something to get filename from windows Save-As dialog}

                     {OutputTapFileName:= Whatever-was-received-by-Windows-Save-As-dialog;}

                  End;

               If OutputTapFileName<>'' Then

                  Begin

                     Writeln('Writing ',TapFileHeader.count+TapFileJobsList.count+TapFileRemainder.count,' Lines to: '+OutputTapFileName);

                     Assign(TapFile,OutputTapFileName);

                     ReWrite(TapFile);

                     If TapFileHeader.count > 1 then

                     For StringCount:=0 to TapFileHeader.count-1 do

                        Writeln(TapFile,TapFileHeader[StringCount]);

                     If TapFileJobsList.count > 1 then

                     For StringCount:=0 to TapFileJobsList.count-1 do

                        Writeln(TapFile,TapFileJobsList[StringCount]);

                     If TapFileRemainder.count > 1 then

                     For StringCount:=0 to TapFileRemainder.count-1 do

                        Writeln(TapFile,TapFileRemainder[StringCount]);

                     Close(TapFile);

                  End

               Else

                  Begin

                     windows.messagebox(0,pchar('No Output Tap File Specified in Program'),pchar('Error'),MB_OK);

                  End;

               TapFileHeader.Free;

               TapFileJobsList.Free;

               TapFileRemainder.Free;

            End

         Else

            Begin

               windows.messagebox(0,pchar(TapFileName+' Not Found'),pchar('Error'),MB_OK);

            End;

      End

   Else

      Begin

         windows.messagebox(0,pchar('No File Name Specified'),pchar('Error'),MB_OK);

      End;

End.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
In reply to this post by Ralf Quint

>And you can't just pop up a dialog window without having a window/form in the first place. 

That’s probably my problem…  My idea of just calling up the windows-API to get the save-as dialog probably won’t work without a form, even though I was able to get message boxes working


>In general, the logic of a GUI based program (regardless if Windows, macOS, Linux, etc) simply is different from a console program. Your console program main loop simply  pretty much just becomes a procedure within the GUI main loop.

This logic difference is what is most confusing to me.   I just don’t know where to put my main program and I don’t know how to output things to some kind of text box.   I don’t want the user to do anything at all unless it’s necessary… so if everything is set up correctly, the program opens, does it’s thing, writes some status stuff to a text box and closes,  no buttons to push or anything…. If I get a GUI program to work, I guess I can put a percentage complete barograph somewhere.  If there’s an error, I need to stop and wait for acknowledgement of the error, or if the output file was not specified, I want the Save-As box to just open up on it’s own with out anyone pushing any buttons, and when the save-as box is closed the process completes on it’s own and the program exits without any further user intervention.

I’ve been tinkering with Lazarus, and I managed to get a form with some buttons based on the examples, and I did make one button open the save-as box… but I’m clueless on how to make the save-as box only come up when needed and by a programming command, not because someone pushed a button.  I still can’t figure out how to write my writeln’s into a text box of some sort.    I get the idea… instead of a sequential program the executes from beginning to end,  everything kind of all happens at the same time


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Luca Olivetti-2
El 3/11/18 a les 21:20, James ha escrit:

>
> I’ve been tinkering with Lazarus, and I managed to get a form with some
> buttons based on the examples, and I did make one button open the
> save-as box… but I’m clueless on how to make the save-as box only come
> up when needed and by a programming command, not because someone pushed
> a button.  I still can’t figure out how to write my writeln’s into a
> text box of some sort.    I get the idea… instead of a sequential
> program the executes from beginning to end,  everything kind of all
> happens at the same time


Try this:

-put a memo on the form (say, memo1) and a save dialog.
-in the object inspector double click on the OnCreate event of the form.
-this will create a FormCreate method. Put your code there (including
the opening of the save dialog if needed).
-in your code show diagnostics messages in the memo
(mem1.lines.add('whatever'))
-at the end of your code, if everything is OK, add

     Application.ShowMainForm:=false;
     Application.Terminate;


This way, if there are no errors the form won't show, otherwise it will
show whatever you put in the memo.

Tip: if your code will take a significant amount of time put at the
beginning

Screen.Cursor:=crHourGlass;

and at the end

Screen.Cursor:=crDefault;


Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Ralf Quint
In reply to this post by Zaaphod
On 11/3/2018 1:20 PM, James wrote:

>And you can't just pop up a dialog window without having a window/form in the first place. 

That’s probably my problem…  My idea of just calling up the windows-API to get the save-as dialog probably won’t work without a form, even though I was able to get message boxes working


>In general, the logic of a GUI based program (regardless if Windows, macOS, Linux, etc) simply is different from a console program. Your console program main loop simply  pretty much just becomes a procedure within the GUI main loop.

This logic difference is what is most confusing to me.   I just don’t know where to put my main program and I don’t know how to output things to some kind of text box.   I don’t want the user to do anything at all unless it’s necessary… so if everything is set up correctly, the program opens, does it’s thing, writes some status stuff to a text box and closes,  no buttons to push or anything…. If I get a GUI program to work, I guess I can put a percentage complete barograph somewhere.  If there’s an error, I need to stop and wait for acknowledgement of the error, or if the output file was not specified, I want the Save-As box to just open up on it’s own with out anyone pushing any buttons, and when the save-as box is closed the process completes on it’s own and the program exits without any further user intervention.

I had that problem many years ago as well, having literally written hundreds of console of TUI based programs, mainly on DOS, myself. And then switching some of them to a GUI program in Delphi (there was no Lazarus at that time) took quite a bit of rethinking of  a couple of decades habits in console/command line ways or even self written TUI programs.

I’ve been tinkering with Lazarus, and I managed to get a form with some buttons based on the examples, and I did make one button open the save-as box… but I’m clueless on how to make the save-as box only come up when needed and by a programming command, not because someone pushed a button.  I still can’t figure out how to write my writeln’s into a text box of some sort.    I get the idea… instead of a sequential program the executes from beginning to end,  everything kind of all happens at the same time


Yup, all the windows (as in GUI) stuff happens all the time, at the same time as your actual program. I have no had a program myself where I had a dialog "come up out of the blue" (as you kind of describe it), but I have written a lot of data conversion programs that at some point required to open up an additional open or save dialog. A lot though depends on what the actual logic behind the actual processing of your console program is. A lot of times, it might take a bit of re-organizing.
I am a bit short of time, as I am dealing on and off all day with some CERT stuff, but I will see that I take a closer look at that program (snippet?) that you posted later today or tomorrow morning  and return a rough sample of a GUI "solution" for it...

Ralf



Virus-free. www.avast.com

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
In reply to this post by Luca Olivetti-2
Thanks for the suggestion...

I put my code in the OnCreate event as you suggested, but when I try to compile it, I get wrong number of parameters specified for call to Assign... my code worked before, and I have no idea what other parameters it could want or why it would be any different than my console application.

I'm doing:
Var  
   TapFileName : AnsiString;
   TapFile  : Text;

Assign(TapFile,TapFileName);

Any ideas why this works in FPC but not in Lazarus?

James

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Luca Olivetti
Sent: Saturday, November 3, 2018 4:47 PM
To: [hidden email]
Subject: Re: [fpc-pascal] Windows programming tutorials for FPC

El 3/11/18 a les 21:20, James ha escrit:

>
> I’ve been tinkering with Lazarus, and I managed to get a form with
> some buttons based on the examples, and I did make one button open the
> save-as box… but I’m clueless on how to make the save-as box only come
> up when needed and by a programming command, not because someone
> pushed a button.  I still can’t figure out how to write my writeln’s
> into a text box of some sort.    I get the idea… instead of a
> sequential program the executes from beginning to end,  everything
> kind of all happens at the same time


Try this:

-put a memo on the form (say, memo1) and a save dialog.
-in the object inspector double click on the OnCreate event of the form.
-this will create a FormCreate method. Put your code there (including the opening of the save dialog if needed).
-in your code show diagnostics messages in the memo
(mem1.lines.add('whatever'))
-at the end of your code, if everything is OK, add

     Application.ShowMainForm:=false;
     Application.Terminate;


This way, if there are no errors the form won't show, otherwise it will show whatever you put in the memo.

Tip: if your code will take a significant amount of time put at the beginning

Screen.Cursor:=crHourGlass;

and at the end

Screen.Cursor:=crDefault;


Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Luca Olivetti-2
El 3/11/18 a les 23:04, James ha escrit:

> Thanks for the suggestion...
>
> I put my code in the OnCreate event as you suggested, but when I try to compile it, I get wrong number of parameters specified for call to Assign... my code worked before, and I have no idea what other parameters it could want or why it would be any different than my console application.
>
> I'm doing:
> Var
>     TapFileName : AnsiString;
>     TapFile  : Text;
>
> Assign(TapFile,TapFileName);
>
> Any ideas why this works in FPC but not in Lazarus?

Because Assign is a method of the form. Use AssignFile or System.Assign.
BTW: what I explained before is *not* how a gui application is usually
written but it should work in your case.

Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
In reply to this post by Ralf Quint

It’s not a snippet, that’s the entire thing.   It’s pretty simple, just a sequential set of events to fix a file.  It would be a great help if you could get me an example of how to make this work.

 

James

 

From: fpc-pascal <[hidden email]> On Behalf Of Ralf Quint
Sent: Saturday, November 3, 2018 5:57 PM
To: [hidden email]
Subject: Re: [fpc-pascal] Windows programming tutorials for FPC

 

On 11/3/2018 1:20 PM, James wrote:

>And you can't just pop up a dialog window without having a window/form in the first place. 

That’s probably my problem…  My idea of just calling up the windows-API to get the save-as dialog probably won’t work without a form, even though I was able to get message boxes working


>In general, the logic of a GUI based program (regardless if Windows, macOS, Linux, etc) simply is different from a console program. Your console program main loop simply  pretty much just becomes a procedure within the GUI main loop.


This logic difference is what is most confusing to me.   I just don’t know where to put my main program and I don’t know how to output things to some kind of text box.   I don’t want the user to do anything at all unless it’s necessary… so if everything is set up correctly, the program opens, does it’s thing, writes some status stuff to a text box and closes,  no buttons to push or anything…. If I get a GUI program to work, I guess I can put a percentage complete barograph somewhere.  If there’s an error, I need to stop and wait for acknowledgement of the error, or if the output file was not specified, I want the Save-As box to just open up on it’s own with out anyone pushing any buttons, and when the save-as box is closed the process completes on it’s own and the program exits without any further user intervention.

I had that problem many years ago as well, having literally written hundreds of console of TUI based programs, mainly on DOS, myself. And then switching some of them to a GUI program in Delphi (there was no Lazarus at that time) took quite a bit of rethinking of  a couple of decades habits in console/command line ways or even self written TUI programs.

I’ve been tinkering with Lazarus, and I managed to get a form with some buttons based on the examples, and I did make one button open the save-as box… but I’m clueless on how to make the save-as box only come up when needed and by a programming command, not because someone pushed a button.  I still can’t figure out how to write my writeln’s into a text box of some sort.    I get the idea… instead of a sequential program the executes from beginning to end,  everything kind of all happens at the same time

 

Yup, all the windows (as in GUI) stuff happens all the time, at the same time as your actual program. I have no had a program myself where I had a dialog "come up out of the blue" (as you kind of describe it), but I have written a lot of data conversion programs that at some point required to open up an additional open or save dialog. A lot though depends on what the actual logic behind the actual processing of your console program is. A lot of times, it might take a bit of re-organizing.
I am a bit short of time, as I am dealing on and off all day with some CERT stuff, but I will see that I take a closer look at that program (snippet?) that you posted later today or tomorrow morning  and return a rough sample of a GUI "solution" for it...

Ralf

 

 

Virus-free. www.avast.com

 


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Tomas Hajny-2
In reply to this post by Zaaphod
On Sat, November 3, 2018 23:04, James wrote:

> Thanks for the suggestion...
>
> I put my code in the OnCreate event as you suggested, but when I try to
> compile it, I get wrong number of parameters specified for call to
> Assign... my code worked before, and I have no idea what other parameters
> it could want or why it would be any different than my console
> application.
>
> I'm doing:
> Var
>    TapFileName : AnsiString;
>    TapFile  : Text;
>
> Assign(TapFile,TapFileName);

I guess that System.Assign(TapFile,TapFileName); should work for you.


> Any ideas why this works in FPC but not in Lazarus?

Probably a conflict with another Assign with a different functionality and
different syntax. I don't use Lazarus myself, but I'm sure it provides a
way to show where Assign is declared and how.

Tomas


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Free Pascal - General mailing list
Am Sa., 3. Nov. 2018, 23:56 hat Tomas Hajny <[hidden email]> geschrieben:
On Sat, November 3, 2018 23:04, James wrote:
> Thanks for the suggestion...
>
> I put my code in the OnCreate event as you suggested, but when I try to
> compile it, I get wrong number of parameters specified for call to
> Assign... my code worked before, and I have no idea what other parameters
> it could want or why it would be any different than my console
> application.
>
> I'm doing:
> Var
>    TapFileName : AnsiString;
>    TapFile  : Text;
>
> Assign(TapFile,TapFileName);

I guess that System.Assign(TapFile,TapFileName); should work for you.


> Any ideas why this works in FPC but not in Lazarus?

Probably a conflict with another Assign with a different functionality and
different syntax. I don't use Lazarus myself, but I'm sure it provides a
way to show where Assign is declared and how.

That's why Delphi back with Delphi 1 already I think introduced AssignFile as an overload of Assign. 

Regards, 
Sven 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Ewald-2
In reply to this post by Zaaphod
On 11/03/2018 09:04 PM, James wrote:
> So my question is, how can I use Ifilesavedialog with just FreePascal in
> a console application?

First off, the IFileSaveDialog is an interface, not a simple function.
So, you'll need to:
        - Include the right units from freepascal (ActiveX and comobj
           IIRC)
        - Initialize and finalize the COM subsystem (see CoInitialize
           and CoUninitialize)
        - Use the CoCreateInstance to instantiate an IFileSaveDialog,
           etc.. I've never used the IFileSaveDialog myself, so have a
           look at
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913%28v=vs.85%29.aspx#usage

That's the nice thing about the GetSaveFileNameA function: one call, and
you're done :-)

Now, if this function is not defined in the windows unit (which could be
the case), you can either look into some other units or simply define it
yourself:

=== code begin ===
Type
        TOpenFileNameAHookProc = function(Wnd: HWND; Msg: UINT; wParam: WPARAM;
lParam: LPARAM): UINT stdcall;

        TOpenFileNameA = Packed Record
                lStructSize: DWord;
                hWndOwner: HWND;
                hInstance: HINST;
                lpstrFilter: PChar;
                lpstrCustomFilter: PChar;
                nMaxCustFilter: DWord;
                nFilterIndex: DWord;
                lpstrFile: PChar;
                nMaxFile: DWord;
                lpstrFileTitle: PChar;
                nMaxFileTitle: DWord;
                lpstrInitialDir: PChar;
                lpstrTitle: PChar;
                Flags: DWord;
                nFileOffset: Word;
                nFileExtension: Word;
                lpstrDefExt: PChar;
                lCustData: LPARAM;
                lpfnHook: TOpenFileNameAHookProc;
                lpTemplateName: PChar;
                lpEditInfo: Pointer; // Undocumented?
                lpstrPrompt: PChar;
                _Reserved1: Pointer;
                _Reserved2: DWord;
                FlagsEx: DWord;
        End;
        POpenFileNameA = ^TOpenFileNameA;

Function GetSaveFileNameA(arg: POpenFileNameA): windows.bool; stdcall;
external 'comdlg32' name 'GetSaveFileNameA';
=== code end ===


--
Ewald
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
This is very interesting, thank you for the code on how to define the GetSaveFileNameA function.  I wrote a sample program to get it to work, but I think I have some syntax wrong, or maybe I'm not initializing something correctly.   It compiles ok, but it doesn't execute even my writeln's, I just get an exit code = 1

James

Program TestGetSaveFileNameA;
Uses CRT,Classes,Sysutils,windows;
Type
        TOpenFileNameAHookProc = function(Wnd: HWND; Msg: UINT; wParam: WPARAM;
lParam: LPARAM): UINT stdcall;

        TOpenFileNameA = Packed Record
                lStructSize: DWord;
                hWndOwner: HWND;
                hInstance: HINST;
                lpstrFilter: PChar;
                lpstrCustomFilter: PChar;
                nMaxCustFilter: DWord;
                nFilterIndex: DWord;
                lpstrFile: PChar;
                nMaxFile: DWord;
                lpstrFileTitle: PChar;
                nMaxFileTitle: DWord;
                lpstrInitialDir: PChar;
                lpstrTitle: PChar;
                Flags: DWord;
                nFileOffset: Word;
                nFileExtension: Word;
                lpstrDefExt: PChar;
                lCustData: LPARAM;
                lpfnHook: TOpenFileNameAHookProc;
                lpTemplateName: PChar;
                lpEditInfo: Pointer; // Undocumented?
                lpstrPrompt: PChar;
                _Reserved1: Pointer;
                _Reserved2: DWord;
                FlagsEx: DWord;
        End;
        POpenFileNameA = ^TOpenFileNameA;

Function GetSaveFileNameA(arg: POpenFileNameA): windows.bool; stdcall; external 'comdlg32' name 'GetSaveFileNameA';

Var
   TFilename                                      : TOpenFileNameA;
   PFilename                                      : POpenFileNameA;

Begin
   Writeln('Start');
   TFilename.lpstrInitialDir:=Pchar('I:\');
   Pfilename:=@Tfilename;
   Writeln(GetSaveFileNameA(PFilename));
   Writeln('Finished');
   Readln;
End.





-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Ewald
Sent: Sunday, November 4, 2018 8:06 AM
To: [hidden email]
Subject: Re: [fpc-pascal] Windows programming tutorials for FPC

On 11/03/2018 09:04 PM, James wrote:
> So my question is, how can I use Ifilesavedialog with just FreePascal
> in a console application?

First off, the IFileSaveDialog is an interface, not a simple function.
So, you'll need to:
        - Include the right units from freepascal (ActiveX and comobj
           IIRC)
        - Initialize and finalize the COM subsystem (see CoInitialize
           and CoUninitialize)
        - Use the CoCreateInstance to instantiate an IFileSaveDialog,
           etc.. I've never used the IFileSaveDialog myself, so have a
           look at
https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913%28v=vs.85%29.aspx#usage

That's the nice thing about the GetSaveFileNameA function: one call, and you're done :-)

Now, if this function is not defined in the windows unit (which could be the case), you can either look into some other units or simply define it
yourself:

=== code begin ===
Type
        TOpenFileNameAHookProc = function(Wnd: HWND; Msg: UINT; wParam: WPARAM;
lParam: LPARAM): UINT stdcall;

        TOpenFileNameA = Packed Record
                lStructSize: DWord;
                hWndOwner: HWND;
                hInstance: HINST;
                lpstrFilter: PChar;
                lpstrCustomFilter: PChar;
                nMaxCustFilter: DWord;
                nFilterIndex: DWord;
                lpstrFile: PChar;
                nMaxFile: DWord;
                lpstrFileTitle: PChar;
                nMaxFileTitle: DWord;
                lpstrInitialDir: PChar;
                lpstrTitle: PChar;
                Flags: DWord;
                nFileOffset: Word;
                nFileExtension: Word;
                lpstrDefExt: PChar;
                lCustData: LPARAM;
                lpfnHook: TOpenFileNameAHookProc;
                lpTemplateName: PChar;
                lpEditInfo: Pointer; // Undocumented?
                lpstrPrompt: PChar;
                _Reserved1: Pointer;
                _Reserved2: DWord;
                FlagsEx: DWord;
        End;
        POpenFileNameA = ^TOpenFileNameA;

Function GetSaveFileNameA(arg: POpenFileNameA): windows.bool; stdcall; external 'comdlg32' name 'GetSaveFileNameA'; === code end ===


--
Ewald
_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Zaaphod
In reply to this post by Luca Olivetti-2
I used System.Assign and now I have my program working in Lazarus,  I am exploring both options of making it a real windows application with Lazarus and a console program that can launch save-as.

The Lazarus version is mostly working in Lazarus, but instead of everything happening before the form is loaded, is there a way I could make the form first, then just start processing everything, so that my messages I send to memo1 show up as it's processing?  I'm guessing I need to move my program from On-create to somewhere else so it runs after the memo box is showing... but I don't know where I would move it to.  Any suggestions?

James  Richters

-----Original Message-----
From: fpc-pascal <[hidden email]> On Behalf Of Luca Olivetti
Sent: Saturday, November 3, 2018 6:15 PM
To: [hidden email]
Subject: Re: [fpc-pascal] Windows programming tutorials for FPC

El 3/11/18 a les 23:04, James ha escrit:

> Thanks for the suggestion...
>
> I put my code in the OnCreate event as you suggested, but when I try to compile it, I get wrong number of parameters specified for call to Assign... my code worked before, and I have no idea what other parameters it could want or why it would be any different than my console application.
>
> I'm doing:
> Var
>     TapFileName : AnsiString;
>     TapFile  : Text;
>
> Assign(TapFile,TapFileName);
>
> Any ideas why this works in FPC but not in Lazarus?

Because Assign is a method of the form. Use AssignFile or System.Assign.
BTW: what I explained before is *not* how a gui application is usually written but it should work in your case.

Bye
--
Luca
_______________________________________________
fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: Windows programming tutorials for FPC

Alexander Grotewohl
In reply to this post by Zaaphod
Program TestGetSaveFileNameA;
Uses windows, commdlg;

Var
    TFilename                                      : TOpenFileNameA;

    ret: array[0..100] of char;

Begin
    Writeln('Start');

    fillchar(TFileName, sizeof(TFileName), 0);
    TFileName.lStructSize:=sizeof(TFileName);

    TFileName.hwndOwner:=0;
    TFileName.lpstrFile:=ret;
    TFileName.lpstrFile[0]:=#0;
    TFileName.lpstrFilter:='Text Files (*.txt)'+#0+'*.txt'+#0;
    TFileName.nMaxFile:=100;
    TFileName.Flags := OFN_EXPLORER or OFN_FILEMUSTEXIST or
OFN_HIDEREADONLY;
    TFileName.lpstrDefExt:='txt';

    Writeln(GetSaveFileNameA(@TFilename));
    Writeln('Finished with '+strpas(TFileName.lpstrFile));
    Readln;
End.


On 11/4/2018 11:21 AM, James wrote:

> This is very interesting, thank you for the code on how to define the GetSaveFileNameA function.  I wrote a sample program to get it to work, but I think I have some syntax wrong, or maybe I'm not initializing something correctly.   It compiles ok, but it doesn't execute even my writeln's, I just get an exit code = 1
>
> James
>
> Program TestGetSaveFileNameA;
> Uses CRT,Classes,Sysutils,windows;
> Type
> TOpenFileNameAHookProc = function(Wnd: HWND; Msg: UINT; wParam: WPARAM;
> lParam: LPARAM): UINT stdcall;
>
> TOpenFileNameA = Packed Record
> lStructSize: DWord;
> hWndOwner: HWND;
> hInstance: HINST;
> lpstrFilter: PChar;
> lpstrCustomFilter: PChar;
> nMaxCustFilter: DWord;
> nFilterIndex: DWord;
> lpstrFile: PChar;
> nMaxFile: DWord;
> lpstrFileTitle: PChar;
> nMaxFileTitle: DWord;
> lpstrInitialDir: PChar;
> lpstrTitle: PChar;
> Flags: DWord;
> nFileOffset: Word;
> nFileExtension: Word;
> lpstrDefExt: PChar;
> lCustData: LPARAM;
> lpfnHook: TOpenFileNameAHookProc;
> lpTemplateName: PChar;
> lpEditInfo: Pointer; // Undocumented?
> lpstrPrompt: PChar;
> _Reserved1: Pointer;
> _Reserved2: DWord;
> FlagsEx: DWord;
> End;
> POpenFileNameA = ^TOpenFileNameA;
>
> Function GetSaveFileNameA(arg: POpenFileNameA): windows.bool; stdcall; external 'comdlg32' name 'GetSaveFileNameA';
>
> Var
>     TFilename                                      : TOpenFileNameA;
>     PFilename                                      : POpenFileNameA;
>
> Begin
>     Writeln('Start');
>     TFilename.lpstrInitialDir:=Pchar('I:\');
>     Pfilename:=@Tfilename;
>     Writeln(GetSaveFileNameA(PFilename));
>     Writeln('Finished');
>     Readln;
> End.
>
>
>
>
>
> -----Original Message-----
> From: fpc-pascal <[hidden email]> On Behalf Of Ewald
> Sent: Sunday, November 4, 2018 8:06 AM
> To: [hidden email]
> Subject: Re: [fpc-pascal] Windows programming tutorials for FPC
>
> On 11/03/2018 09:04 PM, James wrote:
>> So my question is, how can I use Ifilesavedialog with just FreePascal
>> in a console application?
> First off, the IFileSaveDialog is an interface, not a simple function.
> So, you'll need to:
> - Include the right units from freepascal (ActiveX and comobj
>             IIRC)
> - Initialize and finalize the COM subsystem (see CoInitialize
>             and CoUninitialize)
> - Use the CoCreateInstance to instantiate an IFileSaveDialog,
>             etc.. I've never used the IFileSaveDialog myself, so have a
>             look at
> https://msdn.microsoft.com/en-us/library/windows/desktop/bb776913%28v=vs.85%29.aspx#usage
>
> That's the nice thing about the GetSaveFileNameA function: one call, and you're done :-)
>
> Now, if this function is not defined in the windows unit (which could be the case), you can either look into some other units or simply define it
> yourself:
>
> === code begin ===
> Type
> TOpenFileNameAHookProc = function(Wnd: HWND; Msg: UINT; wParam: WPARAM;
> lParam: LPARAM): UINT stdcall;
>
> TOpenFileNameA = Packed Record
> lStructSize: DWord;
> hWndOwner: HWND;
> hInstance: HINST;
> lpstrFilter: PChar;
> lpstrCustomFilter: PChar;
> nMaxCustFilter: DWord;
> nFilterIndex: DWord;
> lpstrFile: PChar;
> nMaxFile: DWord;
> lpstrFileTitle: PChar;
> nMaxFileTitle: DWord;
> lpstrInitialDir: PChar;
> lpstrTitle: PChar;
> Flags: DWord;
> nFileOffset: Word;
> nFileExtension: Word;
> lpstrDefExt: PChar;
> lCustData: LPARAM;
> lpfnHook: TOpenFileNameAHookProc;
> lpTemplateName: PChar;
> lpEditInfo: Pointer; // Undocumented?
> lpstrPrompt: PChar;
> _Reserved1: Pointer;
> _Reserved2: DWord;
> FlagsEx: DWord;
> End;
> POpenFileNameA = ^TOpenFileNameA;
>
> Function GetSaveFileNameA(arg: POpenFileNameA): windows.bool; stdcall; external 'comdlg32' name 'GetSaveFileNameA'; === code end ===
>
>
> --
> Ewald
> _______________________________________________
> fpc-pascal maillist  -  [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist  -  [hidden email]
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12