C #. NET ActiveX Control Creation

Source: Internet
Author: User

Step 1: Create a project. For example, select a class library project under windows.

Step 2: Add a class in the project: IObjectSafety. cs, for example:

The IObjectSafety. cs code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Runtime. InteropServices;

Namespace MyActiveX
{
// Guid is unique and cannot be changed; otherwise, it cannot pass security authentication of ActiveX Control of IE browser
[ComImport, Guid ("CB5BDC81-93C1-11CF-8F20-00805F2CD064")]
[InterfaceType (ComInterfaceType. InterfaceIsIUnknown)]
Public interface IObjectSafety
{
[PreserveSig]
Void GetInterfacceSafyOptions (
Int riid,
Out int pdwSupportedOptions,
Out int pdwEnabledOptions );

Step 3: Add a user control MyActiveXControl. cs, as shown in figure

Modify the MyActiveXControl. cs code to inherit IObjectSafety and define the Guid, which is the classid of ActiveX.
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Drawing;
Using System. Data;
Using System. Text;
Using System. Windows. Forms;
Using System. Runtime. InteropServices;

Namespace MyActiveX
{
[Guid ("218849AF-1B2C-457B-ACD5-B42AC8D17EB7"), ComVisible (true)]
Public partial class MyActiveXControl: UserControl, IObjectSafety
{
Public MyActiveXControl ()
{
InitializeComponent ();
}

# Region IObjectSafety members are used for ActiveX Control Security Trust
Public void GetInterfacceSafyOptions (int riid, out int pdwSupportedOptions, out int pdwEnabledOptions)
{
PdwSupportedOptions = 1;
PdwEnabledOptions = 2;
}

Public void SetInterfaceSafetyOptions (int riid, int dwOptionsSetMask, int dwEnabledOptions)
{
Throw new NotImplementedException ();
}
# Endregion
}
}

Step 4: Add a button to test ActiveX controls, for example:

Add events for this button:
Private void button#click (object sender, EventArgs e)
{
MessageBox. Show ("powered by yyzq.net Ye Zhiqiu yq@yyzq.net ");
}
At this point, the code is partially written.
Step 5: Create an installation package and create an installation project, for example:

Step 6: add the previously created ActiveX DLL: MyActiveX. dll to the file system of the installation project.
(Note: After the file is added in, right-click the file and select properties, and set its Register value to vsdraCOM.) For example:

Step 7: generate the installer, find Setup1.msi under MyActiveX \ Setup1 \ Debug, and double-click to install it.
Then, create an HTML file (test.html) in the directory to test our ActiceX control. The HTML code is as follows:
<Html>
<Title> Powered by yyzq.net Email: yq@yyzq.net </title>
<Head>
</Head>
<Body>
<Div>
<Object id = "yyzq" classid = "clsid: 218849AF-1B2C-457B-ACD5-B42AC8D17EB7"
Width = "320"
Height = "240"
Codebase = "Setup1.msi">
</Object>
</Div>
</Body>
</Html>
Open test.html In the IE browser and click


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.