Delphi WebBrowser调用WebSocket 服务

webSocket Demo
  1. 使用工具:WebBrowser
  2. 传输参数:delphi 给js 赋值JSON字符串,js 接收到后通过JSON.parse()方法转换成JSON对象,然后根据需要参数进行读取, Delphi 通过SuperObject 处理JSON数据
  3. 调用JSON方法:WebBrowser1.oleObject.document.parentWindow.execScript('js方法名','JavaScript')
  4. 设置js变量 :WebBroser1.oleObject.document.Script.str:='newvalue'; str为js定义变量
  5. Delphi 触发html 中的Button 单击事件:WebBrowser1.OleObject.document.getElementById("按钮id").click;
unit uWebSocketDemo;
{
  webSocket Demo
  使用工具:WebBrowser
  传输参数:delphi 给js 赋值JSON字符串,js 接收到后通过JSON.parse()方法转换成JSON对象,然后根据需要参数进行读取,
  Delphi 通过SuperObject 处理JSON数据
  调用JSON方法:WebBrowser1.oleObject.document.parentWindow.execScript('js方法名','JavaScript')
  设置js变量 :WebBroser1.oleObject.document.Script.str:='newvalue';  str为js定义变量
  Delphi 触发html 中的Button 单击事件:WebBrowser1.OleObject.document.getElementById("按钮id").click;
  
}
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    WebBrowser1: TWebBrowser;
    Button8: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button8Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}
uses superobject,ShellAPI;

var
  url:string;
  jo:TSuperObject;
  path:string;
const
  sStu='{"title":"我的websocket","content":"websocket测试客户端"}';
procedure TForm1.Button1Click(Sender: TObject);
begin
  webbrowser1.Navigate(url);
  WebBrowser1.Silent:=True;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  url:=ExtractFilePath(ParamStr(0))+'demo-dip.html';
  path:=ExtractFilePath(ParamStr(0))+'\HOAssistant\HOAssistant\HOAssistant.exe'

end;

procedure TForm1.Button2Click(Sender: TObject);
var
  obj:OleVariant;
begin
  WebBrowser1.OleObject.document.Script.str:=sStu;
  WebBrowser1.OleObject.document.parentWindow.execScript('ftest()','JavaScript');

  Memo1.Lines.Add(WebBrowser1.OleObject.document.Script.str);

end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  WebBrowser1.OleObject.document.getElementById('btnlogin').click;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
   WebBrowser1.OleObject.document.getElementById('btnpatient').click;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
     WebBrowser1.OleObject.document.getElementById('btnbingan').click;
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
   WebBrowser1.OleObject.document.getElementById('btnexit').click;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
   webbrowser1.Navigate(url);
   WebBrowser1.Silent:=True;

end;

procedure TForm1.Button8Click(Sender: TObject);
begin
  ShellExecute(Handle,'open',PAnsiChar(path),nil,nil,SW_SHOWNORMAL);
end;

end.
posted @ 2023-04-07 11:39  丹心石  阅读(385)  评论(0编辑  收藏  举报