b/s系统中,客户端javascript的执行效率不高,运算速度慢,很多时候不得不采用ocx控件来实现一些功能。将简单的ocx的控件的制作方法提供给大家,一个参考。
打开microsoft visual c++6.0,新建一个mfc activex工程,如下图:
点击确定,出现activex controlwizard,如下图:
选择next出现下来界面
继续选择next,创建工程helloworld:
在helloworldctl.cpp中,修改如下地方:
// dispatch and event ids
public:
enum {
//{{afx_disp_id(chelloworldctrl)
// note: classwizard will add and remove enumeration elements here.
// do not edit what you see in these blocks of generated code !
dispidgethello = 1l,
//}}afx_disp_id
};
};
然后添加申明
begin_dispatch_map(chelloworldctrl, colecontrol)
//{{afx_dispatch_map(chelloworldctrl)
disp_function(chelloworldctrl,"gethello",gethello,vt_bstr, vts_bstr)
//}}afx_dispatch_map
end_dispatch_map()
接着添加成员
// message maps
//{{afx_msg(chelloworldctrl)
// note - classwizard will add and remove member functions here.
// do not edit what you see in these blocks of generated code !
afx_msg bstr gethello(lpctstr strbatchname);
//}}afx_msg
添加方法,这个方法返回"hello world!"加上输入参数strbatchname。
bstr chelloworldctrl::gethello(lpctstr strbatchname)
{
cstring aa = (cstring)strbatchname ;
cstring rtnstr = "hello world!" + aa;
return rtnstr.allocsysstring();
}
最后在build的configration中选择下图方式,编译完成,在release文件夹下产生ocx.
接着就是测试了:
将ocx提取出来,我采用microsoft control pad来编辑,得到ocx控件的classid, 然后创建一个html页面,写入javascrip脚本,调用ocx中的方法。 将编译通过的release目录的helloworld.ocx与html存放在同一文件夹下,打开html页面,页面将谈出对话框,信息为"hello world!-- applegreen".