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".
|