C# Activex 與 javascript 互動

來源:互聯網
上載者:User

[Guid("CB5BDC81-93C1-11CF-8F20-00805F2CD064"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IObjectSafety
    {
        // methods
        void GetInterfacceSafyOptions(
            System.Int32 riid,
            out System.Int32 pdwSupportedOptions,
            out System.Int32 pdwEnabledOptions);
        void SetInterfaceSafetyOptions(
            System.Int32 riid,
            System.Int32 dwOptionsSetMask,
            System.Int32 dwEnabledOptions);
    }

 

[ComImport, Guid("00000118-0000-0000-C000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleClientSite
    {
        void SaveObject();
        void GetMoniker(uint dwAssign, uint dwWhichMoniker, object ppmk);
        void GetContainer(out IOleContainer ppContainer);
        void ShowObject();
        void OnShowWindow(bool fShow);
        void RequestNewObjectLayout();
    }

 

 [ComImport, Guid("0000011B-0000-0000-C000-000000000046"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IOleContainer
    {
        void EnumObjects([In, MarshalAs(UnmanagedType.U4)] int grfFlags,
        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppenum);
        void ParseDisplayName([In, MarshalAs(UnmanagedType.Interface)] object pbc,
        [In, MarshalAs(UnmanagedType.BStr)] string pszDisplayName,
        [Out, MarshalAs(UnmanagedType.LPArray)] int[] pchEaten,
        [Out, MarshalAs(UnmanagedType.LPArray)] object[] ppmkOut);
        void LockContainer([In, MarshalAs(UnmanagedType.I4)] int fLock);
    }

 

 

[ComVisible(true)]
    [ProgId("WorkFlowDesignTimeControl")]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("8d838c92-9eff-4f34-b685-76897809f382")]
    public partial class WorkFlowDesignTimeControl : UserControl, IObjectSafety
    {
        public WorkFlowDesignTimeControl()
        {
            InitializeComponent();
        }

        public void ShowMessage(string msg)
        {
            if (msg != null)
            {
                MessageBox.Show(msg);
            }
            CallJavaScript("PPP.Test('" + msg + "')");
        }

        private void CallJavaScript(string code)
        {
            Type typeIOleObject = this.GetType().GetInterface("IOleObject", true);
            object oleClientSite = typeIOleObject.InvokeMember("GetClientSite",
            BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
            null,
            this,
            null);

            IOleClientSite oleClientSite2 = oleClientSite as IOleClientSite;
            IOleContainer pObj;
            oleClientSite2.GetContainer(out pObj);

            //擷取頁面的Script集合
            IHTMLDocument2 pDoc2 = (IHTMLDocument2)pObj;
            IHTMLWindow2 win2 = (IHTMLWindow2)pDoc2.parentWindow;

            win2.execScript(code,  "javascript");
            //object script = pDoc2.Script;

            //try
            //{
            //    script.GetType().InvokeMember(functionName,
            //    BindingFlags.Instance | BindingFlags.InvokeMethod | BindingFlags.Public,
            //    null,
            //    script,
            //    args);
            //}
            //catch { }
        }

        #region IObjectSafety 成員

        public void GetInterfacceSafyOptions(Int32 riid, out Int32 pdwSupportedOptions, out Int32 pdwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.GetInterfacceSafyOptions 實現
            pdwSupportedOptions = 1;
            pdwEnabledOptions = 2;
        }

        public void SetInterfaceSafetyOptions(Int32 riid, Int32 dwOptionsSetMask, Int32 dwEnabledOptions)
        {
            // TODO:  添加 WebCamControl.SetInterfaceSafetyOptions 實現            
        }

 

        #endregion

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

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            RegistryKey ctrl = k.CreateSubKey("Control");
            ctrl.Close();

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);
            inprocServer32.SetValue("CodeBase", Assembly.GetExecutingAssembly().CodeBase);
            inprocServer32.Close();

            k.Close();
        }

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

            RegistryKey k = Registry.ClassesRoot.OpenSubKey(sb.ToString(), true);

            k.DeleteSubKey("Control", false);

            RegistryKey inprocServer32 = k.OpenSubKey("InprocServer32", true);

            k.DeleteSubKey("CodeBase", false);

            k.Close();
        }

    }

 

[assembly: AllowPartiallyTrustedCallers()]

[assembly: ComVisible(true)]

 

regasm AClass.dll /tlb /codebase

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.