Integrating with Visual Basic

[Previous] [Next]

Wait! Don't stop reading because you're a C++ developer! Visual Basic is a powerful tool for developing applications, particularly in the user interface (UI) tier. Visual Basic is one of the most common programming environments in which ActiveX controls appear. In fact, you'll find many more Visual Basic developers than C++ developers—and they all have checkbooks. So it makes sense to learn how they perceive the world and develop software, especially if you want them to use your controls. Let's take a look at how ActiveX controls intermingle with Visual Basic.

The Nature of Visual Basic

Many C++ developers find that Visual Basic is a handy front-end tool. This viewpoint is certainly justifiable. If you're used to developing Microsoft Windows applications using C++, you'll probably find developing Visual Basic applications much simpler, mainly because Visual Basic takes care of most underlying details for you. When you develop in Visual Basic, you can say goodbye to complex message handlers, messy memory allocation routines, and so forth. However, you do lose some of the flexibility you get with C++.

When creating a new Visual Basic project, you have the choice of creating several types of applications or components, including regular EXEs and DLLs, ActiveX EXEs and DLLs, ActiveX controls, Visual Basic add-ins, ActiveX Document applications, as well as some others. The Visual Basic New Project Wizard will pump out all the boilerplate code, which varies depending on the type of application or component you ask it to generate.

When developing applications using Visual Basic, you might find yourself spending much of your time editing a form. A form is basically a window containing UI components. Developing the application involves placing various UI components on the form and writing handlers for the events generated by the controls. Perhaps this definition is a bit oversimplified, but hey—we're C++ programmers. Of course, this sort of development is similar to normal Windows development, except that Visual Basic hides all the grunge.

Because you're familiar with managing dialog boxes using the resource editor in Visual C++, you already know how to work with a Visual Basic form; arranging controls on a Visual Basic form is like working with the Visual C++ resource manager. You get a blank form and Visual Basic's toolbox, which provides all the standard controls (list boxes, group boxes, combo boxes, and so on). In addition to the standard controls, you can use ActiveX controls in your project.

Including ActiveX Controls in Your Project

To add an ActiveX control to your project, select Components from the Project menu. Visual Basic then finds all the ActiveX controls that have registered themselves in the Registry. Simply choose the ones you want to use in your project, and mark their check boxes. Figure 13-1 shows the Visual Basic Components dialog box for adding controls to your project.

After you select a control, Visual Basic places an icon representing it in the toolbox. Figure 13-2 shows a new control in the toolbox. Once this control is referenced in the project, Visual Basic knows the control's properties and methods (from the control's type information)—and you're free to use it.

click to view at full size.

Figure 13-1. The Visual Basic Components dialog box.

click to view at full size.

Figure 13-2. A new control in the Visual Basic toolbox.

Using the Control

The easiest way to use a control in Visual Basic is to select it from the toolbox and drag it onto the form. For example, if you want to use the ATL message traffic control from Chapter 10, simply insert the control in the project. When the control appears in the toolbox, drag it onto the form as you would any other control. Visual Basic renders the control on the form and creates an instance of the control; it's just like using regular controls.

If, for example, you drag the ATLMsgTraffic control onto a form, Visual Basic draws the form as shown in Figure 13-3 and creates an instance of the control named ATLMsgTrafficCtl1. (You can change the name of this control by editing its properties in the Properties window.)

click to view at full size.

Figure 13-3. The ATLMsgTraffic control as it appears in a Visual Basic form.

Visual Basic and ActiveX Control Methods

After you've placed the control on the form, you can program the control by invoking various methods on it. In Visual Basic, methods are dereferenced using a period. For example, if you want to start the message traffic graph as soon as the Visual Basic form loads, you can do so by invoking the StartGraph method when the form loads. Here's the snippet of Visual Basic code showing how to start the graph:

 Private Sub Form_Load() ATLMsgTrafficCtl1.StartGraph End Sub 

Visual Basic and Control Properties

Once the control is on the form, you can manipulate the control programmatically. Remember that the control is a COM object with methods and properties. When you made a reference to the object in your Visual Basic project, Visual Basic read the control's type information to determine its properties and methods.

Just as you can adjust the properties of the regular controls on your form, you can adjust the properties of an ActiveX control on the form. The Properties window lists both the stock properties such as foreground and background color and the properties you defined as custom properties.

If you put a breakpoint in the control's loading and saving code while Visual Basic is hosting it, you'll see that Visual Basic uses the IPersistStreamInit interface to manage property persistence by default. (Put a breakpoint in your control's property map to see this happen.) Visual Basic takes these properties and saves them in a file named formname.frx. For example, if you put the control in a form named MainForm, Visual Basic stores the properties in a file named MainForm.frx.

If your control doesn't implement IPersistStreamInit, Visual Basic tries to use the IPersistPropertyBag interface to store the properties in the form itself, as shown in this Visual Basic code:

 Begin ATLMSGTRAFFICLibCtl.ATLMsgTrafficCtl ATLMsgTrafficCtl1      Height          =   3135     Left            =   360     TabIndex        =   0     Top             =   240     Width           =   4815     BackColor       =   -2147483648     GraphLineColor  =   -2147483642     Threshold       =   217     Interval        =   1117     _cx             =   8493     _cy             =   5530 End 

In addition to managing the properties through the Properties window at design time, you can also manipulate the properties programmatically at run time. For example, if you want to set the timing interval for the ATLMsgTraffic control programmatically, you can use the following code snippet. This code assumes that the form contains a button named SetInterval and an edit control named Interval. It takes the value of the data that the user types into the edit box and changes the control's interval property accordingly.

 Private Sub SetInterval_Click()     ATLMsgTrafficCtl1.Interval = Val(Interval.Text) End Sub 

Extended Controls

As you've already seen, an ActiveX control is just a COM class that implements a number of interfaces. ActiveX controls use the entire OLE Embedding protocol. It's fairly complex—the container and the control exchange a ton of interfaces, such as IOleObject, IOleControl, IDataObject, and IOleControlSite. When a container creates a control, the only information the container has is the properties and methods supported by the control.

To provide a means of manipulating a control with properties and methods that are specific to the container, some containers implement extended controls, wrappers to enhance the controls living within that container. For example, many containers would benefit from storing information about the control, such as the size and position of the control on a form; and though these properties aren't normally part of a control, an extended control can store this information. That way, properties such as size and position appear as native properties to the control itself.

By no means is it necessary for the container to implement extended controls; they just offer a convenient way for containers to provide standard functionality (such as size and position properties) on behalf of the control. You can tell whether a container implements an extended control by asking the container through the IOleControlSite::GetExtendedControl interface. If the container doesn't implement an extended control, IOleControlSite::GetExtendedControl will return the distinguishing HRESULT E_NOTIMPL. Otherwise, GetExtendedControl will return the IDispatch interface for the extended control managed by the site. Another control can then access any properties of the extended control itself, if necessary.

Visual Basic implements extended controls. The following code shows how the ATLMsgTraffic control retrieves this information:

 STDMETHODIMP CATLMsgTrafficCtl::HasExtendedControl() {     HRESULT hr = S_FALSE;     IOleControlSite* pOleControlSite = 0;     // Call this only after the control     //  is fully embedded and active.      m_spClientSite->QueryInterface(IID_IOleControlSite,         (void**)&pOleControlSite);     if(pOleControlSite)     {         IDispatch* pDispatch = 0;         pOleControlSite->GetExtendedControl(&pDispatch);         if(pDispatch)         {             hr = S_OK;             OutputDebugString("Found an extended control\n");             pDispatch->Release();         }         pOleControlSite->Release();     }     return hr; } 

Visual Basic and Events

The last Visual Basic topic we need to cover is how to hook up event handlers for the controls. Remember that the ATLMsgTraffic control defines a default event set that fires two events: ExceededThreshold and NewInterval. When Visual Basic read the type information about the control, it also retrieved all the information necessary for implementing the event interface. When you select the ATLMsgTrafficCtl1 object on the form (using the combo box in the upper left-hand corner of the form editor), the combo box in the right-hand corner lists the events generated by the control. Each time you select an event from the combo box in the right-hand corner of the form editor, Visual Basic inserts a stub for the event handler. Visual Basic hooks up to the control using connection points. (See Chapter 12 for more information about connection points.) Of course, you never actually see this process. The following code shows a Visual Basic application responding to events generated by the ATLMsgTraffic control by placing strings in a list box named NormalEvents.

 Private Sub ATLMsgTrafficCtl1_ExceededThreshold( _     ByVal NumMessages As Long, ByVal CurrentThreshold As Long)     Dim eventStr As String          eventStr = "Exceeded Threshold: " + Str(CurrentThreshold)     NormalEvents.AddItem eventStr End Sub Private Sub ATLMsgTrafficCtl1_NewInterval( _     ByVal NumMessages As Long)     Dim eventStr As String          eventStr = "New Interval" + Str(NumMessages)     NormalEvents.AddItem eventStr End Sub 



Inside Atl
Inside ATL (Programming Languages/C)
ISBN: 1572318589
EAN: 2147483647
Year: 1998
Pages: 127

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net