Creating An ActiveX Document
The Testing Department is calling again. The ActiveX controls you’ve been building are fine, but what about going on to the next step? How about creating an ActiveX document? You ask, why are ActiveX documents the next step? Because, they say, although ActiveX controls can appear in Web pages, ActiveX documents can be Web pages.
The Testing Department is calling again. The ActiveX controls you’ve been building are fine, but what about going on to the next step? How about creating an ActiveX document? You ask, why are ActiveX documents the next step? Because, they say, although ActiveX controls can appear in Web pages, ActiveX documents can be Web pages.
That is, an entire Visual Basic form with all its controls can now appear in your Web browser or other application. Let’s see an example. To create a new ActiveX document, open the Visual Basic New Project dialog box, select the ActiveX document EXE entry, and click on OK (you can create either ActiveX document EXEs or DLLs see the next topic for a discussion of the difference). This creates a new ActiveX document in Visual Basic. To get our start with ActiveX documents, just double-click the document and add this code to the Initialize procedure that opens to draw a set of crisscrossing lines and a black box in the center of the document:
Private Sub UserDocument_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
This is the procedure that’s run when the ActiveX document is first opened. Because we’re drawing from the Initialize event handler, set the document’s AutoRedraw property to True (just as you would for a form). Let’s see our new ActiveX document at work in a Web browser (you can use ActiveX documents in other applications that support them, but Web browsers are particularly handy for testing ActiveX documents in an interactive way). Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1) and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the document with the Start item in the Run menu. Starting the document loads the document’s specification, a VBD file, into the browser, which (if it supports ActiveX documents) runs the document’s EXE file or adds the document’s DLL file to its own process. That’s all there is to it. Now we’ve created our first ActiveX document. Still, not much is going on here yet. In the following topics, we’ll develop our ActiveX documents further.
ActiveX Document DLLs Vs. EXEs
You can create both ActiveX document EXEs and DLLs. Here’s the difference: if an ActiveX document is written as 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. Although ActiveX documents are usually built as EXE projects, the benefit of DLLs is that 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. In addition, the performance of an in-process component, or DLL file, surpasses that of the same component compiled as an EXE. Also, multiple programs accessing the same EXE can overwrite global data, but that doesn’t happen if they each have their own in-process server.
Adding Controls To An ActiveX Document (A Tic-Tac-Toe Example)
The Testing Department is on the phone. The ActiveX document you’ve created is very nice, but why can’t you do anything with it? Well, you say, I was just about to add controls to it and create a Web browser game. Using ActiveX documents, you can display entire forms in Web browsers. To see this at work, we’ll create a mini tic-tac-toe game. This game will let users click buttons to display alternate x’s and o’s (although it won’t include the logic to actually play tic-tac-toe). Working with multiple controls in this way will demonstrate how to display entire programs as Web pages. Create a new ActiveX document (EXE or DLL), and add nine command buttons to it arranged in a 3×3 grid in classic tic-tac-toe fashion. Give each button the same name, Command, clear each button’s caption (that is, select the text in the caption and press the backspace key), and when Visual Basic asks if you want to create a control array, click Yes, because a control array will make the code shorter and easier to handle.
To alternate x’s and o’s as the user clicks buttons, we’ll need a Boolean flag, which we’ll call blnXFlag. If this flag is true, the next caption to set will be “x”; otherwise, “o”. Add the declaration of blnXFlag to the (General) section:
Dim blnXFlag As Boolean
We also initalize blnXFlag to True when the document first loads by adding this code to the Initialize event handler:
Private Sub UserDocument_Initialize()
blnXFlag = True
End Sub Now when the user clicks a button, we alternate between setting the clicked buttons’ captions to “x” and “o” this way:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
...
End Sub
At the end of the code, we toggle the state of blnXFlag for the next time the user clicks a button:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub
Let’s see our new ActiveX document game at work. Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1), and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the game with the Start item in the Run menu. Our entire game appears in the Microsoft Internet Explorer. The user can alternate button captions just by clicking the buttons. Our ActiveX document example is a success. We’ll save the document as activextictactoe.dob (ActiveX documents have the extension .dob when
saved in Visual Basic, just as form files have the extension .frm), and the project as activextictactoe.vbp. The default name for the document is UserDocument1, as you can see in the Visual Basic Properties window; if you want to use a different name for the document, set it in the Properties window. We’ll change the document name to activextictactoedoc. When we create the VBD specification file for this document, then, that file will be activextictactoedoc.vbd, and that’s the file to open in your Web browser The code for this example, activextictactoedoc.dob version 1 (version 2 will support persistent data).
VERSION 6.00
Begin VB.UserDocument activextictactoedoc
AutoRedraw = -1 'True
ClientHeight = 2865
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
HScrollSmallChange= 225
ScaleHeight = 2865
ScaleWidth = 4800
VScrollSmallChange= 225
Begin VB.CommandButton Command
Height = 495
Index = 8
Left = 3360
TabIndex = 8
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 7
Left = 3360
TabIndex = 7
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 6
Left = 3360
TabIndex = 6
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 5
Left = 1800
TabIndex = 5
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 4
Left = 1800
TabIndex = 4
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 3
Left = 1800
TabIndex = 3
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 2
Left = 240
TabIndex = 2
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 1
Left = 240
TabIndex = 1
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 0
Left = 240
TabIndex = 0
Top = 360
Width = 1215
End
End
Attribute VB_Name = "activextictactoedoc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Private Sub UserDocument_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
This is the procedure that’s run when the ActiveX document is first opened. Because we’re drawing from the Initialize event handler, set the document’s AutoRedraw property to True (just as you would for a form). Let’s see our new ActiveX document at work in a Web browser (you can use ActiveX documents in other applications that support them, but Web browsers are particularly handy for testing ActiveX documents in an interactive way). Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1) and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the document with the Start item in the Run menu. Starting the document loads the document’s specification, a VBD file, into the browser, which (if it supports ActiveX documents) runs the document’s EXE file or adds the document’s DLL file to its own process. That’s all there is to it. Now we’ve created our first ActiveX document. Still, not much is going on here yet. In the following topics, we’ll develop our ActiveX documents further.
ActiveX Document DLLs Vs. EXEs
You can create both ActiveX document EXEs and DLLs. Here’s the difference: if an ActiveX document is written as 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. Although ActiveX documents are usually built as EXE projects, the benefit of DLLs is that 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. In addition, the performance of an in-process component, or DLL file, surpasses that of the same component compiled as an EXE. Also, multiple programs accessing the same EXE can overwrite global data, but that doesn’t happen if they each have their own in-process server.
Adding Controls To An ActiveX Document (A Tic-Tac-Toe Example)
The Testing Department is on the phone. The ActiveX document you’ve created is very nice, but why can’t you do anything with it? Well, you say, I was just about to add controls to it and create a Web browser game. Using ActiveX documents, you can display entire forms in Web browsers. To see this at work, we’ll create a mini tic-tac-toe game. This game will let users click buttons to display alternate x’s and o’s (although it won’t include the logic to actually play tic-tac-toe). Working with multiple controls in this way will demonstrate how to display entire programs as Web pages. Create a new ActiveX document (EXE or DLL), and add nine command buttons to it arranged in a 3×3 grid in classic tic-tac-toe fashion. Give each button the same name, Command, clear each button’s caption (that is, select the text in the caption and press the backspace key), and when Visual Basic asks if you want to create a control array, click Yes, because a control array will make the code shorter and easier to handle.
To alternate x’s and o’s as the user clicks buttons, we’ll need a Boolean flag, which we’ll call blnXFlag. If this flag is true, the next caption to set will be “x”; otherwise, “o”. Add the declaration of blnXFlag to the (General) section:
Dim blnXFlag As Boolean
We also initalize blnXFlag to True when the document first loads by adding this code to the Initialize event handler:
Private Sub UserDocument_Initialize()
blnXFlag = True
End Sub Now when the user clicks a button, we alternate between setting the clicked buttons’ captions to “x” and “o” this way:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
...
End Sub
At the end of the code, we toggle the state of blnXFlag for the next time the user clicks a button:
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub
Let’s see our new ActiveX document game at work. Select the Project1 Properties item in the Project menu, and click the Debugging tab in the Project Properties dialog box that opens. Make sure that the option button labeled Start Component is selected (the start component should be given as UserDocument1), and the box labeled Use Existing Browser is checked. Then close the dialog box by clicking on OK, and run the game with the Start item in the Run menu. Our entire game appears in the Microsoft Internet Explorer. The user can alternate button captions just by clicking the buttons. Our ActiveX document example is a success. We’ll save the document as activextictactoe.dob (ActiveX documents have the extension .dob when
saved in Visual Basic, just as form files have the extension .frm), and the project as activextictactoe.vbp. The default name for the document is UserDocument1, as you can see in the Visual Basic Properties window; if you want to use a different name for the document, set it in the Properties window. We’ll change the document name to activextictactoedoc. When we create the VBD specification file for this document, then, that file will be activextictactoedoc.vbd, and that’s the file to open in your Web browser The code for this example, activextictactoedoc.dob version 1 (version 2 will support persistent data).
VERSION 6.00
Begin VB.UserDocument activextictactoedoc
AutoRedraw = -1 'True
ClientHeight = 2865
ClientLeft = 0
ClientTop = 0
ClientWidth = 4800
HScrollSmallChange= 225
ScaleHeight = 2865
ScaleWidth = 4800
VScrollSmallChange= 225
Begin VB.CommandButton Command
Height = 495
Index = 8
Left = 3360
TabIndex = 8
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 7
Left = 3360
TabIndex = 7
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 6
Left = 3360
TabIndex = 6
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 5
Left = 1800
TabIndex = 5
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 4
Left = 1800
TabIndex = 4
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 3
Left = 1800
TabIndex = 3
Top = 360
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 2
Left = 240
TabIndex = 2
Top = 1920
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 1
Left = 240
TabIndex = 1
Top = 1080
Width = 1215
End
Begin VB.CommandButton Command
Height = 495
Index = 0
Left = 240
TabIndex = 0
Top = 360
Width = 1215
End
End
Attribute VB_Name = "activextictactoedoc"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = True
Option Explicit
Dim blnXFlag As Boolean
Private Sub Command_Click(Index As Integer)
Private Sub Command_Click(Index As Integer)
If blnXFlag Then
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub
Command(Index).Caption = "x"
Else
Command(Index).Caption = "o"
End If
blnXFlag = Not blnXFlag
End Sub
Private Sub UserDocument_Initialize()
blnXFlag = True
End Sub
blnXFlag = True
End Sub
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.