Recent

Author Topic: [SOLVED] Placing Opening Code in EXE for Opening Associate Files  (Read 668 times)

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Hi,


I am making a "reader" app.
After a user associates a file type in Windows and they double click it to open it...

Where do I place my Opening code so that when the user double-clicks on file type my app it will run the opening file procedure?

Where do I put my code? FormCreate, Active or Show?

Thanks
« Last Edit: July 13, 2019, 03:00:30 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

Ally

  • New member
  • *
  • Posts: 5
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #1 on: July 12, 2019, 06:27:15 pm »
Code: Pascal  [Select]
  1. procedure TfrmFrmMain.FormShow(Sender: TObject);
  2. begin
  3.   if (ParamCount > 0) and (FileExists(ParamStrUTF8(1))) and (UTF8LowerCase(ExtractFileExt(ParamStrUTF8(1))) = '.xyz') then
  4.     OpenMyFile(ParamStrUTF8(1));
  5. end;
  6.  

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #2 on: July 12, 2019, 06:29:12 pm »
The OnCreate handler of the main form is more or less the"standard" place, because normally it's called only once at the start of the program.

I have seen people put it (conditionally) in the OnShow event and I myself use the OnActivate handler when I have first to make sure the form and controls are completely initialized before loading any file.

But most of the times, the OnCreate handler is the proper one.

Code: Pascal  [Select]
  1. procedure TfrmFrmMain.FormShow(Sender: TObject);
  2. begin
  3.   if (ParamCount > 0) and (FileExists(ParamStrUTF8(1))) and (UTF8LowerCase(ExtractFileExt(ParamStrUTF8(1))) = '.xyz') then
  4.     OpenMyFile(ParamStrUTF8(1));
  5. end;
  6.  

The problem with that code is that the OnShow event is trigered on many conditions, not just on starting the program, so you keep (re-)loading the parameters at odd moments during the application run-time.

If for whatever reason you want to make it in OnShow, use something like:
Code: Pascal  [Select]
  1. procedure TfrmFrmMain.FormCreate(Sender: TObject);
  2. begin
  3.   ParamsLoaded := False;
  4. end;
  5.  
  6. procedure TfrmFrmMain.FormShow(Sender: TObject);
  7. begin
  8.   if not ParamsLoaded then begin
  9.     if (ParamCount > 0) and (FileExists(ParamStrUTF8(1))) and (UTF8LowerCase(ExtractFileExt(ParamStrUTF8(1))) = '.xyz') then
  10.       OpenMyFile(ParamStrUTF8(1));
  11.     ParamsLoaded := True;
  12.   end;
  13. end;

Or if you use OnShow just for that:
Code: Pascal  [Select]
  1. procedure TfrmFrmMain.FormShow(Sender: TObject);
  2. begin
  3.   OnShow := Nil; {Make sure it's called only once}
  4.   if (ParamCount > 0) and (FileExists(ParamStrUTF8(1))) and (UTF8LowerCase(ExtractFileExt(ParamStrUTF8(1))) = '.xyz') then
  5.     OpenMyFile(ParamStrUTF8(1));
  6. end;
« Last Edit: July 12, 2019, 06:37:26 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #3 on: July 12, 2019, 08:56:18 pm »
Thanks... I will give it a go.
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #4 on: July 12, 2019, 09:40:00 pm »
three things...

I don't know anyhing about this code.

1) I get error with "ParamsLoaded" and "UTF8LowerCase" in the onShow event

What is supposed to be my uses declaration and any var or const declaration?


2) What is my procedure code block supposed to look like?


Code: Pascal  [Select]
  1. procedure TForm1.OpenMyFile(Sender: TObject;);  //procedure TForm1.OpenMyFile(ParamStrUTF8(1));
  2. begin
  3.        {my code that loads file - see #3 below}
  4. end;  
  5.  


3) This is the code that woks opening a file using DLG.
Not sure how I pass the file name when opening from an associated file.


Code: Pascal  [Select]
  1. oDlg.Filter:='My File Formats (*.mytxt)|*.mytxt';
  2. oDlg.Filename:='';
  3.     if oDlg.Execute then
  4.        KMemo1.Clear;
  5.        fName:=oDlg.Filename;
  6.        ext:=ExtractFileExt(oDlg.FileName.ToLower);
  7.        if ext = '.mytxt' then
  8.              begin
  9.                   L := TStringList.Create;
  10.                 try
  11.                   L.LoadFromFile(fName);
  12.                   s := CP1252ToUTF8(L.Text);
  13.                   KMemo1.Text := s;
  14.                 finally
  15.                   L.Free;
  16.                 end
  17.              end;
  18.  
  19.  
  20.  

« Last Edit: July 12, 2019, 09:43:31 pm by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lainz

  • Hero Member
  • *****
  • Posts: 3139
    • Lainz
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #5 on: July 12, 2019, 11:03:13 pm »
like this
Code: Pascal  [Select]
  1. procedure TForm1.OpenMyFile(aFilename:string);
  2. var L: TStringList;
  3. s: string;
  4. begin
  5.        if LowerCase(ExtractFileExt(aFilename)) = '.mytxt' then
  6.              begin
  7.                  KMemo1.Clear;
  8.                  L := TStringList.Create;
  9.                 try
  10.                   L.LoadFromFile(aFilename);
  11.                   s := CP1252ToUTF8(L.Text);
  12.                   KMemo1.Text := s;
  13.                 finally
  14.                   L.Free;
  15.                 end
  16.              end;
  17. end;

Code: Pascal  [Select]
  1. OpenMyFile(ParamStrUTF8(1));

then you can replace the dialog function
Code: Pascal  [Select]
  1. oDlg.Filter:='My File Formats (*.mytxt)|*.mytxt';
  2. oDlg.Filename:='';
  3.     if oDlg.Execute then
  4.       OpenMyFile(oDlg.Filename);
« Last Edit: July 12, 2019, 11:13:23 pm by Lainz »

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #6 on: July 13, 2019, 12:09:46 am »
I've attached a small, qucik but rather complete example of how it may be done.

About your questions:

Quote from: pixelink
1) I get error with "ParamsLoaded" and "UTF8LowerCase" in the onShow event

ParamsLoaded  was intended to be a field or var that you add to your code. UTF8LowerCase is in the unit lazutf8, of the LazUtils package.


Quote
2) What is my procedure code block supposed to look like?

3) This is the code that woks opening a file using DLG.
Not sure how I pass the file name when opening from an associated file.

See the attached example; it shows it rather well (if I can say so myself ;))

One thing: Instead of a string list my code uses ReadFileToString().

Doing so takes out all the (small) complexity of creating the list, making it parse Text, freeing it, etc. and makes the code a little terser and simpler, but it may look "strange" if you normally use string lists for this.
« Last Edit: July 13, 2019, 12:24:00 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #7 on: July 13, 2019, 12:30:18 am »
I've attached a small, qucik but rather complete example of how it may be done.

About your questions:

Quote from: pixelink
1) I get error with "ParamsLoaded" and "UTF8LowerCase" in the onShow event

ParamsLoaded  was intended to be a field or var that you add to your code. UTF8LowerCase is in the unit lazutf8, of the LazUtils package.


Quote
2) What is my procedure code block supposed to look like?

3) This is the code that woks opening a file using DLG.
Not sure how I pass the file name when opening from an associated file.

See the attached example; it shows it rather well (if I can say so myself ;))

One thing: Instead of a string list my code uses ReadFileToString().

Doing so takes out all the (small) complexity of creating the list, making it parse Text, freeing it, etc. and makes the code a little terser and simpler, but it may look "strange" if you normally use string lists for this.

@LUCAMAR
Actually, it does work perfect if I say so myself :)

You are pretty good at this stuff.

I got a ways to go :)

THANKS....
I will study it and figure it out so I can finish my app.
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #8 on: July 13, 2019, 01:12:50 am »
Glad it helped :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #9 on: July 13, 2019, 01:40:13 am »
I am getting an error.... ReadFileToString not found.

I don't see where this is declared.
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #10 on: July 13, 2019, 01:48:12 am »
Never mind... figured it out.
Went online to looked it up and I see I forgot the FileUtil uses
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #11 on: July 13, 2019, 02:22:05 am »
Sorry, I thought that was automatically added? It always is in my projects but I've tweaked so many things ...

What I usually do when that happens is open lhelp and search in the docs. Just to avoid a trip online :)

Then I also have a kind a series of indexes to the help files made with chmls and filtered through a small utility of mine. That allows me to do a quick grep to know if something is in the docs before opening the CHMs, which can be somewhat slow (lcl.chm being the worst, of course).

But al this is OT, sorry :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #12 on: July 13, 2019, 02:25:21 am »
Sorry, I thought that was automatically added? It always is in my projects but I've tweaked so many things ...

What I usually do when that happens is open lhelp and search in the docs. Just to avoid a trip online :)

Then I also have a kind a series of indexes to the help files made with chmls and filtered through a small utility of mine. That allows me to do a quick grep to know if something is in the docs before opening the CHMs, which can be somewhat slow (lcl.chm being the worst, of course).

But al this is OT, sorry :-[

Don't apologize, it was in your sample. I just didn't copy that part.

:)
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060

lucamar

  • Hero Member
  • *****
  • Posts: 1814
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #13 on: July 13, 2019, 02:28:05 am »
Don't apologize, it was in your sample. I just didn't copy that part.

Oh! Ok, that's reasuring  :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 1.8.4 & 2.0.2 w/FPC 3.0.4 on:
(K|L)Ubuntu 12..16, Windows XP SP3, various DOSes.

pixelink

  • Hero Member
  • *****
  • Posts: 935
    • Pixelink Media
Re: Placing Opening Code in EXE for Opening Associate Files
« Reply #14 on: July 13, 2019, 03:00:11 am »
Alright....

My end game was to be able to open many different file formats, both known (txt, rtf) and my own proprietary formats which I will making in the future.
Plus they can be associated files.

I was able to use some of your code and I left the parts out that targeted just the 'mytxt" format and the Boolean pre-Check.

All works perfectly.

FYI, when I upload this updated version tomorrow, It will have a messagebox (see screen below) that gives you credit.


The project is here (doesn't have your contributed code yet though)
https://www.pixelink.media/pixelreader.php


THANK YOU FOR ALL YOUR HELP!


« Last Edit: July 13, 2019, 03:08:09 am by pixelink »
Can't Type - Forgetful - Had Stroke = Forgive this old man!


Pixelink Media •  Pixelink Forum
LAZ 2.02 • Win10 • 16G RAM • Nivida GForce RTX 2060