Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Listing 26.4 olemultiple.frm version 2

VERSION 6.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3015
   ClientLeft      =   165
   ClientTop       =   735
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3015
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.OLE OLEControls
      Height          =   1215
      Index           =   1
      Left            =   840
      SizeMode        =   2  'AutoSize
      TabIndex        =   1
      Top             =   1680
      Width           =   3015
   End
   Begin VB.OLE OLEControls
      Height          =   1215
      Index           =   0
      Left            =   840
      SizeMode        =   2  'AutoSize
      TabIndex        =   0
      Top             =   240
      Width           =   3015
   End
   Begin VB.Menu File
      Caption         =   "File"
      Begin VB.Menu ActivateObject
         Caption         =   "Activate object"
      End
      Begin VB.Menu CreateNewOLEControl
         Caption         =   "Create new OLE control"
      End
   End
   Begin VB.Menu Insert
      Caption         =   "Insert"
      Begin VB.Menu InsertObject
         Caption         =   "Insert object"
      End
      Begin VB.Menu PasteSpecial
         Caption         =   "Paste special"
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim intTotalOLEControls As Integer
Dim intXOffset, intYOffset As Integer

Private Sub ActivateObject_Click()
    If TypeOf ActiveControl Is OLE Then
        ActiveControl.DoVerb 0
    End If
End Sub

Private Sub CreateNewOLEControl_Click()
        intTotalOLEControls = intTotalOLEControls + 1

        Load OLEControls(intTotalOLEControls - 1)
        OLEControls(intTotalOLEControls - 1).Move 0, 0
        OLEControls(intTotalOLEControls - 1).Visible = True
        OLEControls(intTotalOLEControls - 1).InsertObjDlg

        If OLEControls(intTotalOLEControls - 1).OLEType = None Then
            MsgBox "OLE operation failed."
        End If
End Sub

Private Sub Form_Load()
    intTotalOLEControls = 2
End Sub

Private Sub InsertObject_Click()
    If TypeOf ActiveControl Is OLE Then
        ActiveControl.InsertObjDlg
        If ActiveControl.OLEType = None Then
            MsgBox "OLE operation failed."
        End If
    End If
End Sub


Private Sub PasteSpecial_Click()
    If TypeOf ActiveControl Is OLE Then
        If ActiveControl.PasteOK Then
            ActiveControl.PasteSpecialDlg
        End If
        If ActiveControl.OLEType = None Then
            MsgBox "OLE operation failed."
        End If
    End If
End Sub

Private Sub Form_Click()
    Dim intLoopIndex As Integer

    For intLoopIndex = 0 To intTotalOLEControls - 1
        OLEControls(intLoopIndex).AppIsRunning = False
    Next intLoopIndex
End Sub

Dragging OLE Objects In A Form

In most OLE container programs, such as Microsoft Word, users can position OLE objects as they like in order to create a composite document. To let users position OLE objects in a form as they would like, we can support dragging those controls using their Drag method:

OLEControl.Drag action

Here are the possible values for the action argument:

  vbCancel—Cancels the drag operation.
  vbBeginDrag—Starts the drag operation
  vbEndDrag—Ends the drag operation.

Let’s see an example. We’ll modify the olemultiple example program that we’ve developed in the previous few topics to let the user drag OLE controls. When the user presses the mouse in an OLE control, we will start the drag operation, and we do that by recording the location of the mouse in two new form-wide variables, intXOffset and intYOffset:

Dim intXOffset, intYOffset As Integer

Here’s how we set those variables and begin the drag operation when the user presses the mouse button:

Private Sub OLEControls_MouseDown(Index As Integer, Button As Integer, _
   Shift As Integer, X As Single, Y As Single)
   intXOffset = X
   intYOffset = Y
   OLEControls(Index).Drag vbBeginDrag
End Sub

Now when the user drags the control to a new location in the form, we will move the control to that new location using the form’s DragDrop event handler:

Private Sub Form_DragDrop(Source As Control, X As Single, Y As Single)
    Source.Move X - intXOffset, Y - intYOffset
End Sub

If the user doesn’t move the control far enough, however, the control itself gets a DragDrop event when the user drops it, because Visual Basic is assuming the user is dropping the control on top of itself. To handle that case, we set up a DragDrop event handler for the OLE control itself, convert the coordinates we are passed in that event handler from coordinates local to the OLE control to coordinates based on the form’s client area, and move the control to its new location:

Private Sub OLEControls_DragDrop(Index As Integer, Source As Control, _
    X As Single, Y As Single)
    Source.Move X + OLEControls(Index).Left - intXOffset, Y + _
        OLEControls(Index).Top - intYOffset
End Sub

And that’s it—now the user can drag and drop the OLE controls in our olemultiple example, as shown in Figure 26.14. The final code for this example is located in the olemultiple folder on this book’s accompanying CD-ROM.


Figure 26.14  Letting the user drag OLE controls to position them.

Deleting OLE Objects

Besides adding OLE objects, OLE container programs should also allow the user to delete those objects. You can delete an OLE object in an OLE control with the control’s Delete method (this method takes no parameters). Let’s see an example. Here, we add code to a menu item, Delete Object, to delete an OLE object in the currently active OLE control. First, we check to see if the type of control that’s currently active (that is, has the focus) is an OLE control:

Private Sub DeleteObject_Click()
    If TypeOf ActiveControl Is OLE Then
...
    End If
End Sub

If the active control is an OLE control, we delete the OLE object in that control with the control’s Delete method:

Private Sub DeleteObject_Click()
    If TypeOf ActiveControl Is OLE Then
        ActiveControl.Delete
    End If
End Sub

This method deletes the OLE object in the control and does not, as the name of this method might make you think, delete the control itself.


Previous Table of Contents Next


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.