从 https://github.com/PierceNg/fpwebview 下载 fpwebview-master.zip 简单易用。
先请看 \fpwebview-master\README.md
cd \lazarus\projects\fpwebview-master\demo\lclembed
修改 lclembed.lpr 如下,将 fphttpapp. 注释掉,因为我用不上。
program lclembed;
{$mode objfpc}{$H+}
{$ifdef darwin}{$linklib libwebview}{$endif}
{$ifdef mswindows}{$linklib libwebview}{$endif}
uses
{$IFDEF UNIX}cmem, cthreads,{$ENDIF}
Interfaces, Forms, math,
classes, fphttpapp, fpwebfile,
guiform;
{$R *.res}
type
TWebServerThread = class(TThread)
protected
procedure Execute; override;
public
constructor Create(CreateSuspended: boolean);
end;
constructor TWebServerThread.Create(CreateSuspended: boolean);
begin
inherited Create(CreateSuspended);
FreeOnTerminate := true;
end;
procedure TWebServerThread.Execute;
begin
//fphttpapp.Application.Run;
end;
var
currDir: String;
begin
{ Set math masks. libwebview throws at least one of these from somewhere deep inside. }
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);
{ Set up embedded webserver. }
GetDir(0, currDir);
TSimpleFileModule.BaseDir := currDir + '/htdocs';
TSimpleFileModule.RegisterDefaultRoute;
TSimpleFileModule.IndexPageName := 'index.html';
MimeTypesFile := 'mime.types';
//fphttpapp.Application.Port := 8000;
//fphttpapp.Application.Threaded := true;
//fphttpapp.Application.Initialize;
//TWebServerThread.Create(false);
{ Set up LCL application. }
RequireDerivedFormResource:=True;
Forms.
Application.Scaled:=True;
Forms.Application.Initialize;
Forms.Application.CreateForm(TForm1, Form1);
Forms.Application.Run;
end.
修改 guiform.pas 如下,我增加一个Edit1 和 Button1
uses
Classes, SysUtils, StrUtils,
procedure TForm1.Button1Click(Sender: TObject);
var url: String;
begin
url:= Edit1.Text;
if AnsiStartsStr('192.', url) then url:= 'http://' + Trim(url)
else if AnsiStartsStr('http', url) then url:= Trim(url)
else url:= 'https://' + Trim(url);
webview_navigate(wvHandle, PAnsiChar(url));
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: char);
begin
if key = #13 then Button1Click(Sender);
end;
其它代码暂时不改变
编写 winbuild.bat 如下,并运行。
@echo off
echo Set up FPC executable path.
set fpcexe=D:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
if not exist "%fpcexe%" (
echo ERROR: Edit this batch file to set up location of fpc.exe
exit /b 1
)
echo "%fpcexe%"
echo Building...
copy "..\..\dll\x86_64\libwebview.a" .
copy "..\..\dll\x86_64\webview.dll" .
copy "..\..\dll\x86_64\WebView2Loader.dll" .
点击菜单:运行:构建,构建成功后再点击 运行
或者 运行 lclembed.exe