|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
There are a few reasons why you may want to create your ActiveX document as an in-process component (DLL file). The performance of an in-process component surpasses that of the same component compiled as an EXE. In addition, multiple programs accessing the same EXE can overwrite global data; that doesnt happen if they each have their own in-process server. Which ActiveX Component Do I Want To Build?With all the different types of ActiveX components to choose from, how do you decide which type of component you want to create? Take a look at this list:
Thats it for the overview of ActiveX controls and documents for the momentits time to turn to the Immediate Solutions. Immediate SolutionsCreating An ActiveX ControlThe Testing Department is calling again. Wouldnt it be great if you built a new ActiveX control that displayed a digital clock? That control could be reused in many other programs. Hmm, you think, how do you create an ActiveX control? Select the New Project menu item in the Visual Basic File menu to open the New Project dialog box, as shown in Figure 20.1.
Select the ActiveX Control item in the New Project dialog box and click on OK. This creates a new, empty ActiveX control, as shown in Figure 20.2.
Believe it or not, youve created your first ActiveX control, UserControl1. You can even run the control with the Run menus Start item, which would display the control by launching the Microsoft Internet Explorer if you have it installed in your computer; however, there would be nothing to see because the control is empty. The default name of the control is Project1, but we can change that to, say, FirstControl. To do that, select the Project1 Properties item in the Project menu, and type FirstControl into the Project Name box in the Project Properties dialog box, then click on OK. Also, save the project as firstcontrol.vbp. Instead of a FRM file, you save ActiveX controls in CTL files. Select the Save UserControl1 item in the file menu to save the control as firstcontrol.ctl. Heres what appears in that file on disk: VERSION 6.00 Begin VB.UserControl UserControl1 ClientHeight = 3600 ClientLeft = 0 ClientTop = 0 ClientWidth = 4800 ScaleHeight = 3600 ScaleWidth = 4800 End Attribute VB_Name = "UserControl1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = True Attribute VB_PredeclaredId = False Attribute VB_Exposed = True Now that weve seen how to create a new ActiveX control, its time to make it do something. Well see how that works in the next topic. Designing An ActiveX Control From ScratchThe testing department is on the phone. Your new ActiveX control looks fine, but why doesnt it do anything? Should it? you ask. Yes, they say. You can design the appearance of your ActiveX control entirely from scratch, creating an entirely new control, never seen before. In that case, youre responsible for creating the controls appearance from scratch. Later, you can add events to your control, as well as methods and properties, as well see later in this chapter. To design the appearance of your entirely new control, you can use the Visual Basic graphics methods that the UserControl object supports, such as Circle, Line, PSet, Print, Cls, and Point. You can also display an image in the UserControl object by setting its Picture property. Lets see an example. Here, well just draw two lines to crisscross an ActiveX control and draw a black box in the middle. Create a new ActiveX control now, and double-click it at design time to open the code window to the UserControl_Initialize function: Private Sub UserControl_Initialize() End Sub This function is just like the Form Load procedure that were familiar with. Set the controls AutoRedraw property to True so we can draw graphics from UserControl_Initialize, and then draw the lines to crisscross the control, using the Line method and ScaleWidth and ScaleHeight just as you would in a Visual Basic form:
Private Sub UserControl_Initialize()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
...
End Sub
Next, we draw a filled-in black box in the center of the control this way:
Private Sub UserControl_Initialize()
Line (0, 0)-(ScaleWidth, ScaleHeight)
Line (0, ScaleHeight)-(ScaleWidth, 0)
Line (ScaleWidth / 4, ScaleHeight / 4)-(3 * ScaleWidth / 4, _
3 * ScaleHeight / 4), , BF
End Sub
Lets test this new ActiveX control now in the Microsoft Internet Explorer (assuming you have that browser installed). To do that, just select the Run menus Start item now. Doing so opens the Project Properties dialog box, shown in Figure 20.3.
Leave UserControl1 in the Start Component box, and make sure the Use Existing Browser box is clicked, then click on OK. This registers our control with Windows, creates a temporary HTML page with the control embedded in it, and starts the Internet Explorer, as you see in Figure 20.4.
You can see our new ActiveX control in Figure 20.4. Now weve created our first ActiveX control and designed its appearance from scratch. If we wanted to, we could add events, properties, and methods to this control (well see how to do so later in this chapter). Heres the temporary HTML page that Visual Basic creates to display our ActiveX control; note that our control is registered with Windows and has its own ID, so this page can use the HTML <OBJECT> tag to embed one of our controls in the page: <HTML> <BODY> <OBJECT classid="clsid:B2A69D3B-D38C-11D1-8881-E45E08C10000"> </OBJECT> </BODY> </HTML>
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |