Selected from the "Red Horse World" blog, made an appropriate modification will be a web project in the next two chapters (http://www.cnblogs.com/homer/), must write an ActiveX control yourself. Today's ActiveX control is mostly developed by VB / C , and I am not familiar with them, so I consider writing ActiveX controls using familiar C #. First, create a WinForm control project helloWorld and drag into a Label control, and the text is set to helloworld, as shown:
UserControl1.cs content is as follows:
Using
System; using
System.collections; using
System.componentmodel; Using
System.drawing; using
System.data; using
System.Windows.Forms; Namespace
HelloWorld
{/ ** //// /// UserControl1 Summary Description. /// summary> public class demo: system.windows.Forms.userControl {private system.windows.forms.Label label1; ////// // The designer variable is required. /// summary> private system.componentmodel.container components = null; public demo () {// This call is required for the Windows.Forms Form Designer. InitializationComponent (); // Todo: Add any initialization after the initComponent call} / ** //// /// clean all the resources being used. /// summary> Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) Components.Dispose ();} Base.Dispose (Disposing);} Component Designer Generated Code # The Region component designer generated code / ** //// /// designer supports the required method - do not use the code editor // to modify the contents of this method.
/// summary> private void initialization () {this.label1 = new system.windows.forms.label (); this.suspendlayout (); // // label1 // this.label1.location = new system. Drawing.Point (32, 32); this.label1.name = "label1"; this.label1.size = new system.drawing.size (120, 32); this.label1.tabindex = 0; this.label1.text = "HelloWorld"; this.label1.textalign = system.drawing.contentalignment.middleCenter; // // demo // this.controls.add (this.label1); this.Name = "demo"; this.size = new System.drawing.size (184, 96); this.ResumeLayout (false);} #ENDREGION}} The compile project at this time can generate HelloWorld.dll. Copy this DLL to the virtual root directory of IIS, then create a file file with HelloWorld.htm, HTML code as follows:
= '# 223344'>
body>
Enter the following address in the IE address bar: http://localhost/helloWorld.htm, appears interface:
As shown in the figure, the control has been successfully displayed on the page. OK, we have completed the first step. But the problem has not been resolved here. Do not believe? You can try to test on another machine, pay attention to modify the corresponding HTML code and URL address. You can see this place where the control is displayed is a red fork, or a dialog will also pop up, indicating that this control does not have any permissions. This result is caused by Microsoft's default settings. The author must perform a security declaration in AssemblyInfo.cs / VB in which the control is located, and declare that this control must be given to the permissions. We reference the system.security namespace in AssemblyInfo.cs and add a sentence:
[assmbly: allowPartiallytrustedCallers ()]
Now recompile, and replace the previous DLL, the interface can be displayed again. It is necessary to remind that until now, we have written it yet is not a real ActiveX control. This control is only available now, but it is only possible to display itself, and more features cannot be implemented, such as implementing the interaction with the script or a registry or disk of the client. This is due to the security model of the .NET Framework. If we want this control to break through the restriction of the .NET Framework security model, implement the interaction with the script or how to operate the client's registry or disk, you must make it a true ActiveX control. Below, we turn the control just now into a real ActiveX control. First Use Tools -> Create GUID to generate a GUID and modify the userControl1.cs file. First add to the reference system.runtime.interopservices namespace and add a statement in front of DEMO: Note that the string in the GUID is the GUID string you generated. It is the unique identifier of the ActiveX control you generated. The project properties are then modified, as shown: Watch the last item in the panel, the only thing we need to modify is to change its value to True. Rebate. We use tools -> OLE / COM object viewer view, as shown: You can see that the HelloWorld.Demo we wrote has been properly identified as COM components. Now, we can already display it in the web page like using other ActiveX controls. Right-click on HelloWorld.Demo, as shown: Select Copy HTML