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


This program is a fairly complex one, involving lots of Visual Basic code, and it appears in Listing 23.1 for reference. This program, screencap, puts together almost all of the Windows API operations we’ve seen so far in this chapter, so it’s worth taking a look at as an example of a substantial program that uses the Windows API.

Listing 23.1 screencap.frm

VERSION 6.00
Begin VB.Form Form1
   Caption   = "Form1"
   ClientHeight = 4755
   ClientLeft  = 1425
   ClientTop  = 2175
   ClientWidth  = 6495
   LinkTopic  = "Form1"
   PaletteMode  = 1 'UseZOrder
   ScaleHeight  = 317
   ScaleMode  = 3 'Pixel
   ScaleWidth  = 433
   Begin VB.PictureBox Picture1
      AutoRedraw  = -1 'True
      BackColor  = &H00FFFFFF&
      Height   = 3735
      Left   = 480
      ScaleHeight  = 3675
      ScaleWidth  = 5595
      TabIndex  = 0
      Top    = 120
      Width   = 5655
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Dim blnStartCapture As Boolean
Dim blnAmCapturing As Boolean
Dim intHandleDisplay As Integer
Dim intStartX As Integer
Dim intStartY As Integer
Dim intMouseX As Integer
Dim intMouseY As Integer
Dim intOldMouseX As Integer
Dim intOldMouseY As Integer

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) _
    As Long
Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal _
    lpDriverName As String, ByVal lpDeviceName As String, ByVal _
    lpOutput As String, lpInitData As DEVMODE) As Long
Private Declare Function SetROP2 Lib "gdi32" (ByVal hdc As Long, ByVal_
    nDrawMode As Long) As Long
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long,
    ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x _
    As Long, ByVal y As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
    ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal _
    nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal _
    ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As _
    Long, lpPoint As POINTAPI) As Long

Const SRCCOPY = &HCC0020
Const R2_NOT = 6

Private Sub Form_Load()
    Dim devDevMode As DEVMODE

    blnStartCapture = False
    blnAmCapturing = False

    intHandleDisplay = CreateDC("DISPLAY", 0&, 0&, devDevMode)
End Sub

Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As _
    Single, y As Single)
    Dim ptPoint As POINTAPI

    If Button = 1 Then
        blnStartCapture = True
        intRetVal = SetCapture(hwnd)
    End If

    If blnStartCapture And Button = 2 Then

        ptPoint.x = x
        ptPoint.y = y
        retval = ClientToScreen(hwnd, ptPoint)
        intStartX = ptPoint.x
        intStartY = ptPoint.y

        intOldMouseX = intStartX
        intOldMouseY = intStartY

        blnAmCapturing = True
    End If
End Sub
Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y _
    As Single)

    If blnAmCapturing Then

        EraseOld x, y

        intOldMouseX = intMouseX
        intOldMouseY = intMouseY

        DrawNew x, y

    End If
End Sub

Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As_
    Single)
    Dim intLeftX As Integer
    Dim intLeftY As Integer

    If blnAmCapturing Then
        ReleaseCapture
        blnStartCapture = False
        blnAmCapturing = False

        EraseOld x, y

        intLeftX = intMouseX
        If intStartX < intMouseX Then
            intLeftX = intStartX
        End If

        intLeftY = intMouseY
        If intStartY < intMouseY Then
            intLeftY = intStartY
        End If

        Picture1.Cls
        intRetVal = BitBlt(Picture1.hdc, 0, 0, Abs(intStartX - _
            intMouseX), Abs(intStartY - intMouseY), intHandleDisplay, _
            intLeftX, intLeftY, SRCCOPY)

    End If
End Sub

Private Sub EraseOld(ByVal x As Integer, ByVal y As Integer)
    Dim ptPoint As POINTAPI

    intRetVal = SetROP2(intHandleDisplay, R2_NOT)

    ptPoint.x = x
    ptPoint.y = y
    retval = ClientToScreen(hwnd, ptPoint)
    intMouseX = ptPoint.x
    intMouseY = ptPoint.y

    lngRetVal = MoveToEx(intHandleDisplay, intOldMouseX, intOldMouseY, _
        ptPoint)
    intRetVal = LineTo(intHandleDisplay, intStartX, intOldMouseY)
    intRetVal = LineTo(intHandleDisplay, intStartX, intStartY)
    intRetVal = LineTo(intHandleDisplay, intOldMouseX, intStartY)
    intRetVal = LineTo(intHandleDisplay, intOldMouseX, intOldMouseY)

End Sub

Private Sub DrawNew(ByVal x As Integer, ByVal y As Integer)
    Dim ptPoint As POINTAPI

    intRetVal = SetROP2(intHandleDisplay, R2_NOT)

     ptPoint.x = x
     ptPoint.y = y
     retval = ClientToScreen(hwnd, ptPoint)
     intMouseX = ptPoint.x
     intMouseY = ptPoint.y

     lngRetVal = MoveToEx(intHandleDisplay, intMouseX, intMouseY, _
         ptPoint)
     intRetVal = LineTo(intHandleDisplay, intStartX, intMouseY)
     intRetVal = LineTo(intHandleDisplay, intStartX, intStartY)
     intRetVal = LineTo(intHandleDisplay, intMouseX, intStartY)
     intRetVal = LineTo(intHandleDisplay, intMouseX, intMouseY)

End Sub

Private Sub Form_Unload(Cancel As Integer)
    intRetVal = DeleteDC(intHandleDisplay)
End Sub

Getting A Window Handle For Any Window On The Screen

To work with windows in the Windows API, you need a window handle. One way of getting a handle for any window on the screen is to use the WindowFromPoint function. You just pass that function the x- and y-coordinates (in screen coordinates) of a point inside the window for which you want the handle:

Private Declare Function WindowFromPoint Lib "user32" (ByVal xPoint As _
    Long, ByVal yPoint As Long) As Long

Let’s see an example. Here, we’ll let the users click a command button, Command1, with the caption Choose Window. When they do, they can move the mouse to any window on the screen, click the window, and get a handle for that window.

When the users click the Choose Window button, we’ll set up a form-wide Boolean variable, blnChoose, to True, so we know the next time the users click the mouse, they’re indicating the window for which they want a handle:

Dim blnChoose As Boolean


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.