从 https://github.com/PierceNg/fpwebview 下载 fpwebview-master.zip 简单易用。
先请看 \fpwebview-master\README.md
cd \lazarus\projects\fpwebview-master\demo\lclembed
修改 lclembed.lpr 如下,将 fphttpapp. 注释掉,因为我用不上 a simple webserver。
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}typeTWebServerThread = class(TThread)protectedprocedure Execute; override;publicconstructor Create(CreateSuspended: boolean);end;constructor TWebServerThread.Create(CreateSuspended: boolean);
begininherited Create(CreateSuspended);FreeOnTerminate := true;
end;procedure TWebServerThread.Execute;
begin
//fphttpapp.Application.Run;
end;varcurrDir: 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
usesClasses, SysUtils, StrUtils,procedure TForm1.Button1Click(Sender: TObject);
var url: String;
beginurl:= 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);
beginif key = #13 then Button1Click(Sender);
end;其它代码暂时不改变
编写 winbuild.bat 如下,并运行。
@echo offecho 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.exeexit /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