|
Obsolete IDL Features: The IDLDrawWidget ActiveX Control |
|
The goal of this first example is very simple: to create a user interface in Microsoft Visual Basic and have IDL respond to events and display an image. The following figure shows what the finished project looks like when it runs. The Visual Basic source code used to create the example is shown in the following figure:
![]() |
As the figure shows, our first example program consists of two buttons ("Plot Data" and "Exit"), a graphics area, and a text box. All of these elements reside on top of what is called a form in Visual Basic parlance. (A form in Visual Basic is similar to a top level base in IDL.) Clicking the Plot Data button causes IDL to produce the surface plot shown. Clicking Exit causes IDL and the Visual Basic program to free memory and exit.
Begin building the first example by creating a new Visual Basic project, adding the IDL ActiveX control, and drawing the interface components.
Launch Microsoft Visual Basic and create a new project.
Select the "IDLDrawX3 ActiveX Control module" check box and close the Components window. Visual Basic will display the IDLDrawWidget's icon in the toolbar.
Having added IDLDrawWidget to the Visual Basic project, we now have access to IDLDrawWidget's properties and methods. Use the IdlPath and GraphicsLevel properties to specify the directory path of the IDL ActiveX control and to choose between IDL's direct and object graphics capabilities. Refer to IDLDrawWidget Control Reference for a complete list of the properties and methods to IDLDrawWidget.
c:\ITT\idlxx
where xx is the current IDL version.
With the interface drawn and the properties of the IDLDrawWidget set, now write some Visual Basic code to give the application behavior. By double-clicking on the form which contains all of the interface components, Visual Basic will automatically generate the following subroutine.
Private Sub Form_Load() End Sub
Visual Basic's Form_Load routine executes automatically when a program starts running. This procedure can be used to initialize IDL, create the IDLDrawWidget, and direct output from IDL to a text box. The code to accomplish these tasks will be placed between the two statements listed above.
IDL needs to be initialized before Visual Basic can interact with the IDLDrawWidget. This is done with the InitIDL method. InitIDL takes the hWnd of the form containing the IDLDrawWidget as an argument and returns 1 or less than 1, depending on whether or not IDL initialized successfully. Assuming that the default names given to the form and the IDLDrawWidget were not changed, IDL can be initialized with the following statement.
n = IDLDrawWidget1.InitIDL(Form1.hWnd)
A conditional statement is included to display an error message and exit the program if IDL failed to initialize.
If n <= 0 Then
MsgBox ("IDL failed to initialize")
End
End If
When a box is drawn with the "IDLDrawWidget" icon in the toolbar, an OCX frame is created. This is a container for the IDLDrawWidget. This container is analogous to an IDL widget base. The graphics window that will be used by IDL still must be created. This is accomplished with the CreateDrawWidget method, as shown in the following statement:
IDLDrawWidget1.CreateDrawWidget
The example program displays any output returned by IDL in a text box created in Visual Basic. This is accomplished with the SetOutputWnd method of the IDLDrawWidget. The SetOutputWnd method takes the hWnd of the text box that will contain the IDL output as an argument. The text box in the example program is named IDL_Output_Box, hence the following statement.
IDLDrawWidget1.SetOutputWnd (IDL_Output_Box.hWnd)
| Note Although this is the last statement within the Form_Load() subroutine, it could be placed before the call to InitIDL to get standard IDL version information printed. |
The easiest way to integrate IDL with Visual Basic is to let Visual Basic manage the events and pass instructions to IDL. Recall that our example program contains two buttons: "Plot Data" and "Exit". When you double-click on "Plot Data", Visual Basic automatically creates the following subroutine:
Private Sub Plot_Button_Click() End Sub
Visual Basic will execute any statements within this subroutine when the user clicks "Plot Data". Instructions are passed to IDL using the ExecuteStr method to the IDLDrawWidget. The ExecuteStr method takes a string as an argument. This string is passed to IDL for execution as if it were entered at the IDL command line.
The five statements which follow instruct IDL to produce the surface plot shown in the figure above.
IDLDrawWidget1.ExecuteStr ("Z = SHIFT(DIST(40), 20, 20)")
IDLDrawWidget1.ExecuteStr ("Z = EXP(-(Z/10)^2)")
IDLDrawWidget1.ExecuteStr ("SURFACE, Z")
IDLDrawWidget1.ExecuteStr ("PRINT, SIZE(Z)")
This project exits when the user clicks "Exit". Exiting is a two step process. IDL is given a chance to clean up and exit by issuing the DoExit method. The Visual Basic program then exits with an End statement.
Private Sub Exit_Button_Click() IDLDrawWidget1.DoExit End End Sub
IDL Online Help (March 06, 2007)