VS. C#activex Control Development Summary

Source: Internet
Author: User

An ActiveX control, formerly known as an OLE control or OCX control, is a software component or object that can be inserted into a Web page or other application. With ActiveX plug-ins, you can easily and conveniently insert multimedia effects, interactive objects, complex programs, and more in a Web page. ActiveX controls are typically developed using C + + or VB, and this article explores the technical implementation of developing ActiveX controls in visual Studio 2012 environments using C #.

I. Development of ActiveX controls

1. Create a Blank Solution


2, on the solution right click → add → new project →visual c#→windows→windows Form Control Library, enter the name of ActiveX.


Click OK to generate a control named UserControl1, inherit from the UserControl class, drag a button above, and write the event response code:


3. Right-click on the new ActiveX control library → properties → application → assembly information → make assembly com visible (M). Then switch to the "Generate" tab → tick "register for COM Interop".






4, modify the ActiveX AssemblyInof.cs file, add the following code:


5. Add content for Control1.cs

Add namespaces using System.Runtime.InteropServices;

Create a GUID for the control: tools → create a GUID, choose 5, click Copy!


Paste the GUID number into ActiveX.cs, as follows:

Using system;using system.collections.generic;using system.componentmodel;using system.drawing;using System.Data; Using system.linq;using system.text;using system.threading.tasks;using system.windows.forms;using System.runtime.interopservices;namespace activex{    [Guid ("D4176a17-2a33-4903-8f37-9ebdd7caffd3")]    public Partial class Usercontrol1:usercontrol    {public        UserControl1 ()        {            InitializeComponent ();        }        private void Button1_Click (object sender, EventArgs e)        {            MessageBox.Show ("Hello, geostorm!");        }    }}
6. In order for the ActiveX control to get the trust of the client, the control class also needs to implement an interface called "IObjectSafety". (Note: The GUID value of the interface cannot be modified.)

Create interface: Right-click Add-New Item →visual C # item → Interface for ActiveX engineering

The code is as follows:

Using system;using system.runtime.interopservices;namespace activexdemo{        [ComImport, GuidAttribute (" cb5bdc81-93c1-11cf-8f20-00805f2cd064 ")]//fixed    [InterfaceTypeAttribute ( Cominterfacetype.interfaceisiunknown)] public    interface IObjectSafety    {        [preservesig]        int GetInterfaceSafetyOptions (ref Guid riid, [MarshalAs (UNMANAGEDTYPE.U4)] ref int pdwsupportedoptions, [MarshalAs ( UNMANAGEDTYPE.U4)] ref int pdwenabledoptions);        [PreserveSig ()]        int SetInterfaceSafetyOptions (ref Guid riid, [MarshalAs (UNMANAGEDTYPE.U4)] int dwoptionsetmask, [MarshalAs ( UNMANAGEDTYPE.U4)] int dwenabledoptions);}    }
then integrate and implement the interface in the control class:

Using system;using system.windows.forms;namespace activextest{using System.Runtime.InteropServices; [Guid ("D4176a17-2a33-4903-8f37-9ebdd7caffd3"), ProgId ("Activexdemo.usercontrol1"), ComVisible (true)] public Partial class Usercontrol1:usercontrol,iobjectsafety {#region iobjectsafety member format fixed private con        St String _iid_idispatch = "{00020400-0000-0000-c000-000000000046}";        Private Const String _iid_idispatchex = "{a6ef9860-c720-11d0-9337-00a0c90dcaa9}";        Private Const String _iid_ipersiststorage = "{0000010a-0000-0000-c000-000000000046}";        Private Const String _iid_ipersiststream = "{00000109-0000-0000-c000-000000000046}";        Private Const String _iid_ipersistpropertybag = "{37d84f60-42cb-11ce-8135-00aa004bb851}";        Private Const int interfacesafe_for_untrusted_caller = 0x00000001;        Private Const int interfacesafe_for_untrusted_data = 0x00000002;        Private Const int S_OK = 0; Private Const int E_FAIL = Unchecked ((int) 0x80004005);        Private Const int E_NOINTERFACE = unchecked ((int) 0x80004002);        private bool _fsafeforscripting = true;        private bool _fsafeforinitializing = true;            public int getinterfacesafetyoptions (ref Guid riid, ref int pdwsupportedoptions, ref int pdwenabledoptions) {            int Rslt = E_FAIL; String strguid = riid.            ToString ("B"); Pdwsupportedoptions = Interfacesafe_for_untrusted_caller |            Interfacesafe_for_untrusted_data;                    Switch (strguid) {case _iid_idispatch:case _iid_idispatchex:                    Rslt = S_OK;                    pdwenabledoptions = 0;                    if (_fsafeforscripting = = true) Pdwenabledoptions = Interfacesafe_for_untrusted_caller;                Break                    Case _iid_ipersiststorage:case _iid_ipersiststream:case _iid_ipersistpropertybag: Rslt = S_OK;                    pdwenabledoptions = 0;                    if (_fsafeforinitializing = = true) Pdwenabledoptions = Interfacesafe_for_untrusted_data;                Break                    Default:rslt = E_nointerface;            Break        } return Rslt;            } public int setinterfacesafetyoptions (ref Guid riid, int dwoptionsetmask, int dwenabledoptions) {            int Rslt = E_FAIL; String strguid = riid.            ToString ("B");                    Switch (strguid) {case _iid_idispatch:case _iid_idispatchex: if (((Dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_caller) && (_fsafeforscripting = =                    true) Rslt = S_OK;                Break                    Case _iid_ipersiststorage:case _iid_ipersiststream:case _iid_ipersistpropertybag: if (((dwenabledoptions & dwoptionsetmask) = = Interfacesafe_for_untrusted_data) && (_fsafeforinitializing = =                    true) Rslt = S_OK;                Break                    Default:rslt = E_nointerface;            Break        } return Rslt;        } #endregion Public UserControl1 () {InitializeComponent ();        } private void Button1_Click (object sender, EventArgs e) {MessageBox.Show ("Hello, Geostorm"); }    }}
this way, an ActiveX control is developed!

Ii. Deployment of ActiveX controls

Add a packaged deployment project (VS2012 requires a separate installation of InstallShield Limited Edition and enter the serial number), add the main output, set its Register property to vsdrpCOM, and build after completion, this procedure is skipped.

Third, testing

1. Create a new Web application:

File → new → project →visual c#→web→asp.net empty WEB application →webapplication1→ added to the solution.


2. Add a Web page

Right-click → add →web form

<%@ page language= "C #" autoeventwireup= "true" codebehind= "WebForm1.aspx.cs" inherits= "Webapplication1.webform1"% ><! DOCTYPE html>

3. View the effect in IE browser


Iv. ActiveX Control packaging (. cab) and client Auto-installation











VS. C#activex Control Development Summary

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.