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?
Mailing lists / fpc-pascal / 2018-11
Windows programming tutorials for FPC
31 messages · 13 people · –
- JJames 12
- LLuca Olivetti 3
- AAlexander Grotewohl 2
- BBo Berglund 2
- EEwald 2
- RRalf Quint 2
- +7 more
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
AM James wroteI'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
I've been programming for decades with Pascal, starting with Turbo Pascal, and for a few…
"James" wroteI 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 wrote 14 lines
.
.
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
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
Earlier message 54 lines
James wrote 17 lines
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 :-)
That is correct, I have only ever done console programming, but now I find I'm lost…
James wrote 18 lines
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
---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus
For the simple stuff like displaying a message box or acquiring a filename, you could use…
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.The main thing to keep in mind that the main program loop in a GUI program is to handle…
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
El 3/11/18 a les 21:20, James ha escrit:
Show quoted text 9 lines
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
That’s probably my problem… My idea of just calling up the windows-API to get the…
James wrote 28 lines
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.
Show quoted text 11 lines
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
---
This email has been checked for viruses by Avast antivirus software. https://www.avast.com/antivirus
El 3/11/18 a les 21:20, James ha escrit: Try this: -put a memo on the form (say, memo1)…
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
Earlier message 46 lines
El 3/11/18 a les 23:04, James ha escrit:
Show quoted text 12 lines
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
I had that problem many years ago as well, having literally written hundreds of console…
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
Earlier message 42 lines
Thanks for the suggestion... I put my code in the OnCreate event as you suggested, but…
James wrote 14 lines
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
Am Sa., 3. Nov. 2018, 23:56 hat Tomas Hajny geschrieben:
Show quoted text 25 lines
That's why Delphi back with Delphi 1 already I think introduced AssignFile as an overload of Assign.
Regards,
Sven
Thanks for the gersavefilenamea idea, I have used windows API function calls before in my…
James wroteSo 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 ===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:\');
;
Writeln(GetSaveFileNameA(PFilename));
Writeln('Finished');
Readln;
End.Earlier message 65 lines
El 3/11/18 a les 23:04, James ha escrit: Because Assign is a method of the form. Use…
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
Earlier message 26 lines
This is very interesting, thank you for the code on how to define the GetSaveFileNameA…
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.James wrote 127 lines
I used System.Assign and now I have my program working in Lazarus, I am exploring both…
"James" wrote 8 lines
In a Lazarus GUI program you can use a timer to handle this. Just set it to say 1s delay and thenb enable it in Form.OnCreate:
TfrmMain = class(TForm)
...
Timer1: TTimer;
...
procedure TfrmMain.FormCreate(Sender: TObject);
begin
Timer1.Interval := 500; //Default is 1000 ms
Timer1.Enabled := true;
end;
procedure TfrmMain.Timer1Timer(Sender: TObject);
begin
//Put your code here
Application.Terminate; //The application will disappear from view...
end;If you want your form to be responsive you also need this inside loops in your program:
Application.ProcessMessages;
Otherwise the form will feel like a dead brick while the Timer function runs...
I used System.Assign and now I have my program working in Lazarus, I am exploring both…
El 4/11/18 a les 17:29, James ha escrit:
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?
Oh, I thought you just wanted the program to work as a normal console (silent) application and just show the form in case of errors.
Try this: instead of OnCreate use Onshow, but don't put your code there, just this
Application.QueueAsyncCall(@MyProc,0)
then put the cursor on MyProc and press CTRL+SHIFT+C, now lazarus will automatically declare the MyProc method. You can put your code there. It will be invoked asynchronously once the form has been shown. The problem is, while your loop is running the form will be totally unresponsive, it won't even show the lines you add to the memo. To avoid it you could either put your code in a thread (difficult) in order not to stall the gui, or simply call Application.ProcessMessages from time to time, e.g. when you add a line to the memo.
Bye
I used System.Assign and now I have my program working in Lazarus, I am exploring both…
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?
Hi James,
Move your code to the form's OnActivate event.
cheers,
Martin.
It’s not a snippet, that’s the entire thing. It’s pretty simple, just a…
Il 03/11/2018 23:16, James ha scritto:
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.
The simplest thing you can do is just to transform your application into a GUI application.
Try to do the following:
Start your Lazarus, then select Project->New Project -> Application
You get that way an empty form and a skeleton unit.
You'll find that in the var section there's de declaration of Form1, you may add there your var's.
If you want your user to pick up a file name from the file dialog, do the following:
Click on the Dialogs Tab, on the icon "TOpen Dialog", and then click anywhere on the Form.
Now you have an OpenDialog icon on your form, which will not be visible run time. It's there just to let you set its properties in the Object Inspector. You may set there a default extension, an Initial Dir, a default file name, or whatever you think can be useful to the user. Or you may leave the fields empty so that the system defaults are taken. You may also set the OpenDialog File name from the invocation command line: in the initialization section (or in the OnCreate event of the form) you may add OpenDialog1.FileName := ParamStr(1).
Now from the "Standard" Tab click on the TButton Icon and click on the form. You get a Button on the form. Change in the Object Inspector the Caption to what you want, sort of "Select Input File". In the object Inspector select the Events tab and then the OnClick event. Click on the three periods to the right, and you'll get in the source editor the skeleton of a new procedure (TForm1.Button1Click).
That's where all of your code goes. Typically:
If OpenDialog1.Execute then begin
TapFileName := OpenDialog1.FileName;
......
etc.
You may add a Tedit (always fron the "Standard" tab) object to show your
messages: your writeln becomes Edit1.Text := 'Whatever'.Where you need to ask the user for a new filename, you may just call a second time OpenDialog1.execute, to get it.
If you want to be kind to your user, you may add a "Close" button, to close the application when the user is done.
Just pick from the "Aditional" tab a TBitBtn, put it in your form, then in the Object Inspector select the "Kind" as "bkClose".
There's no more than that. You may now save your project, giving the program and the unit some name you like. Then play a bit with it, and adjust following your needs.
Hope that it helps.
Giuliano
Program TestGetSaveFileNameA; Uses windows, commdlg; Var …
Thank you for this example! It works perfectly and I now have my console program putting up message boxes and opening a Save-As box as needed.
James
Earlier message 173 lines
Move your code to the form's OnActivate event. Martin.
Thank you everyone who helped me with this. I have both the Lazarus version and the FPC console version both working with a Save-As dialog and message boxes for errors. Basic functionality is working great with both methods. Since it's such a simple program, I'll tinker with it with both methods for a while and use it as a learning process. I am encouraged by getting this to work in Lazarus, so it's probably time I took to time to learn more about it instead of always writing console applications, I can see where it would be great to have some buttons for options and things like that... and at the same time I am thrilled to have such capability in my console applications!
James
Thank you for this example! It works perfectly and I now have my console program putting…
I've been using the example below to use the Save-as dialog in my console program, and it works great, but I would like to be able to detect if the user pushes either the red X or the cancel button in the dialog. I am supplying a suggested default name, and what's happening is if the user cancels or hits the red X, it just saves the file using the suggested default name, but the correct behavior would be to not save anything. I'm not sure how this is normally done with GetSaveFileNameA.
Earlier message 173 lines
I've been programming for decades with Pascal, starting with Turbo Pascal, and for a few…
James wroteI'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 - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Tutorials? well, that is funny. Usually there are many tutorials the other way around, that is, young guys that master GUI and must learn real programming to go further.
I know where you are, because I was there many years ago when I moved from DOS programming to windows programming. Someone has posted that programming is programming, no matter GUI or not GUI. Well, yes and no. You must change your paradigm a little. I suggest you to try Lazarus.
Besides tutorials and howtos people have posted, let me tell you how I changed my point of view. Hope it helps you, but I don't expect much, everybody has his own epiphany ;-).
I started to stop seeing programs as a lineal process. I saw it more like an iceberg, where the window you see in the GUI is the tip, whenever you e.i. click a button, the program goes down, gets results and returns to the tip.
In console programs, you read data one by by one, a prompt and stop expecting an input. If the input data is wrong you ask it again. Creating a window with many data allowing the user is complicated, you must control cursor keys etc, special keys to submit the full form etc, you must inspect keys pressed by user in a loop. That is the "easy" part in GUI, using a RAD you create the window in design time, you write the prompts for data, the font, the position the size, the colors, the order, initial values etc, you run de program, and there it is. (Paradoxically the more you work with GUI, the more you do in run time and the less in design time). The GUI is composed by a window and controls, that are anything on the window, a button, a text written, a bevel, a box, a check box, a menu,a progress bar, a date prompt... and hundreds of different controls.
The GUI programming is event driver. That is, the program, the windows, sits there doing nothing, and reacts to what user does. The user clicks a button, then the program performs what it is expected to do and returns to wait for a user action. That applies to user clicking a button, or moving the mouse, or typing a key, or even moving a window. Usually every control has some events, a button a "onClick" event, an edit box a "on selected text", and some controls, like a label, may not have events. The question is that many events are irrelevant to you, the GUI takes care of them, i.e., you press the key TAB and GUI moves to other control and you needn't to write a line of code. Or a check box, when clicked it toggles from checked to not checked for you etc. The GUI does a bunch of UI things for you. When you want the program to respond to an event as you want, you create a function and associate it with that event.
The even effect is accomplished with a queue of messages. That is, when user click a button, a message is sent to queue, the loop waiting for messages picks the message from the queue and then executes the action associated with the click event. Usually you don't have to worry about this, it is transparent to you. But it is important to understand that if your event takes a long time, the full GUI will be irresponsive until it finish, all effects in the GUI are messages in the queue and until it picks the next messages, nothing happens it is frozen. So, in long process, you may disable things in the window, to inform the user he can't do anything, and while doing real work tell the GUI to process pending messages, so the user doesn't think the program is frozen. You can also update the window to show progress, but you also must say GUI to process messages queue, because you updates are just messages to the window.
A RAD like Lazarus, creates a form, a window, and lets you drop controls, resize them easily, change some properties, etc. The RAD show you the events each control has, when you click them, it creates am empty function and brings to front the editor to let you write the real code.
It is an over simplification, but hope it has helped a little. But well, as I said, everybody has his own epiphany. Good luck
I've been using the example below to use the Save-as dialog in my console program, and it…
This line:
Writeln(GetSaveFileNameA(@TFilename));What does it write when you select a file vs when you click x/cancel? :-):-)
James wrote 182 lines
Thank you! I somehow missed the result of the function being the status I was looking for. I guess the answer was so easy I couldn't see it 😊
Earlier message 202 lines
Tutorials? well, that is funny. Usually there are many tutorials the other way around,…
Santiago A. wrote 91 lines
Thanks Santiago for all you wrote!
Here is a little that I wrote (back in March 2005): http://codenewsfast.com/cnf/article/0/permalink.art-ng1824q786
That link appears on this page:
http://www.turbocontrol.com/APro.htm
Regards,
Paul
www.ControlPascal.com
