free pascal:fpwebview 组件简单易用

从 https://github.com/PierceNg/fpwebview 下载 fpwebview-master.zip 简单易用。

先请看 \fpwebview-master\README.md

cd \lazarus\projects\fpwebview-master\demo\browser_cli

修改 winbuild.bat 如下

@echo offecho Set up FPC executable path.
set fpcexe=D:\lazarus\fpc\3.2.2\bin\x86_64-win64\fpc.exe
其它不改变

修改 browser_cli.lpr  如下,增加了命令行参数。

program browser_cli;{$mode objfpc}{$H+}
{$linklib libwebview}uses{$ifdef unix}cthreads,{$endif}Classes,Process,SysUtils,StrUtils,math,webview;varw: PWebView;url: String;beginif Assigned(InitProc) thenTProcedure(InitProc);{ Set math masks. libwebview throws at least one of these from somewhere deep inside. }SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);url := 'http://localhost/';if ParamCount =1 thenbeginif Length(ParamStr(1))<6 thenurl:= 'http://localhost:' + ParamStr(1)elsebeginif AnsiStartsStr('http', ParamStr(1)) then url:= ParamStr(1)else if AnsiStartsStr('192.', ParamStr(1)) then url:= 'http://' + ParamStr(1)else url:= 'https://' + ParamStr(1);end;endelseurl := ParamStr(1);writeln(url);w := webview_create(WebView_DevTools, nil);webview_set_size(w, 1024, 768, WebView_Hint_None);webview_set_title(w, PAnsiChar('WebView Free Pascal'));webview_navigate(w, PAnsiChar(url));webview_run(w);webview_destroy(w);writeln('Goodbye, webview.');
end.

参考:AnsiStartsStr

注意:pascal 关键字 else 前面的语句一定不能带有分号,否则会产生编译错误。

编译运行 winbuild.bat

修改 winrun.bat  如下

@echo off
@echo browser_cli.exe 
browser_cli.exe  %1

运行 winrun.bat  www.baidu.com  

运行 winrun.bat 8888

默认访问 http://localhost:8888

运行 winrun.bat 192.168.1.101:8080

访问 http://192.168.1.101:8080