Search In Site

28 June, 2013

Creating ActiveX Controls in Vb

ActiveX Controls
We have seen ActiveX controls throughout the book those are the controls you can add to the Visual Basic toolbox using the Components dialog box. You can add those controls to a Visual Basic program like any other control. You can also use ActiveX controls on the Internet, embedding them in your Web pages, as we’ll see when we work on creating ActiveX controls.
 ActiveX controls can support properties, methods, and events. Your ActiveX control can be built entirely from scratch (in other words, you’re responsible for its appearance), it can be built on another control (such as a list box), or it can contain multiple existing controls (these ActiveX controls are said to contain constituent controls).
 Visual Basic ActiveX controls are based on the Visual Basic UserControl object. When you create an ActiveX control, you create a control class file with the extension .ctl. Visual Basic uses that file to create the actual control, which has the extension .ocx. After you register that control with Windows (you can use Windows utilities like regsvr32.exe to register a control, as we’ll see in this chapter), the control will appear in the Visual Basic Components dialog box, ready for you to add to a program. You can also use these controls in Web pages.

Code Components
Code components were formerly called OLE automation servers. These objects let you use their code in other programs.
 For example, you might have a calculation routine that you expose in a code component; doing so makes that routine available to other programs. Code components can support properties and methods. If you take a look at the kind of ActiveX components you can build with Visual Basic in the NewProject window, you’ll see all kinds:
• ActiveX document DLL, ActiveX EXE
• ActiveX control
• ActiveX EXE, ActiveX DLL (these are code components)

There’s still quite a confusion of terms here what’s the difference between a DLL and EXE ActiveX component? Let’s explore further.

In-Process Vs. Out-Of-Process Components
If an ActiveX component has been implemented as part of an executable file (EXE file), it is an out-of-process server and runs in its own process. If it has been implemented as a dynamic link library (DLL file), it is an in-process server and runs in the same process as the client application. If your ActiveX component is an out-of-process server, it is an EXE file, and can run standalone.
Applications that use in-process servers usually run faster than those that use out-of-process servers because the application doesn’t have to cross process boundaries to use an object’s properties, methods, and events.
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 doesn’t 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:
• To build an invisible component that provides routines in code that you can call, build a code component (ActiveX EXE or an ActiveX DLL).
• To build a component that can run in the same process with your application, build an ActiveX DLL.
• To build a component that can serve multiple applications and can run on a remote computer,build an ActiveX EXE.
• To build a visible component that can be dropped into an application at design time, build an ActiveX control.
• To build a visible component that can take over an application window at runtime, build an ActiveX document.

That’s it for the overview of ActiveX controls and documents for the moment it’s time to turn to the Immediate Solutions. The Testing Department is calling again. Wouldn’t 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?

Creating An ActiveX Control
Select the New Project menu item in the Visual Basic File menu to open the New Project dialog box. Select the ActiveX Control item in the New Project dialog box and click on OK. This creates a new empty ActiveX control, Believe it or not, you’ve created your first ActiveX control, UserControl1. You can even run the control with the Run menu’s 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. Here’s 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


Designing An ActiveX Control From Scratch
The testing department is on the phone. Your new ActiveX control looks fine, but why doesn’t 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, you’re responsible for creating the control’s appearance from scratch. Later, you can add events to your control, as well as methods and properties, 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.
Let’s see an example. Here, we’ll 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 we’re familiar with. Set the control’s 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

Let’s test this new ActiveX control now in the Microsoft Internet Explorer (assuming you have that browser installed). To do that, just select the Run menu’s Start item now. Doing so opens the Project Properties dialog box, 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, You can see our new ActiveX control in Figure 20.4. Now we’ve 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 .Here’s 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>

0 comments:

Post a Comment

Dear Visitors All The Tricks And Hacks Posted Here Are Only For Knowledge Purpose.Don't Use These for Illegal Operations.

 
Twitter Bird Gadget