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
Okay, well here's the guts of what I came up. Constructive criticism and observations appreciated!
// load either an ansi or unicode-type html doc into the browser component. // the filename has already been confirmed as an existing file procedure TfrmBrowser.LoadDocument(FFileName:string); var FWideText : Widestring; FAnsiText : AnsiString; FRequiredLen : Integer; FFileStream : TFileStream; FMemStream : TMemoryStream; FBuffer : Byte; begin FFileStream := TFileStream.Create(FFileName, fmOpenRead or fmShareDenyNone); // anything less than half a dozen bytes would be pointless, but... if FFileStream.Size>1 then begin // checking the first byte of the file to give us a clue about file-type FFileStream.Read(FBuffer,1); FFileStream.Position:=0; // rewind position if (FBuffer=$FF) or (FBuffer=$EF) then begin // probably Unicode FRequiredLen := FFileStream.Size div 2; // 2 bytes per char SetLength(FWideText, FRequiredLen); FFileStream.Read(FWideText[1], FFileStream.Size); // cast it into an Ansistring FAnsiText := FWideText; FMemStream := TMemoryStream.Create; FMemStream.Write(FAnsiText[1], FRequiredLen); FMemStream.Position := 0; // rewind the position // load the stream into the THtmlViewer vwBrowse.LoadFromStream(FMemStream); FMemStream.Free; end else begin // probably Ansi, just load original filestream in vwBrowse.LoadFromStream(FFileStream); end; FFileStream.Free; end;
Obviously missing some error-trapping, but that's the basic idea.