Visual C # create and use ActiveX components Fengfeng. net zhuyun)

Source: Internet
Author: User
Development is based on. it is difficult for programmers on the net platform to essentially associate Visual C # With ActiveX components. Although Visual C # is used to develop applications. net Framework SDK is incomplete, and ActiveX is also required. However, even so, it is difficult to associate the two. The reason is that the files can be directly used by Visual C # And the executable programs generated through Visual C # can only be hosted files. The Active X component is not a hosted file. The difference in such files determines the nature of the two ". So this raises the first question in this article, what is the relationship between ActiveX and Visual C.
 
   I. Visual C # and Active X components:

At this point, some may say that since Visual C # Can only directly use code files, what is the reference that can be provided in Visual C # To Call ActiveX directly? Indeed, Visual C # provides operations that reference ActiveX components. This operation effectively utilizes a lot of previous resources, making these resources not available with Microsoft. net platform, but this operation to introduce ActiveX Components in Visual C # Is not advocated by Microsoft, nor is it in line with Microsoft's launch. net. This is because Microsoft launched. Net to achieve cross-platform, to achieve "Write Once and Run Anywhere", and to Write the code again, it can Run on any platform. If the Active X component is used in the program, it is determined from the other hand that the program can only be used on the Windows platform, so it cannot achieve Microsoft's "Write Once and Run Anywhere" final goal.

In addition, the reference ActiveX component operation provided by Visual C #, in fact, when the Active X component is added to the "toolbox" of Visual C #, Visual Stuio. net actually performs a lot of operations on ActiveX components, and these operations are hidden by Visual C #, users are often not completely clear. These operations are used to convert unmanaged ActiveX components into managed components. These operations are collectively referred to as "interoperability". Careful programmers may find that, after the ActiveX component is dragged to the program form, several "Dll" files are added to the "Bin" directory where the source program is located, these files are generated after interoperability conversion by the Active X component. In this case, Visual C # does not use ActiveX components, but uses ActiveX components for interoperability to obtain class libraries that are available for the. Net platform and have the same functions as the original ActiveX components.

Since ActiveX components cannot be directly used in Visual C #, ActiveX components that appear to be used in Visual C # actually use class libraries that are converted after interoperability. Can Visual C # generate an Active X component? This article describes how to generate ActiveX Components in Visual C. The method is to first create a Windows component through Visual C #, and then publish its interface in the form of COM.

  2. The program design and running environment described in this article:

(1). Microsoft Windows 2000 Server Edition.
 
(2). Visual Studio. Net 2003 enterprise structure edition,. Net Framework SDK 4322.

  3. Use Visual C # To create Windows Components:

Follow these steps to create a Windows component using Visual C:

1. Start Visual Studio. Net.

2. Select File, new, and project. The new project dialog box is displayed.

3. Set project type to Visual C # project ].

4. Set [TEMPLATE] to [class library ].

5. Enter ActiveXDotNet In the Name text box ].

6. in the "location" text box, enter "C: \ Class" and click "OK". Then, Visual C # is displayed in "C: create the "ActiveXDotNet" folder in the \ Class "Directory, which stores the ActiveXDotNet project file, as shown in 01:


Figure 01: Create a new project dialog box

7. Select Solution Explorer and upload the Class1.cs file from it because the file is useless in this program.

8. select project | add component. The add new item dialog box is displayed. In this dialog box, set template to component class and name to MyControl. after cs, click open. Add a file named "MyControl. cs" to the project file. 02:


Figure 02: Add new project dialog box 9. Set Visual Studio . NetSwitch to the MyControl. cs (Design) window, and 【 WindowsIn the form components tab, drag the following components to the design form in sequence:

One GrouPBOx component, and then drag to this component,

A TextBox component and a Lable component.

10. Switch the current window of Visual Studio. Net to the "MyControl. cs" code editing window and replace InitializeComponent in MyControl. cs with the following code. The following code initializes the components added above:

Private void InitializeComponent ()
{
This. groupBox1 = new System. Windows. Forms. GroupBox ();
This.txt UserText = new System. Windows. Forms. TextBox ();
This. label1 = new System. Windows. Forms. Label ();
This. groupBox1.SuspendLayout ();
This. SuspendLayout ();
This. groupBox1.Controls. Add (this.txt UserText );
This. groupBox1.Controls. Add (this. label1 );
This. groupBox1.Location = new System. Drawing. Point (8, 8 );
This. groupBox1.Name = "groupBox1 ";
This. groupBox1.Size = new System. Drawing. Size (272, 56 );
This. groupBox1.TabIndex = 0;
This. groupBox1.TabStop = false;
This. groupBox1.Text = "Active X component created by Visual Studio. Net ";
This.txt UserText. Enabled = false;
This.txt UserText. Location = new System. Drawing. Point (84, 20 );
This.txt UserText. Name = "txtUserText ";
This.txt UserText. Size = new System. Drawing. Size (180, 21 );
This.txt UserText. TabIndex = 1;
This.txt UserText. Text = "";
This. label1.Location = new System. Drawing. Point (8, 24 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (66, 16 );
This. label1.TabIndex = 0;
This. label1.Text = "input information :";
This. Controls. Add (this. groupBox1 );
This. Name = "MyControl ";
This. Size = new System. Drawing. Size (288, 72 );
This. groupBox1.ResumeLayout (false );
This. ResumeLayout (false );
}
Now the Active X component interface created by the ActiveXDotNet project is basically complete, as shown in 03:


Figure 03: design interface of the Active X component created by ActiveXDotNet

11. in MyControl. in cs, [Summary of MyControl] Add the following code in the code area. The following Code defines a public interface that tells COM/COM +, there is a common attribute for read/write operations:

Public interface AxMyControl
{
String UserText {set; get ;}
}
12. Add the following code in the MyControl. cs [MyControl] class code area. The following Code defines a private string and uses this string to saveWebThe value passed in the test page defines a public attribute. In the following Web test page, the value is passed through this attribute, Which is readable and writable:

Private String mStr_UserText;
Public String UserText
{
Get {return mStr_UserText ;}
Set
{
MStr_UserText = value;
// Modify the component Value
TxtUserText. Text = value;
}
}
13. Save the above modification steps. Now we use VisualC #A class named MyControl is created, which is a component encapsulated by Visual C # that resembles the Active X component.

14. click Ctrl + F5, Visual C # automatically completes compilation, and generates a name named "ActiveXDotNet" in the "C: \ Class \ ActiveXDotNet \ bin \ Debug" directory. dll file, which is the generated component.

The following is all the code for MyControl. cs generated after the above steps:

Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Drawing;
Using System. Data;
Using System. Windows. Forms;
Namespace ActiveXDotNet
{
Public interface AxMyControl
{
String UserText {set; get ;}
}
/// <Summary>
/// Summary of MyControl.
/// </Summary>
Public class MyControl: System. Windows. Forms. UserControl, AxMyControl
{
/// <Summary>
/// Required designer variables.
/// </Summary>
Private System. ComponentModel. Container components = null;
Private System. Windows. Forms. GroupBox groupBox1;
Private System. Windows. Forms. Label label1;
Private System. Windows. Forms. TextBox txtUserText;
Private String mStr_UserText;
Public String UserText
{
Get {return mStr_UserText ;}
Set
{
MStr_UserText = value;
// Modify the component Value
TxtUserText. Text = value;
}
}
Public MyControl ()
{
// This call is required by the Windows. Forms Form Designer.
InitializeComponent ();

// TODO: add any initialization after InitializeComponent calls
}
/// <Summary>
/// Clear all resources in use.
/// </Summary>
Protected override void Dispose (bool disposing)
{
If (disposing)
{
If (components! = Null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing );
}
# Code generated by the region component designer
/// <Summary>
/// The designer supports the required methods-do not use the code editor
/// Modify the content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. groupBox1 = new System. Windows. Forms. GroupBox ();
This.txt UserText = new System. Windows. Forms. TextBox ();
This. label1 = new System. Windows. Forms. Label ();
This. groupBox1.SuspendLayout ();
This. SuspendLayout ();
This. groupBox1.Controls. Add (this.txt UserText );
This. groupBox1.Controls. Add (this. label1 );
This. groupBox1.Location = new System. Drawing. Point (8, 8 );
This. groupBox1.Name = "groupBox1 ";
This. groupBox1.Size = new System. Drawing. Size (272, 56 );
This. groupBox1.TabIndex = 0;
This. groupBox1.TabStop = false;
This. groupBox1.Text = "Visual C # Created Active X component ";
This.txt UserText. Enabled = false;
This.txt UserText. Location = new System. Drawing. Point (84, 20 );
This.txt UserText. Name = "txtUserText ";
This.txt UserText. Size = new System. Drawing. Size (180, 21 );
This.txt UserText. TabIndex = 1;
This.txt UserText. Text = "";
This. label1.Location = new System. Drawing. Point (8, 24 );
This. label1.Name = "label1 ";
This. label1.Size = new System. Drawing. Size (66, 16 );
This. label1.TabIndex = 0;
This. label1.Text = "input information :";
This. Controls. Add (this. groupBox1 );
This. Name = "MyControl ";
This. Size = new System. Drawing. Size (288, 72 );
This. groupBox1.ResumeLayout (false );
This. ResumeLayout (false );
}
# Endregion
}
}

Iv. VisualC #Use the encapsulated Active X component:

Follow these steps to test the above components on the web page:

1. Create a file named test.htm. MyControl is tested on this Web page. The content of this file is as follows:

<Html>
<Body color = white>
<Hr>

<Font face = arial size = 1>
<OBJECT id = "MyControl1" name = "MyControl1" classid = "ActiveXDotNet. dll # ActiveXDotNet. MyControl" width = 288 height = 72>
</OBJECT>
</Font>

<Form name = "frm" id = "frm">
<Input type = "text" name = "txt" value = "Enter data:"> <input type = button value = "OK" onClick = "doScript ();">
</Form>
<Hr>
</Body>

<Script language = "JavaScript">
Function doScript ()
{
MyControl1.UserText = frm.txt. value;
}
</Script>
</Html>
2. Copy all the generated "test.htm" and "ActiveXDotNet. dll" files to the virtual directory of the machine. Generally, the virtual directory is "C: \ Inetpub \ wwwroot ".

3. Open the browser, enter "http: // localhost/test.htm" in the address bar of the browser, and click "go to". The following running interface is displayed:


Figure 04: test the running interface of the Active X component generated by Visual C #

So far, all the work of the Active X component generated by Visual C # and the test of this component has been completed.

  V. Summary:

Although the methods described in this article can easily solve many difficult problems on the Web page, this article introduces that the components generated by Visual C # are very similar to Active X components in practicality, however, in essence, the components produced in this article are not actually Active X components. To use the components created in this article, you must install them on the machine where the Web page is located.. The component generated in this article is actually a hosted code file. It tells COM/COM + objects cleverly by defining interfaces. This component has a public attribute that can be accessed through read/write operations on this attribute, similar to the Active X component.

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.