External Interfaces Previous page   Next Page

Creating an ActiveX Control

You can create an ActiveX control from the MATLAB client using either a graphical user interface or the actxcontrol function directly from the command line. Either of these methods creates an instance of the control in the MATLAB client process and returns a handle to the primary interface to the COM object. Through this interface, you can then access any public property or method of the object. You can also establish additional interfaces to the object, including interfaces that use IDispatch and any custom interfaces that may exist.

This section covers the following topics describing how to create the control in the client process and how to position its physical representation in the MATLAB figure window:

Finding Out What Controls Are Installed

The actxcontrollist function enables you to see what COM controls are currently installed on your system. Type

and MATLAB returns a list of each control, including its name, programmatic identifier (or ProgID), and filename, in the output cell array.

Here is an example of the information that might be returned for several controls:

Creating Control Objects Using a Graphical Interface

The simplest way to create a control object is using the actxcontrolselect function. This function displays a graphical user interface that lists all controls installed on your system. When you select an item from the list and click the Create button, MATLAB creates the control and returns a handle to it:

Figure showing ActiveX control list with calendar control 9.0 selected

The interface has a selection panel at the left of the window and a preview panel at the right. Click on one of the control names in the selection panel to see a preview of the control displayed. (For controls that do not have a preview, the preview panel is blank). If MATLAB cannot create the control, an error message is displayed in the preview panel.)

Setting Properties with actxcontrolselect.   Click the Properties button on the actxcontrolselect window to enter nondefault values for properties when creating the control. You can select which figure window to put the control in (Parent field), where to position it in the window (X and Y fields), and what size to make the control (Width and Height).

You can also register any events you want the control to respond to in this window. (See Control and Server Events for an explanation of event registration). Register an event and the callback routine to handle that event by entering the name of the routine to the right of the event under Callback M-File.

You can also select callback routines by browsing for their respective M-files. Click a name in the Event Names column and then click the Browse button to select the callback for that event. To assign a callback routine to more than one event, press the Ctrl key and click on individual event names, or drag the mouse over consecutive event names, and then click Browse to select the callback routine.

MATLAB responds only to those events that are registered, so any events for which you do not specify a Callback M-File will be ignored when the event fires.

At the actxcontrolselect window, select Calendar Control 10.0 and click Properties to see the window shown below. Enter a Width of 500 and a Height of 350 to change the default size for the control. Click OK in this window, and Create in the actxcontrolselect window to create the Calendar control.

Figure: Choose ActiveX Control Creation Parameters

You can also set control parameters using the actxcontrol function. One parameter you can set with actxcontrol but not with actxcontrolselect is the name of an initialization file. When you specify this filename, MATLAB sets the initial state of the control to that of a previously saved control.

Information Returned by actxcontrolselect.   actxcontrolselect creates an object that is an instance of the MATLAB COM class. The function returns up to two output arguments: a handle for the object, and a 1-by-3 cell array containing information about the control. These are shown here as h and info, respectively:

Use the handle to identify this particular control object when calling other MATLAB COM functions. The information returned in the cell array shows the name, ProgID, and filename for the control.

If you select Calendar Control 9.0 and then click Create, MATLAB returns

Expand the info cell array to show the control name, ProgID, and filename.

Creating Control Objects from the Command Line

If you already know which control you want and you know its ProgID, you can bypass the graphical user interface by using the actxcontrol function to create it. (The ProgID is a string that is defined by the vendor and can be obtained from the vendor's documentation. For example, the ProgID for MATLAB is matlab.application.)

The only required input when calling the function is the ProgID. However, as with actxcontrolselect, you can supply additional inputs that enable you to select which figure window to put the control in, where to position it in the window, and what size to make it. You can also register any events you want the control to respond to, or set the initial state of the control by reading that state from a file. See the reference page on actxcontrol for a full explanation of its input arguments.

actxcontrol returns a handle to the primary interface to the object. Use this handle to reference the object in other COM function calls. You can also use the handle to obtain additional interfaces to the object. For more information on using interfaces, see Getting Interfaces to the Object.

This example creates a control to run a Microsoft Calendar application. Position the control in figure window fig3, at a [0 0] x-y offset from the bottom left of the window, and make it 300 by 400 pixels in size:

Repositioning the Control in a Figure Window

Once a control has been created, you can change its shape and position in the window with the move function.

Observe what happens to the object created in the last section when you specify new origin coordinates (70, 120) and new width and height dimensions of 400 and 350:

Using Microsoft Forms 2.0 Controls

You may encounter problems when creating or using Microsoft Forms 2.0 controls in MATLAB. Forms 2.0 controls were designed for use only with applications that are enabled by Visual Basic for Applications (VBA). Microsoft Office is one such application.

To work around these problems, you can use the replacement controls listed below, or consult article 236458 in the Microsoft Knowledge Base for further information:

http://support.microsoft.com/?id=236458%22.

Affected Controls.   You may see this behavior with any of the following Forms 2.0 controls:

Replacement Controls.   The following replacements are recommended by Microsoft:

Old
New
Forms.TextBox.1
RICHTEXT.RichtextCtrl.1
Forms.CheckBox.1
vidtc3.Checkbox
Forms.CommandButton.1
MSComCtl2.FlatScrollBar.2
Forms.TabStrip.1
COMCTL.TabStrip.1


Previous page  MATLAB COM Client Support Instantiating a DLL Component Next page

© 1994-2005 The MathWorks, Inc.