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