This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.


Create Microsoft ActiveX control in figure window


h = actxcontrol('progid')
h = actxcontrol('progid','param1',value1,...)
h = actxcontrol('progid',position)
h = actxcontrol('progid', position, fig_handle)
h = actxcontrol('progid',position,fig_handle,event_handler)
h = actxcontrol('progid',position,fig_handle,event_handler,'filename')


h = actxcontrol('progid') creates an ActiveX® control in a figure window. The programmatic identifier (progid) for the control determines the type of control created. (See the documentation provided by the control vendor to get this value.) The returned object, h, represents the default interface for the control.

You cannot use an ActiveX server for the progid because MATLAB® cannot insert ActiveX servers in a figure. See actxserver for use with ActiveX servers.

h = actxcontrol('progid','param1',value1,...) creates an ActiveX control using the optional parameter name/value pairs. Parameter names include:

  • position — MATLAB position vector specifying the position of the control. The format is [left, bottom, width, height] using pixel units.

  • parent — Handle to parent figure, model, or Command Window.

  • callback — Name of event handler. To use the same handler for all events, specify a single name. To handle specific events, specify a cell array of event name/event handler pairs.

  • filename — Sets the initial conditions to the previously saved control.

  • licensekey — License key to create licensed ActiveX controls that require design-time licenses. See Deploy ActiveX Controls Requiring Run-Time Licenses for information on how to use controls that require run-time licenses.

One possible format is:

h = actxcontrol('myProgid','newPosition',[0 0 200 200],...
    'myCallback',{'Click' 'myClickHandler';...
    'DblClick' 'myDblClickHandler';...
    'MouseDown' 'myMouseDownHandler'});

    Note:   The following syntaxes are not recommended. Use the previous syntaxes instead.

h = actxcontrol('progid',position) creates an ActiveX control having the location and size specified in the vector, position. The format of this vector is:

[x y width height]

The first two elements of the vector determine where the control is placed in the figure window. x and y are offsets, in pixels, from the bottom left corner of the figure window to the same corner of the control. width and height determine the size of the control itself.

The default position vector is [20 20 60 60].

h = actxcontrol('progid', position, fig_handle) creates an ActiveX control at the specified position in an existing figure window. The handle, fig_handle, identifies this window.

The gcf command returns the current figure handle.

    Note   If the figure window designated by fig_handle is invisible, the control is invisible. If you want the control you are creating to be invisible, use the handle of an invisible figure window.

h = actxcontrol('progid',position,fig_handle,event_handler) creates an ActiveX control that responds to events. Controls respond to events by invoking a MATLAB function whenever an event (such as clicking a mouse button) is fired. The event_handler argument identifies one or more functions to be used in handling events. For more information, see Specifying Event Handlers.

h = actxcontrol('progid',position,fig_handle,event_handler,'filename') creates an ActiveX control with the first four arguments, and sets its initial state to that of a previously saved control. MATLAB loads the initial state from the file specified in filename.

If you do not want to specify an event_handler, use '' as the fourth argument.

The progid argument must match the progid of the saved control.

Specifying Event Handlers

There is more than one valid format for the event_handler argument. Use this argument to specify one of the following:

  • A different event handler routine for each event supported by the control

  • One common routine to handle selected events

  • One common routine to handle all events

In the first case, use a cell array for the event_handler argument, with each row of the array specifying an event and handler pair:

{'event' 'eventhandler'; 'event2' 'eventhandler2'; ...}

event is either a character vector containing the event name or a numeric event identifier (see Handling Events). eventhandler is a character vector identifying the function you want the control to use in handling the event. Include only those events that you want enabled.

In the second case, use the same cell array syntax described, but specify the same eventhandler for each event. Again, include only those events that you want enabled.

In the third case, make event_handler a character vector (instead of a cell array) that contains the name of the one function that is to handle all events for the control.

There is no limit to the number of event and handler pairs you can specify in the event_handler cell array. However, if you register the same event name to the same callback handler multiple times, MATLAB executes the event only once.

Event handler functions accept a variable number of arguments.

Character vectors used in the event_handler argument are not case-sensitive.

    Note   Although using a single handler for all events might be easier sometimes, specifying an individual handler for each event creates more efficient code, resulting in better performance.


Handling Events

The event_handler argument specifies how you want the control to handle any events that occur. The control can handle all events with one common handler function, selected events with a common handler function, or each type of event can be handled by a separate function.

Create an mwsamp control with one event handler, sampev, to respond to all events:

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],gcf,'sampev');

Invoke event handler when events Click and DblClick are fired.

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],gcf,{'Click' 'sampev'; 'DblClick' 'sampev'});

Assign a different handler routine to each event.

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],...
    gcf,{'Click', 'myclick'; 'DblClick' 'my2click'; ...
    'MouseDown' 'mymoused'});

Specify the events using numeric event identifiers.

h = actxcontrol('mwsamp.mwsampctrl.2',[0 0 200 200],...
    gcf,{-600, 'myclick'; -601 'my2click'; -605 'mymoused'});

Related Examples

More About

collapse all


If the control implements any custom interfaces, use the interfaces function to list them, and the invoke function to get a handle to a selected interface.

When you no longer need the control, call release to release the interface and free memory and other resources used by the interface. Releasing the interface does not delete the control itself. To release the interface, use the delete function.

For an example event handler, see the file sampev.m in the toolbox\matlab\winfun\comcli folder.

COM functions are available on Microsoft® Windows® systems only.

    Note   If you encounter problems creating Microsoft Forms 2.0 controls in MATLAB or other non-VBA container applications, see Microsoft Forms 2.0 Controls.

Introduced before R2006a

Was this topic helpful?