How to make PBear's THtmlViewer load & show a unicode HTML file?

后端 未结 3 1538
猫巷女王i
猫巷女王i 2021-01-16 11:32

I have some unicode .html files that I want to display inside a THtmlViewer component, in Delphi.

I can\'t seem to persuade the code to work just doing \'.LoadFromF

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-16 11:34

    You are using Delphi 2007. That's before the beginning of Unicode era in Delphi Programming!

    Although it is very tiresome to make Unicode work in early versions of Delphi, it is quite possible to attain a satisfactory result in some controls, especially the THtmlView component.

    I post some code example from one of my programs:

    //code to toggle source or WYSIWYG views
    var
      htmEd: IHTMLDocument2;
    begin
      htmEd := HtmlEdit.Document as IHtmlDocument2;
      if ToggleTabSet.TabIndex = 0 then
      begin
        HtmlEditContainer.PageIndex := 0; // Tab sheet index
        htmEd.body.innerHTML := Memo1.Lines.Text; // TTntMemo
        pnlEditorState.Caption := 'Design View';
      end
      else
        if ToggleTabSet.TabIndex = 1 then
        begin
          HtmlEditContainer.PageIndex := 1;
          Memo1.Lines.Text := HtmEd.body.innerHTML;
          pnlEditorState.Caption := 'Source View';
        end;
    

    Reading the above code you can see that I'm using a TTntMemo component to which the html file is loaded first. I'm then loading the 'Text' of the memo to HtmlView's 'body.innerHTML' property.

    htmEd.body.innerHTML := Memo1.Lines.Text;
    

    Note:

    1. TntWare's 'Memo1.Lines.Text;' is WideString type.
    2. 'IHTMLDocument2' comes from TEmbeddedWB. See reasons for why TEmbeddedWB is good?

    That is what worked for me in early days. I have switched to Delphi 2009, and things are much easier now (just setting suitable TEncoding while loading files)!

提交回复
热议问题