​Delphi 开发ActiveX控件(非ActiveForm)

  Delphi 开发ActiveX控件(非ActiveForm)

  Q:为什么不采用ActiveForm工程?通过它可以快速开发带窗体控件,创建过程也非常简单(都不用考虑安全接口问题),很省事!

  A:如果要实现的功能用不上窗口,而一个带窗口的空程序就500多KB,不便于ActiveX的发布,本实现方法可降到100多KB,CAB打包后只有几十KB; 好吧,我承认我有点体积控。

  Delphi 开发ActiveX控件(非ActiveForm) - yyimen - yyimen的博客

2. 在该工程中新建一个 Automation Object对象

  Delphi 开发ActiveX控件(非ActiveForm) - yyimen - yyimen的博客

填写类名

  Delphi 开发ActiveX控件(非ActiveForm) - yyimen - yyimen的博客

3.建一个测试方法(Test)

  Delphi 开发ActiveX控件(非ActiveForm) - yyimen - yyimen的博客

向导会自动生成相关代码!

注意:

  如果需要发布到Internet,还需要实现一个IObjectSafety接口,标记脚本是安全的,运行时才不会被IE阻止。如果想在线安装ActiveX控件还需要数字证书签名后才可以。

代码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

{

* created by HouSoft

* 2011/11/18

}

unit AXDemo_u;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses

ComObj, ActiveX, AXDemo_TLB, StdVcl,

Windows;

type

TAXDemoCtrl = class(TAutoObject, IAXDemoCtrl, IObjectSafety)

FObjectSafetyFlags: DWORD;

{ 实现IObjectSafety 接口用于将控件标记为安全,否则IE加载时可能会被阻止!}

function GetInterfaceSafetyOptions(const IID: TIID; pdwSupportedOptions,

pdwEnabledOptions: PDWORD): HResult; stdcall;

function SetInterfaceSafetyOptions(const IID: TIID; dwOptionSetMask,

dwEnabledOptions: DWORD): HResult; stdcall;

protected

procedure Test(const Msg: WideString); safecall;

implementation

uses ComServ;

function TAXDemoCtrl.GetInterfaceSafetyOptions(const IID: TIID;

pdwSupportedOptions, pdwEnabledOptions: PDWORD): HResult;

var

Unk               : IUnknown;

begin

if (pdwSupportedOptions = nil) or (pdwEnabledOptions = nil) then

Result := E_POINTER;

Result := QueryInterface(IID, Unk);

if Result = S_OK then

pdwSupportedOptions^ := INTERFACESAFE_FOR_UNTRUSTED_CALLER or

INTERFACESAFE_FOR_UNTRUSTED_DATA;

pdwEnabledOptions^ := FObjectSafetyFlags and

(INTERFACESAFE_FOR_UNTRUSTED_CALLER or INTERFACESAFE_FOR_UNTRUSTED_DATA);

pdwSupportedOptions^ := 0;

pdwEnabledOptions^ := 0;

function TAXDemoCtrl.SetInterfaceSafetyOptions(const IID: TIID;

dwOptionSetMask, dwEnabledOptions: DWORD): HResult;

var

Unk               : IUnknown;

begin

Result := QueryInterface(IID, Unk);

if Result <> S_OK then

FObjectSafetyFlags := dwEnabledOptions and dwOptionSetMask;

procedure TAXDemoCtrl.Test(const Msg: WideString);

begin

MessageBox(GetActiveWindow,

PChar(string(Msg)),

initialization

TAutoObjectFactory.Create(ComServer, TAXDemoCtrl, Class_AXDemoCtrl,

ciMultiInstance, tmApartment);

部署测试:

<html>
<head>
<title>ActiveX Test Page</title>
</head>
<H1>ActiveX Test Page</H1>
<hr>
<p>
<body>
<object classid="clsid:C7C16439-AFF0-48A4-A22D-4655400FC4BE" codebase="AXDemo.cab#version=1,0,0,0">
</object>

<script>
functiontry'Hello World!'catch'ActiveX测试失败!\r\n'+</script>

你可能感兴趣的:(​Delphi)