用vb编写一个简单的ocx和dll,这2种都是属于active类型的。
active ocx 和active dll的区别,可以简单的认为ocx是用来实现显示界面用的,而dll是实现函数调用的。
1.打开vb建立active ocx,在usercontrol上添加一个text1控件。
编写代码:
Public Property Get Text() As String Text = Text1.Text End Property Public Property Let Text(ByVal value As String) Text1.Text = value PropertyChanged "Text" End Property Private Sub UserControl_ReadProperties(PropBag As PropertyBag) '读属性 Text1.Text = PropBag.ReadProperty("Text", "Text1") End Sub Private Sub UserControl_WriteProperties(PropBag As PropertyBag) '写属性 PropBag.WriteProperty "Text", Text1.Text, "Text1" End Sub
2.建立index.htm
代码:
<html> <head> <mce:script type= "text/javascript" language="javascript"><!-- function t(){ var k=document.getElementById("ocxtype").text; alert(k); } // --></mce:script> </head> <body> <FORM ACTION="s.asp" METHOD="post"> <OBJECT id=ocxtype height=100px width=300px classid=CLSID:D6F5075B-250E-47BF-9CF4-556D1E1E251A> <PARAM NAME="_ExtentX" VALUE="7567"> <PARAM NAME="_ExtentY" VALUE="3969"> <PARAM NAME="Text" VALUE="pathtext"> </OBJECT> <br> <input type=text value="ok" name="ok" οnclick="t();"/> FirstName:<INPUT TYPE="TEXT" NAME="FirstName"> LastName:<INPUT TYPE="TEXT" NAME="LastName"> <INPUT TYPE="SUBMIT" VALUE="Send"> </FORM> </body> </html>
classid=CLSID:D6F5075B-250E-47BF-9CF4-556D1E1E251A
是ocx的id,这个去注册表找的。也可以通过其他方法查找。
asp:代码
<%
response.write "count=" & request.form.count & "<br>"
For Each objItem In Request.Form
Response.Write objItem & "=" & Request.Form(objItem) & "<br>"
next
response.write "testtext=" & request.form("testtext") & "<br>"
'obj = Server.CreateObject("Text.u")
'temp=obj.text
response.write temp & "<br>"
%>
========================下面是查看ocx的属性和方法的脚本====================================
<html>
<head>
<title>查看OCX的属性和方法的脚本</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<script language="JavaScript">
var flag=true
function showall(){
alist = new Array();
newocx();
list1.value = "";
list2.value = "";
list1.value = ocxtype.outerHTML;
j = 0;
for (i in ocxtype)
{
alist[j] = i;
j++;
}
for (i = 0; i < alist.length; i++)
for (j = (alist.length-1); j > 0; j--)
{
if (alist[j] < alist[j-1])
{
temps = alist[j];
alist[j] = alist[j-1];
alist[j-1] = temps;
}
}
for (i = 0; i < alist.length; i++)
list2.value += alist[i] + "/n";
alert("共有 "+alist.length+" 个属性方法")
}
function newocx()
{
ocxfield.innerHTML = "<OBJECT ID='ocxtype' WIDTH='0px' HEIGHT='0px'" +
"CLASSID='CLSID:" + ocxid.value + "'></OBJECT>";
}
</script>
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div align="center">
<p>classid
<input type="text" id="ocxid" style="width:400" value="F3A614DC-ABE0-11d2-A441-00C04F795683" οnchange="jscript:flag=true">
<input type="button" id="look" value="查看" οnclick="showall()">
</p>
<table width="75%" border="1">
<tr>
<td height="24">
<div align="center">html中的写法</div>
</td>
<td height="24">
<div align="center">OCX中的属性事件</div>
</td>
</tr>
<tr>
<td>
<textarea id="list1" style="width: 500px; height: 400px;" readonly></textarea>
</td>
<td>
<textarea id="list2" style="width:250px; height:400px;" readonly></textarea>
</td>
</tr>
</table>
</div>
<div id="ocxfield">
</div>
</body>
</html>
=================================================================================
建立active dll
1.编写代码:
publicfunctionadd(a as integer,b as integer) as integer
add=a+b
end function
2.编写代码html
<script>
function add(a,b)
var sum=new ActiveXObject("工程名.类名");
sum.add(a,b);
end function
</script>