Recent

Author Topic: [Solved] THtmlViewer - Embed HTML in Design-Time  (Read 1843 times)

OLLI_S

  • Full Member
  • ***
  • Posts: 111
[Solved] THtmlViewer - Embed HTML in Design-Time
« on: February 28, 2019, 01:53:09 pm »
Hello,

I am using Lazarus 1.8.4 in Windows 10 Professional x64.
I know that Lazarus 2.0 is out now, just had no time to install...

On my form I have a THtmlViewer and I can lad an existing HTML file while my application is running:
Code: Pascal  [Select]
  1. HtmlViewer1.LoadFromFile('filename.html');

In the past I used a TRichMemo and here I have the property RTF where I can load the RTF Document in Design time (so I don't have to deliver the RTF document).
Is his also possible with the THtmlViewer?
I did not find an property that can load and HTML text.

Of cause I can define a constant:
Code: Pascal  [Select]
  1.   HTML_Help_Text =
  2.     '<!DOCTYPE html>' +
  3.     '<html lang="en">' +
  4.     '  <head>' +
  5.     '    <title>Title of the document</title>' +
  6.     '    <style>' +
  7.     '      h1 {' +
  8.     '            font-family: Arial;' +
  9.     '            color:#2E74B5;' +
  10.     '            font-size:16.0pt;' +
  11.     '            font-weight:bold;' +
  12.     '         }' +
  13.     '      p {' +
  14.     '            font-family: Arial;' +
  15.     '            font-size:11.0pt;' +
  16.     '        }' +
  17.     '    </style>' +
  18.     '  </head>' +
  19.     '  <body>' +
  20.     '    <h1>Headline</h1>' +
  21.     '    <p>' +
  22.     '      Here is some text.<br>' +
  23.     '      Here is some more text.' +
  24.     '    </p>' +
  25.     '    <p>' +
  26.     '      <span style="color:#C00000;font-weight:bold;">Attention</span><br>' +
  27.     '      Here is some mote text' +
  28.     '    </p>' +
  29.     '  </body>' +
  30.     '</html>';

The whole text (without the HTML code) I want to display will be 4 pages printed out.
Such a long text inflates the code and makes editing the document very difficult.
So is there a comfortable way to insert an existing HTML code into the THtmlViewer without having to define such a huge constant?
Thank you!

Best regards

OLLI
« Last Edit: March 02, 2019, 04:56:02 pm by OLLI_S »

correa.elias

  • New member
  • *
  • Posts: 18
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #1 on: February 28, 2019, 02:19:45 pm »
Maybe embedding your html as a resource?

http://wiki.freepascal.org/Lazarus_Resources

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #2 on: February 28, 2019, 02:28:09 pm »
Puh, did never do this before.
I am on a beginner level....

I saw in the Project Inspector that I can add the HTML file.
So is this a good way?
« Last Edit: February 28, 2019, 04:12:42 pm by OLLI_S »

lucamar

  • Hero Member
  • *****
  • Posts: 1546
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #3 on: February 28, 2019, 05:43:11 pm »
I saw in the Project Inspector that I can add the HTML file.
So is this a good way?

No; it just adds the file to the list of "files in this project". I do it a lot for readme, history, changelog, etc. files and the only thing it does is acknowledge they are there and that they belong to the project.

What you need is to add the HTML file as a resource: go to "Project->Project options ..." click on "Resources" then the "add" button, etc. Then, in your program, do a LoadFromResource or similar to use it.

Check the help for the fcl-res package; and IIRC there are also some pages in the wiki about this topic.
« Last Edit: February 28, 2019, 05:53:27 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.

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #4 on: March 01, 2019, 01:13:12 pm »
Hello lucamar,

I can't get that working.

I Lazarus I added the HTML file like you suggested.
See attached screen shot.

I had a look at http://wiki.freepascal.org/Lazarus_Resources
At this wiki page I found the following example that looks very promising:

Code: Pascal  [Select]
  1. uses ...LResources...;
  2.  
  3. ...
  4. procedure TForm1.FormCreate(Sender: TObject);
  5. var
  6.   r: TLResource;
  7.   Data: String;
  8. begin
  9.   r:=LazarusResources.Find('datafile1');
  10.   if r=nil then raise Exception.Create('resource datafile1 is missing');
  11.   Data:=r.Value;
  12.   ...do something with the data...
  13. end;

So I adapted it to my code:

Code: Pascal  [Select]
  1. var
  2.   ChangeLog_Text: String;
  3.   MyResource: TLResource;                    
  4.  
  5. begin
  6.   MyResource := LazarusResources.Find('CHANGELOG');
  7.   if  MyResource = nil then raise Exception.Create('resource CHANGELOG is missing');
  8.   ChangeLog_Text := MyResource.value;
  9.   ShowMessage(ChangeLog_Text);
     

But here the Exception is raised.
Same if I create the TLResource in the first line of the code:
Code: Pascal  [Select]
  1.   MyResource :=  TLResource.Create;

What is wrong?

Best regards

OLLI
« Last Edit: March 01, 2019, 01:14:52 pm by OLLI_S »

correa.elias

  • New member
  • *
  • Posts: 18
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #5 on: March 01, 2019, 02:49:06 pm »
Missing {$I mylazarusresource.lrs} in your initialization section?

lucamar

  • Hero Member
  • *****
  • Posts: 1546
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #6 on: March 01, 2019, 02:53:38 pm »
Use the example way below that: Using ressources in your program.

It uses TResourceStream which it's easier to work with for what you want; instead of saving it to a file read the ressource to p.e. a string list (with StringList.LoadFromStream).

I'll see if I can write a small example later on...

Missing {$I mylazarusresource.lrs} in your initialization section?

I don't think so: The lrs is used to contain the resources for binary conversion while compiling but the ressources themselves should be automaticaly linked into the executable.
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.

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #7 on: March 02, 2019, 01:55:12 pm »
Hello lucamar,

I renamed the resource to HTMLHELP (in the Project Options Dialog, where I add the resource).
To make sure that the resource name is not the problem.

Now I have the following code:

Code: Pascal  [Select]
  1. var
  2.   MyStringList: TStringList;
  3.   MyResourceStream: TResourceStream;
  4.  
  5. begin
  6.   ShowMessage('Loading Resources...);
  7.  MyResourceStream := TResourceStream.Create(HInstance, 'HTMLHELP', 'HTML');
  8.  MyStringList := TStringList.create;
  9.  MyStringList.LoadFromStream(MyResourceStream);
  10.  ShowMessage(MyStringList.Text);      

Here I get the error message Resource "HTMLHELP" not found.

Best regards

OLLI

lucamar

  • Hero Member
  • *****
  • Posts: 1546
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #8 on: March 02, 2019, 02:34:57 pm »
I renamed the resource to HTMLHELP (in the Project Options Dialog, where I add the resource).
To make sure that the resource name is not the problem.

Now I have the following code:
[... some code ...]
Here I get the error message Resource "HTMLHELP" not found.

Are you sure the resource type is (and is declared as) "HTML"?
And, can you use some rersource tool (ResourceHacker or similar) to see exactly what your resources look like in the EXE?
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.

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #9 on: March 02, 2019, 02:57:07 pm »
I renamed the resource to TEXT_HELP so maybe there is a conflict with the HTML component.
But it did not help.

It is defined as HTML as you can see in the attached screen shot.

I opened my EXE in the Resource Hacker 5.1.7.
Here I see in the tree a folder HTML and in there I see TEXT_HELP.
If I click it, I see exactly the HTML file.
See attached screen shot.

Could it be an issue with compile options?
I added the compile profiles for Debug and Release.
But then the HTML would not be in the EXE...
« Last Edit: March 02, 2019, 03:00:19 pm by OLLI_S »

Cyrax

  • Hero Member
  • *****
  • Posts: 727
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #10 on: March 02, 2019, 04:33:04 pm »
Code: Pascal  [Select]
  1. MyResourceStream := TResourceStream.Create(HInstance, 'TEXT_HELP', 'HTML');

You have the resource name wrong.

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #11 on: March 02, 2019, 04:39:41 pm »
Code: Pascal  [Select]
  1. MyResourceStream := TResourceStream.Create(HInstance, 'TEXT_HELP', 'HTML');

You have the resource name wrong.

No, Cyrax, I have not (would love that this is the reason).

When I posted my code, I had the old resource name.
Then I renamed the resource and also adjusted my code (but did not post the new code here).
Maybe you have overseen this when you read my postings.

So I attached again the current screen shot of the properties widow to this posting.

And here is the current code:
Code: Pascal  [Select]
  1. var
  2.   MyStringList: TStringList;
  3.   MyResourceStream: TResourceStream;  
  4.  
  5. begin
  6.  
  7.   ShowMessage('Loading Resources...');
  8.   MyResourceStream := TResourceStream.Create(HInstance, 'TEXT_HELP', 'HTML');
  9.   MyStringList := TStringList.create;
  10.   MyStringList.LoadFromStream(MyResourceStream);
  11.   ShowMessage(MyStringList.Text);      

Cyrax

  • Hero Member
  • *****
  • Posts: 727
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #12 on: March 02, 2019, 04:45:16 pm »
Okay, then try this snippet of code.

Code: Pascal  [Select]
  1. MyResourceStream := TResourceStream.Create(HInstance, 'TEXT_HELP', RT_HTML);

OLLI_S

  • Full Member
  • ***
  • Posts: 111
Re: THtmlViewer - Embed HTML in Design-Time
« Reply #13 on: March 02, 2019, 04:54:36 pm »
Okay, then try this snippet of code.

Code: Pascal  [Select]
  1. MyResourceStream := TResourceStream.Create(HInstance, 'TEXT_HELP', RT_HTML);

Hello Cyrax,

perfectly, it is working now!!!!
Thank you very very much!

OLLI