⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testimpl1.pas

📁 详细介绍了DELPHI开始的ActiveX窗体与JAVA的通信。可以实现WEBGIS等功能
💻 PAS
字号:
unit testImpl1;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ActiveX, AxCtrls, testProj1_TLB, StdVcl,shdocvw,mshtml, StdCtrls;

type
  Ttest = class(TActiveForm, Itest)
    Button1: TButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);

  private
    { Private declarations }
    FEvents: ItestEvents;
    FWebBrowser: IWebBrowser2;
    procedure ActivateEvent(Sender: TObject);
    procedure ClickEvent(Sender: TObject);
    procedure CreateEvent(Sender: TObject);
    procedure DblClickEvent(Sender: TObject);
    procedure DeactivateEvent(Sender: TObject);
    procedure DestroyEvent(Sender: TObject);
    procedure KeyPressEvent(Sender: TObject; var Key: Char);
    procedure PaintEvent(Sender: TObject);


    function FindIEWebBrowser: IWebBrowser2;
    function FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;

    function CallScript: Boolean;

  protected
    { Protected declarations }
    procedure DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage); override;
    procedure EventSinkChanged(const EventSink: IUnknown); override;
    function Get_Active: WordBool; safecall;
    function Get_AlignDisabled: WordBool; safecall;
    function Get_AutoScroll: WordBool; safecall;
    function Get_AutoSize: WordBool; safecall;
    function Get_AxBorderStyle: TxActiveFormBorderStyle; safecall;
    function Get_Caption: WideString; safecall;
    function Get_Color: OLE_COLOR; safecall;
    function Get_Cursor: Smallint; safecall;
    function Get_DoubleBuffered: WordBool; safecall;
    function Get_DropTarget: WordBool; safecall;
    function Get_Enabled: WordBool; safecall;
    function Get_Font: IFontDisp; safecall;
    function Get_HelpFile: WideString; safecall;
    function Get_HelpKeyword: WideString; safecall;
    function Get_HelpType: TxHelpType; safecall;
    function Get_KeyPreview: WordBool; safecall;
    function Get_PixelsPerInch: Integer; safecall;
    function Get_PrintScale: TxPrintScale; safecall;
    function Get_Scaled: WordBool; safecall;
    function Get_Visible: WordBool; safecall;
    function Get_VisibleDockClientCount: Integer; safecall;
    procedure _Set_Font(var Value: IFontDisp); safecall;
    procedure Set_AutoScroll(Value: WordBool); safecall;
    procedure Set_AutoSize(Value: WordBool); safecall;
    procedure Set_AxBorderStyle(Value: TxActiveFormBorderStyle); safecall;
    procedure Set_Caption(const Value: WideString); safecall;
    procedure Set_Color(Value: OLE_COLOR); safecall;
    procedure Set_Cursor(Value: Smallint); safecall;
    procedure Set_DoubleBuffered(Value: WordBool); safecall;
    procedure Set_DropTarget(Value: WordBool); safecall;
    procedure Set_Enabled(Value: WordBool); safecall;
    procedure Set_Font(const Value: IFontDisp); safecall;
    procedure Set_HelpFile(const Value: WideString); safecall;
    procedure Set_HelpKeyword(const Value: WideString); safecall;
    procedure Set_HelpType(Value: TxHelpType); safecall;
    procedure Set_KeyPreview(Value: WordBool); safecall;
    procedure Set_PixelsPerInch(Value: Integer); safecall;
    procedure Set_PrintScale(Value: TxPrintScale); safecall;
    procedure Set_Scaled(Value: WordBool); safecall;
    procedure Set_Visible(Value: WordBool); safecall;
    function Get_Property1: Integer; safecall;
    function Get_CallActiveX(const Param2, Param3: WideString): WideString;
      safecall;
    procedure Set_CallActiveX(const Param2, Param3, Value: WideString);
      safecall;


  public
    { Public declarations }
    procedure Initialize; override ;


  end;

implementation

uses ComObj, ComServ;

{$R *.DFM}

{ Ttest }



procedure Ttest.DefinePropertyPages(DefinePropertyPage: TDefinePropertyPage);
begin
  { Define property pages here.  Property pages are defined by calling
    DefinePropertyPage with the class id of the page.  For example,
      DefinePropertyPage(Class_testPage); }
end;

procedure Ttest.EventSinkChanged(const EventSink: IUnknown);
begin
  FEvents := EventSink as ItestEvents;
  inherited EventSinkChanged(EventSink);
end;

procedure Ttest.Initialize;
begin
  inherited Initialize;
  OnActivate := ActivateEvent;
  OnClick := ClickEvent;
  OnCreate := CreateEvent;
  OnDblClick := DblClickEvent;
  OnDeactivate := DeactivateEvent;
  OnDestroy := DestroyEvent;
  OnKeyPress := KeyPressEvent;
  OnPaint := PaintEvent;
end;

function Ttest.Get_Active: WordBool;
begin
  Result := Active;
end;

function Ttest.Get_AlignDisabled: WordBool;
begin
  Result := AlignDisabled;
end;

function Ttest.Get_AutoScroll: WordBool;
begin
  Result := AutoScroll;
end;

function Ttest.Get_AutoSize: WordBool;
begin
  Result := AutoSize;
end;

function Ttest.Get_AxBorderStyle: TxActiveFormBorderStyle;
begin
  Result := Ord(AxBorderStyle);
end;

function Ttest.Get_Caption: WideString;
begin
  Result := WideString(Caption);
end;

function Ttest.Get_Color: OLE_COLOR;
begin
  Result := OLE_COLOR(Color);
end;

function Ttest.Get_Cursor: Smallint;
begin
  Result := Smallint(Cursor);
end;

function Ttest.Get_DoubleBuffered: WordBool;
begin
  Result := DoubleBuffered;
end;

function Ttest.Get_DropTarget: WordBool;
begin
  Result := DropTarget;
end;

function Ttest.Get_Enabled: WordBool;
begin
  Result := Enabled;
end;

function Ttest.Get_Font: IFontDisp;
begin
  GetOleFont(Font, Result);
end;

function Ttest.Get_HelpFile: WideString;
begin
  Result := WideString(HelpFile);
end;

function Ttest.Get_HelpKeyword: WideString;
begin
  Result := WideString(HelpKeyword);
end;

function Ttest.Get_HelpType: TxHelpType;
begin
  Result := Ord(HelpType);
end;

function Ttest.Get_KeyPreview: WordBool;
begin
  Result := KeyPreview;
end;

function Ttest.Get_PixelsPerInch: Integer;
begin
  Result := PixelsPerInch;
end;

function Ttest.Get_PrintScale: TxPrintScale;
begin
  Result := Ord(PrintScale);
end;

function Ttest.Get_Scaled: WordBool;
begin
  Result := Scaled;
end;

function Ttest.Get_Visible: WordBool;
begin
  Result := Visible;
end;

function Ttest.Get_VisibleDockClientCount: Integer;
begin
  Result := VisibleDockClientCount;
end;

procedure Ttest._Set_Font(var Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure Ttest.ActivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnActivate;
end;

procedure Ttest.ClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnClick;
end;

procedure Ttest.CreateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnCreate;
end;

procedure Ttest.DblClickEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDblClick;
end;

procedure Ttest.DeactivateEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDeactivate;
end;

procedure Ttest.DestroyEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnDestroy;
end;

procedure Ttest.KeyPressEvent(Sender: TObject; var Key: Char);
var
  TempKey: Smallint;
begin
  TempKey := Smallint(Key);
  if FEvents <> nil then FEvents.OnKeyPress(TempKey);
  Key := Char(TempKey);
end;

procedure Ttest.PaintEvent(Sender: TObject);
begin
  if FEvents <> nil then FEvents.OnPaint;
end;

procedure Ttest.Set_AutoScroll(Value: WordBool);
begin
  AutoScroll := Value;
end;

procedure Ttest.Set_AutoSize(Value: WordBool);
begin
  AutoSize := Value;
end;

procedure Ttest.Set_AxBorderStyle(Value: TxActiveFormBorderStyle);
begin
  AxBorderStyle := TActiveFormBorderStyle(Value);
end;

procedure Ttest.Set_Caption(const Value: WideString);
begin
  Caption := TCaption(Value);
end;

procedure Ttest.Set_Color(Value: OLE_COLOR);
begin
  Color := TColor(Value);
end;

procedure Ttest.Set_Cursor(Value: Smallint);
begin
  Cursor := TCursor(Value);
end;

procedure Ttest.Set_DoubleBuffered(Value: WordBool);
begin
  DoubleBuffered := Value;
end;

procedure Ttest.Set_DropTarget(Value: WordBool);
begin
  DropTarget := Value;
end;

procedure Ttest.Set_Enabled(Value: WordBool);
begin
  Enabled := Value;
end;

procedure Ttest.Set_Font(const Value: IFontDisp);
begin
  SetOleFont(Font, Value);
end;

procedure Ttest.Set_HelpFile(const Value: WideString);
begin
  HelpFile := String(Value);
end;

procedure Ttest.Set_HelpKeyword(const Value: WideString);
begin
  HelpKeyword := String(Value);
end;

procedure Ttest.Set_HelpType(Value: TxHelpType);
begin
  HelpType := THelpType(Value);
end;

procedure Ttest.Set_KeyPreview(Value: WordBool);
begin
  KeyPreview := Value;
end;

procedure Ttest.Set_PixelsPerInch(Value: Integer);
begin
  PixelsPerInch := Value;
end;

procedure Ttest.Set_PrintScale(Value: TxPrintScale);
begin
  PrintScale := TPrintScale(Value);
end;

procedure Ttest.Set_Scaled(Value: WordBool);
begin
  Scaled := Value;
end;

procedure Ttest.Set_Visible(Value: WordBool);
begin
  Visible := Value;
end;

function Ttest.CallScript: Boolean;
var
  tmpDocument2: IHTMLDocument2;
  tmpDispID: Integer;
  tmpScriptName: WideString;
  tmpDispParams: TDispParams;
  tmpResult: Variant;
  tmpParam1Value, tmpParam2Value: WideString;
  tmpExcepInfo: TExcepInfo;
begin
  try
    tmpDocument2 := FindIEWebBrowser.Document as IHTMLDocument2;
    //获取角本函数的指针,角本函数名为"CallScript"
    tmpScriptName := 'CallScript';
    OleCheck(tmpDocument2.Script.GetIDsOfNames(GUID_NULL, @tmpScriptName, 1, LOCALE_SYSTEM_DEFAULT, @tmpDispID));

    //设置参数个数
    tmpDispParams.cArgs := 2;
    //设置参数值
    New(tmpDispParams.rgvarg);
    tmpParam1Value := 'delphi内部参数';
    tmpDispParams.rgvarg[0].bstrVal := PWideChar(tmpParam1Value);
    tmpDispParams.rgvarg[0].vt := VT_BSTR;
    tmpParam2Value :=edit2.text;
    tmpDispParams.rgvarg[1].bstrVal := PWideChar(tmpParam2Value);
    tmpDispParams.rgvarg[1].vt := VT_BSTR;
    tmpDispParams.cNamedArgs := 0;

    //调用脚本函数
    OleCheck(tmpDocument2.Script.Invoke(tmpDispID, GUID_NULL, LOCALE_SYSTEM_DEFAULT,
      DISPATCH_METHOD, tmpDispParams, @tmpResult, @tmpExcepInfo, nil));
    ShowMessage(tmpResult);
    Result := true;
  except
    Result := false;
  end;
end;



function Ttest.FindIEWebBrowser: IWebBrowser2;
var
  tmpShell: IShellWindows;
  tmpIntf: IDispatch;
  tmpIE: IWebBrowser2;
  i, Count: Integer;
begin
  try
    if FWebBrowser = nil then
    begin
      Count := 0;
      repeat
        tmpSHell := CoShellWindows.Create;
        for i := 0 to tmpShell.Count - 1 do
        begin
          tmpIntf := tmpShell.Item(i);
          if tmpIntf = nil then continue;
          tmpIntf.QueryInterface(IID_IWebBrowser2, tmpIE);
          if tmpIE = nil then Continue;
          if (Integer(Handle) = tmpIE.HWND) or FindIEWindow(Integer(tmpIE.HWND), Handle) then
            FWebBrowser := tmpIE;
        end;
        Inc(Count);
        Sleep(50);
        Application.ProcessMessages;
      until (FWebBrowser <> nil) or (Count > 50);
    end;
    Result := FWebBrowser;
  except
  end;
end;

function Ttest.FindIEWindow(ParentHandle, ChildHandle: HWND): Boolean;
var
  tmpHandle : HWND;
begin
  tmpHandle := GetParent(ChildHandle);
  if tmpHandle = 0 then
  begin
    Result := False;
    Exit;
  end else
  begin
    if tmpHandle = ParentHandle then
    begin
     Result := True;
     Exit;
    end else
    begin
      Result := FindIEWindow(ParentHandle, tmpHandle);
    end;
  end;
end;


procedure Ttest.Button1Click(Sender: TObject);

begin
  CallScript;
end;




function Ttest.Get_Property1: Integer;
begin

end;

function Ttest.Get_CallActiveX(const Param2,
  Param3: WideString): WideString;
begin
  Result := Param2+' '+Param3;
  edit1.Text:=Result;
 // ShowMessage('ActiveX: ' + Result);

end;

procedure Ttest.Set_CallActiveX(const Param2, Param3, Value: WideString);
begin

end;

procedure Ttest.Button2Click(Sender: TObject);
begin
   showmessage('ok');
end;

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    Ttest,
    Class_test,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    tmApartment);


end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -