[fpc-pascal] Windows programming tutorials for FPC
Alexander Grotewohl
alex at dcclost.com
Mon Nov 12 17:10:18 CET 2018
This line:
Writeln(GetSaveFileNameA(@TFilename));
What does it write when you select a file vs when you click x/cancel?
:-):-)
On 11/12/2018 4:31 AM, James wrote:
> 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.
>
>
> -----Original Message-----
> From: fpc-pascal <fpc-pascal-bounces at lists.freepascal.org> On Behalf Of Alexander Grotewohl
> Sent: Sunday, November 4, 2018 11:48 AM
> To: fpc-pascal at lists.freepascal.org
> Subject: Re: [fpc-pascal] Windows programming tutorials for FPC
>
> 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 <fpc-pascal-bounces at lists.freepascal.org> On Behalf
>> Of Ewald
>> Sent: Sunday, November 4, 2018 8:06 AM
>> To: fpc-pascal at lists.freepascal.org
>> 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 - fpc-pascal at lists.freepascal.org
>> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>> _______________________________________________
>> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
>> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
> _______________________________________________
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
More information about the fpc-pascal
mailing list