lazarus在macos里调用外部浏览器打开一个网址

lazarus
跨平台free pascal语言ide工具,社区 http://www.fpccn.com/

下载 ftp://freepascal.dfmk.hu/pub/lazarus/

https://sourceforge.net/projects/lazarus/files/

号称一次编码,到处编译,window linux macos 等各种平台都可用。borland的windows下的delphi也是pascal开发工具但是收费,开源的lazarus免费用用挺好

大的项目不说,小的工具还是值得一用,今天整了一段跨平台代码到macos环境试一下

首先要安装,体验结果ftp下载网址网速不快,晚上的时候http网址也不快到了早上没人用http网址还算比较快

安装完成,根据教程先整合分体式ide为1体式,谁知安装了anchordockingdsgn.lpk相关dock搜索出来的3个包之后,重新构建重启后还是没变化?

原来是重新构建的文件不在系统路径,没权限写进去,在~/.lazarus/bin/lazarus,自己手动复制到/Library/Lazarus/目录下覆盖下即可,修改sourceeditor.pp和sourceeditor.lfm解决源代码编辑器不能输入中文也是注意提前给权限写文件否则只读文件不能编辑


procedure TForm1.FormCreate(Sender: TObject);
var
  IniFile: TIniFile;
begin
  if FileExists(ExtractFilePath(ParamStr(0)) + 'Test.ini') then
  begin
    IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Test.ini');
    Edit1.Text := IniFile.readString('item', 'browser', '');
    IniFile.Free;
  end;
  StatusBar1.SimpleText := '已经加载:' + ExtractFilePath(ParamStr(0)) + 'Test.ini';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  IniFile: TIniFile;
begin
  IniFile := TIniFile.Create(ExtractFilePath(ParamStr(0)) + 'Test.ini');
  IniFile.writeString('item', 'browser', Edit1.Text);
  IniFile.Free;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    Edit1.Text := OpenDialog1.FileName;
end;

procedure TForm1.correctBtnClick(Sender: TObject);
var
  IniFile: TIniFile;
begin
  if FileExists(Edit1.Text) then
    if '.desktop' = ExtractFileExt(Edit1.Text) then
    begin
      IniFile := TIniFile.Create(Edit1.Text);
      Edit1.Text := IniFile.ReadString('Desktop Entry', 'Exec', '');
      IniFile.Free;
    end;
  if '.app' = ExtractFileExt(Edit1.Text) then
  begin
    OpenDialog1.InitialDir := Edit1.Text;
    if OpenDialog1.Execute then
      Edit1.Text := OpenDialog1.FileName;
  end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if pos(' ', Edit1.Text) > 0 then
    StatusBar1.SimpleText :=
      '请检查是否存在空格,请把空格和之后的参数去掉';
  if '.desktop' = ExtractFileExt(Edit1.Text) then
    StatusBar1.SimpleText :=
      'linux环境请检查是否是.desktop文件,是的话需要修正';
  if '.app' = ExtractFileExt(Edit1.Text) then
    StatusBar1.SimpleText :=
      'macos环境请检查是否是.app目录, 是的话需要修正';

  if FileExists(Edit1.Text) then
  begin
    attr := FileGetAttr(Edit1.Text);
    if (attr and faSymLink) > 0 then
      StatusBar1.SimpleText :=
        '如果不能正常运行,请检查是否是链接,尝试原文件.文件属性:'+IntToStr(attr);
  end;
end;

//x-www-browser     http://www.baidu.com
procedure TForm1.testBtnClick(Sender: TObject);
var
  Process: TProcess;
begin
  if pos(' ', Edit1.Text) > 0 then
    StatusBar1.SimpleText := '请检查可执行文件路径是否存在空格';
  Process := TProcess.Create(nil);
  try
    //ExecuteProcess The calling process runs synchronously: it 'hangs' until the external program has finished
    //ExecuteProcess(MarshaledAString(UTF8String(Edit1.Text)),'https://blog.csdn.net/qiaozhangchi',[ExecInheritsHandles]);
    // if RunCommand(Edit1.Text,['https://blog.csdn.net/qiaozhangchi'],s) then
    Process.InheritHandles := False;
    Process.Options := [];
    Process.ShowWindow := swoShow;
    Process.Executable := Edit1.Text;
    Process.Parameters.Add('https://blog.csdn.net/qiaozhangchi');
    Process.Execute;
    StatusBar1.SimpleText := '已经执行';
  except
    on E: EOSError do
      StatusBar1.SimpleText :='请检查文件路径是否有空格或不能直接执行的后缀名,请修正';
  end;
  Process.Free;
end;  

跨平台代码就是好,这一段代码包含几个知识点

  • ini文件读写
  • TProcess启动另外程序
  • 打开文件对话框
  • 状态栏控件
由于lazarus中的例子: lazarus\fpc\2.6.0\source\packages\fcl-web\examples\webdata\demo 用的extjs是3.4版,在新的extjs4.2环境下不能运行,费了好大劲,终于搞定。期间到laz英文论坛提问,可能跟我英文差有关吧,一个回答的都没有。搞定后,版主又要我进行了“ open a new issue in the bugtracker and add a diff with the changes ”。本着互助、共享的原则,整个东西放到lazarus中文社区论坛,欢迎大家不断改善源码,提高性能,并且将改善后的源码进行共享。 我的环境:winxp,lazarus1.1,fpc2.6.0,apache2.2,extjs4.2。 不要犯怵,安装配置很简单,运行这个demo一行代码都不需要编写。后面我会详细讲安装运行方法。 安装配置: 1、安装Apache。下载地址:http://www.fayea.com/apache-mirror//httpd/binaries/win32/httpd-2.2.22-win32-x86-openssl-0.9.8t.msi 其他版本我没试过,高版本的应该可以。 我安装到了D:\apache2.2 2、下载安装Extjs4.2。下载地址:http://cdn.sencha.io/ext-4.2.0-beta.zip?ref=extjs.org.cn 下载后,解压缩,文件夹复制到 D:\apache2.2\htdocs\,然后改名为Ext。 3、下载附件。 原来的lazarus demo 源码未做任何修改,直接编译即可。编译之前需要安装weblaz和lazwebextra两个包。 本文附件中已经有extgrid.exe,可以直接使用。 将extgrid.exe和users.dbf复制到 apache2.2\cgi-bin 目录下。 将附件中其余文件复制到 apache2.2\htdocs\demodbf 目录下。 好了,全部配置完成。 4、启动Apache,访问:http://localhost/demodbf/extgrid-json.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值