电竞比分网-中国电竞赛事及体育赛事平台

分享

activex控件的開發(fā)

 ShangShujie 2007-10-11

activex控件的開發(fā)


來源:技術(shù)資料中心   作者:新書城收集整理   發(fā)布時間:2006-7-8 17:07:00   人氣:50

ActiveX控件的開發(fā)

 

    這段時間因?yàn)楣ぷ鞯男枰?,開發(fā)了一個ActiveX控件,采用的開發(fā)工具是VC,希望可以給大家做類似的開發(fā)時提供參考。

 

一,ActiveX控件的功能

1,主要是用來下載和保存指定的網(wǎng)頁。

2,要能夠嵌在網(wǎng)頁中,供服務(wù)器頁面的腳本調(diào)用。

3,提供下載保存到哪個文件夾的窗口,給用戶自己選擇保存位置的機(jī)會。

4,提供程序接口,使得用戶可以保存單個文件。

5,提供程序接口,使得用戶可以保存多個文件,文件名以數(shù)組的方式傳遞過來。

 

二,ActiveX控件的開發(fā)過程

1,通過VCMFC ActiveX  ControlWizard 向?qū)ч_發(fā)這個控件,將項(xiàng)目命名為ActiveX3

2,在向?qū)У?/span>Step2 of 2中為Invisible at runtime打鉤,并去掉Has an About Box前面的鉤,該控件在調(diào)用時則不顯示。

 

3,打開ClassWizard,Automation下添加方法:

1 External Name: DownloadSingle

 Internal Name: DownloadSingle

 Return Type: BOOL

 Parameter List: strDownload  LPCTSTR

         

         該方法的作用:接受并處理參數(shù)為字符串類型的下載請求,同時為操作員提供指定下載目錄的機(jī)會并接受其指定。  

 

(2)    External  Name: DownloadArray

Internal Name: DownloadArray

Return Type: BOOL

Parameter List: arrDownload  Variant

 

         該方法的作用:接受并處理參數(shù)字符串?dāng)?shù)組類型的下載請求,同時為操作員提供指定下載目錄的機(jī)會并接受其指定。

 

4,通過手工添加方法:  

   void TryUrl(CString strAddress)

   DownloadArrayDownloadSingle調(diào)用,用來實(shí)現(xiàn)具體下載功能。

 

三,關(guān)鍵代碼

 1   BOOL CActiveX3Ctrl::DownloadSingle(LPCTSTR strDownload)

{

       // TODO: Add your dispatch handler code here

      char buf[MAX_PATH];

       _getcwd(buf,MAX_PATH);

   location=d:\\;

   TryUrl(strDownload);

       MessageBox("下載完成!");

       return TRUE;

}

 

2, BOOL CActiveX3Ctrl::DownloadArray(const VARIANT FAR& arrDownload)

{

       // TODO: Add your dispatch handler code here

       char buf[MAX_PATH];

       _getcwd(buf,MAX_PATH);

       location=d:\\;

       const VARIANT *varArray;

       if(arrDownload.vt==(VT_VARIANT|VT_BYREF))

       {

          varArray=arrDownload.pvarVal;

       }

       else

       {

          varArray=&arrDownload;

    }

   if((varArray->vt)==(VT_ARRAY | VT_BYREF | VT_VARIANT))

        {

              VARIANT* strarray;

              SAFEARRAY* psa = *(varArray->pparray);

              SafeArrayAccessData(psa,(void**)&strarray);

              UINT uDim = SafeArrayGetDim(psa);

              if(uDim==1)

              {

                     long lLbound,lRbound;

                     SafeArrayGetLBound(psa,1,&lLbound);

                     SafeArrayGetUBound(psa,1,&lRbound);

                     for(long i=lLbound;i<=lRbound;i++)

                     {

                            if(strarray[i].vt==VT_BSTR)

                            {    

                                 TryUrl(strarray[i].bstrVal);

                            }

                     }

              }

              SafeArrayUnaccessData(psa);

       }

    MessageBox("下載完成!");

       return TRUE;

}

3,void CActiveX3Ctrl::TryUrl(CString strAddress)

{

       CFile newFile;

              Cstring fileName=strAddress.Mid(strAddress.ReverseFind(‘/‘)+1); 

              newFile.Open(location+"\\"+fileName,CFile::modeCreate | CFile::modeWrite); 

              CInternetSession session;

       CInternetFile* file=NULL;

       try

         {

              file=(CInternetFile*)session.OpenURL(strAddress);      }

          catch(CInternetException* pEx)

         {

              file=NULL;

              pEx->Delete();

         }

         if(file)

         { 

              int number=0;

              BYTE *buf;

              buf=new BYTE[1024];

              do

              {

              number=file->Read(buf,1024);

           newFile.Write(buf,number);

              }

              while(number>0);

        delete []buf;

         }

         file->Close();

      newFile.Close();

       return;

}

四,測試頁面

<html>

<head>

<title>Download three files from the web.</title>

<script language="javascript">

function download1()

{

var file;

file=document.form1.text1.value;

FavouriteDown.downloadSingle(file);

return;

}

</script>

<script language="VBScript">

<!--

Sub downN_OnClick

Dim s(2)

s(0)=document.form1.text1.value

s(1)=document.form1.text2.value

s(2)=document.form1.text3.value

ActiveXDown.DownloadArray(s)

End Sub

-->

</script>

</head>

<body>

<OBJECT ID="ActiveXDown"

CLASSID="CLSID:ADD73828-78F4-4EF1-8734-61590B044F87"

CODEBASE="ActiveX3.ocx#version=1,0,0,0">

</OBJECT>

<center>

<form name="form1">

<input type="text" name="text1" size="50"><p>

下載一個文件:<input type="button" name="down1" value="down1" onclick="javascript:download1();"><p>

<input type="text" name="text2" size="50"><p>

<input type="text" name="text3" size="50"><p>

下載三個文件:<input type="button" name="downN" value="downN"><p>

</form>

</body>

</html>

 


本文地址:http://www./info/1708.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多