How to Write an ActiveX Control

How to Write an ActiveX Control_第1张图片

A button can be created in an ActiveX control.

An ActiveX Control is a software component that can be plugged into different programs and used as if it were a native part of the program. Programmers can write ActiveX controls in any language that supports Component Object Model (COM ) development. Typical examples of ActiveX controls are command buttons, list boxes and dialog boxes. An ActiveX Control can be created using Visual Basic, C# or VC++.NET.

Difficulty:
 
Challenging

Instructions

things you'll need:

  • Visual Basic editor
  • C# editor
  • Visual C++ editor
  1. Visual Basic

    • 1

      Open MS Visual Basic and create a new project type as "ActiveX Control."

    • 2

      Create a Command button, called cmdTest, and a text box, called txtTest. Drag the controls from the toolbox to create these.

    • 3

      Add the following functions:

      Public Function setText(txt As String)

      txtTest.Text = txt

      End Function

      Private Sub cmdTest_Click()

      MsgBox txtTest.Text

      End Sub

      This will pass a value to the text box from the Web page. When a user clicks the command button, it will display this text in a message box.

    • 4

      Click on "Tools" and then "Package and Deployment Wizard" to save the project. Save the project as an Internet package and name as "ctlVBActive."

    • 5

      Create an HTML page in a text editor, such as Notepad.

    • 6

      Add the following code:

      <select id="drpValues" onclick="send_value(this)">

      <option value=""></option>

      <option value="value1">value1</option>

      <option value="value2">value2</option>

      <option value="value3">value3</option>

      <option value="value4">value4</option>

      <option value="value5">value5</option>

      </select>

      <OBJECT ID="ctlVBActive"

      CLASSID="CLSID:748FEF73-28D1-4889-A582-E5F8F526CDD1"

      CODEBASE="vbActiveX.CAB#version=1,0,0,0">

      </OBJECT>

    • 7

      Save and open the file in a browser.

    C#

    • 1

      Open C# editor and create a new project.

    • 2

      Add the following code, to create an ActiveX control:

      using System;

      using System.Runtime.InteropServices;

      namespace ANamespace

      {

      public interface ASignatures

      {

      string FName();

      string SName();

      int Age { get;}

      }

      [ClassInterface(ClassInterfaceType.AutoDual)]

      public class AClass :ASignatures

      {

      public string FName()

      {

      return "John";

      }

      public string SName()

      {

      return "Ryan";

      }

      public int Age

      {

      get { return 40; }

      }

      }

      }

      Save the file. Call it "AClass.dll."

    • 3

      Compile the file. The is done using a compiler file called "csc.exe," which is located at: \WINDOWS\Microsoft.NET\Framework\v2.0. Open this compiler file, which opens as a console, and add the code:

      regasm AClass.dll /tlb /codebase

      This class file created in the previous step is compiled and can be called from a HTML page.

    • 4

      Create an HTML file to call the ActiveX control:

      <html>

      <head>

      <script language="javascript">

      <!-- Load the ActiveX object -->

      var x = new ActiveXObject("ANamespace.AClass");

      <!-- Access the Method -->

      alert(x.FName());

      alert(x.SName());

      <!-- Access the Property -->

      alert(x.Age);

      </script>

      </head>

      <body>

      </body>

      </html>

    • 5

      Save the HTML file and run in a browser.

    Visual C++

    • 1

      Open the Visual C++ editor.

    • 2

      Create a new project and select "ActiveX control."

    • 3

      Add code to create a new icon

      void CMyActiveXIconCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)

      CBitmap::LoadBitmap()

      CDC::StretchBlt()

      This function will draw the ActiveX control icon.

    • 4

      Press F5 to run the program. In Visual C++ .NET, the control is registered automatically.

    • 5

      Go to the "Tools" menu and click on ActiveX Control Test container. Go to the "Edit" menu and click on "Insert Control" option. A window will appear. Check the "MyActiveX Icon Control," which now shows the ActiveX Control Test Container you have created.



Read more: How to Write an ActiveX Control | eHow.com http://www.ehow.com/how_7852194_write-activex-control.html#ixzz1KhwP99VS

你可能感兴趣的:(ActiveX)