您的位置:首页 > 其它

.net创建一个ActiveX控件并使用的简单例子

2008-08-04 10:23 821 查看
在很多情况下,客户往往需要在客户端实现一些复杂的功能,有些可以通过AJAX来实现,有些则是现有客户端脚本不能完成的,此时则必须通过ActiveX控件来实现。

这里给出一个用.net编写简单的ActiveX并在网页中应用的例子:

///////////////////////////////////////////////////////////////////////////////////

// AxComp.cs

// Creates a simple ActiveX component.

//

// 1. Compile into a class library, or use:

// csc /t:library AxComp.cs

// 2. Register and generate the Typelib on the client's machine with:

// regasm AxComp.dll /tlb:AxCompNet.dll /codebase

///////////////////////////////////////////////////////////////////////////////////

using System;

using System.Runtime.InteropServices;

namespace AxComponent

{

public interface IAxTest

{

string CallIt();

}

[ClassInterface(ClassInterfaceType.AutoDual)]

public class AxComp : IAxTest

{

public string CallIt()

{

return "This is a test.";

}

}

}

Code

///////////////////////////////////////////////////////////////////////////////////

// Tester.htm

// Test the ActiveX component.

///////////////////////////////////////////////////////////////////////////////////

<html>

<head>

<script language=JavaScript>

var g_axComponent = new ActiveXObject('AxComponent.AxComp');

alert(g_axComponent.CallIt());

</script>

</head>

<body>

<h1>Tester.htm</h1>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: