C# 開發Activex

來源:互聯網
上載者:User

 

由於要在網頁上顯示Tree的結構,開始使用JQuery的Tree,但是當資料量很大時會導致第一次載入很慢。(特別慢,資料量太大了網頁一下還反映不過來),於是改用JQuery的FileTree的外掛程式,每層通過ajax的方式到後台擷取資料,結果還好,不過當資料量大的時候,頻繁的點擊頁面,反映也會很慢,而且要求Client的配置好些,而這邊的site上帝配置都比較低,有的機器會導致無反映的情況。無奈改用Activex的方式吧。對vc不是特別的熟悉,於是選擇了C#,因為site上的每個Client都會有.net Framework。OK..開始改寫。

主要參考:http://www.codeproject.com/KB/cs/CreateActiveXDotNet.aspx

1.建立類庫工程

2.添加一個UserControl,AxTree

3.定義AxTree Class的屬性

[ProgId("MyTreeActiveX.AxTree")]
[Guid("A5C532BA-45B2-44c7-ACAE-D526EF9D47B0")]
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComSourceInterfaces(typeof(ControlEvents))]

public partial class AxTree : UserControl, IObjectSafety

說明:

ComVisible:是否可以見

ComSourceInterfaces:該Activex實現的事件的類

IObjectSafety:繼承與該介面,標記安全,否則在生產Activex後,和網頁互動還需要對ie進行設定。

[
      Serializable,
      ComVisible(true)
  ]
   public enum ObjectSafetyOptions
   {
       INTERFACESAFE_FOR_UNTRUSTED_CALLER = 0x00000001,
       INTERFACESAFE_FOR_UNTRUSTED_DATA = 0x00000002,
       INTERFACE_USES_DISPEX = 0x00000004,
       INTERFACE_USES_SECURITY_MANAGER = 0x00000008
   }

[
      ComImport(),
      Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"),
      InterfaceType(ComInterfaceType.InterfaceIsIUnknown)
  ]
  public interface IObjectSafety
  {
      [PreserveSig]
      long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions);

      [PreserveSig]
      long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions);
  };

4.添加方法,屬性

[ComVisible(true)]
       public string Leve1ColorRGB{get..set…}

方法和屬性添加 [ComVisible(true)] 就好

5.添加Event

寫一個Event的介面,ControlEvent

[ComVisible(true)]
    [Guid("5F6E5E2D-8C6D-4524-AD44-E8B169D90371")]
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    public interface ControlEvents
    {
        
        [DispId(0x60020001)]
        void OnDial(string callno, string updeppkey);

    }

在AxTree中定義對應的Event:

public event ControlEventHandler OnDial;

6.實現IObjectSafety介面

#region [IObjectSafety implementation]
        public long GetInterfaceSafetyOptions(ref Guid iid, out int pdwSupportedOptions, out int pdwEnabledOptions)
        {
            pdwSupportedOptions = (int)m_options;
            pdwEnabledOptions = (int)m_options;
            return 0;
        }

        public long SetInterfaceSafetyOptions(ref Guid iid, int dwOptionSetMask, int dwEnabledOptions)
        {
            return 0;
        }
        #endregion

 

7.註冊和反註冊 Atviex

[ComRegisterFunction()]
       public static void RegisterClass(string key)
       {
           // Strip off HKEY_CLASSES_ROOT\ from the passed key as I don't need it
           StringBuilder sb = new StringBuilder(key);
           sb.Replace(@"HKEY_CLASSES_ROOT\", "");

           // Open the CLSID\{guid} key for write access
           RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

           // And create the 'Control' key - this allows it to show up in
           // the ActiveX control container
           RegistryKey ctrl = k.CreateSubKey("Control");
           ctrl.Close();

           // Next create the CodeBase entry - needed if not string named and GACced.
           RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
           inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
           inprocServer32.Close();

           // Finally close the main key
           k.Close();

       }

       [ComUnregisterFunction()]
       public static void UnregisterClass(string key)
       {
           StringBuilder sb = new StringBuilder(key);
           sb.Replace(@"HKEY_CLASSES_ROOT\", "");

           // Open HKCR\CLSID\{guid} for write access
           RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

           // Delete the 'Control' key, but don't throw an exception if it does not exist
           k.DeleteSubKey("Control", false);

           // Next open up InprocServer32
           RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

           // And delete the CodeBase key, again not throwing if missing
           k.DeleteSubKey("CodeBase", false);

           // Finally close the main key
           k.Close();
       }

 

8.產生強命名檔案。xxx.snk.編譯產生…ActvieX的dll寫好了。

 

安裝:

在該解決方案中添加一個安裝項目,選擇項目輸出->主輸出,確認ActiveX的dll檔案的屬性Register:vsdrpCOM,在調整下其他的屬性(檔案名稱..)編譯就好。

cab的安裝製作:

下載cabsdk的工具,微軟網站上有的下。

下載後執行如下:

C:\cabsdk\BIN\MyTreeActiveX>cabarc.exe n TreeActiveX.cab TreeActiveXSetup.msi My
TreeActiveX.inf

Microsoft (R) Cabinet Tool - Version 1.00.0601 (03/18/97)
Copyright (c) Microsoft Corp 1996-1997. All rights reserved.

Creating new cabinet 'TreeActiveX.cab' with compression 'MSZIP':
  -- adding TreeActiveXSetup.msi
  -- adding MyTreeActiveX.inf

Completed successfully

會產生一個TreeActiveX.cab的檔案,網頁上就可以使用了。

對了。inf的定義:

[version]
signature="$CHICAGO$"
AdvancedINF=2.0
[Setup Hooks]
hook1=hook1

[hook1]
run=msiexec.exe /i "%EXTRACT_DIR%\TreeActiveXSetup.msi" /qn

 

網頁的寫法:

<object id="ActivexObject" name="ActivexObject" width="100%" height="100%" classid="clsid:A5C532BA-45B2-44c7-ACAE-D526EF9D47B0"></object>

<script language='javascript'   for='ActivexObject'   event='OnDial(tel,uppkey)'>  

dosame…

</script>

 

通過以上步驟完成ActiveX的編寫,cab的製作和網頁的部分。部署好後,會正常使用。

不過該ActiveX沒有簽名,所有需要設定一下ie,允許下載為簽名的activex才可以正常下載和使用。

如何對ActiveX進行簽名我還不知道,等有時間使用Google大法好好再查查看。

 

收…

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.