VB6 beginners tutorial - Learn VB6

Advanced VB6 tutorial - Learn Advanced VB6

Systems Analysis - System analysis and Design tutorial for Software Engineering

Browse Topics

- Getting started
- Data Types
- Modules
- Operators in VB6
- VB6 Variable
- VB6 Procedures
- VB6 Control Structures
- Loops in VB6
- VB6 Exit Do & With End With
- Arrays in VB6
- User-Defined Data Types
- VB6 Constants
VB6 Built-in Functions
- Date and Time in VB6
- VB6 Controls
- TextBox Control
- ComboBox & OptionButton
- Label & Frame
- PictureBox & ImageBox
- Timer Control
- ListBox & ComboBox
- VB6 ScrollBar
- Control Arrays in VB6
- Files controls in VB6
- VB6 CheckBox
- Forms in VB6
- Menus in VB6
- MDI Form in VB6
- InputBox
- MessageBox
- Mouse events
- Mouse Move
- Error Handling
Error Handling (2)
VB6 Database

You are here: Visual Basic > VB6 (Beginners Tutorial)

Tutorial Main Page | Previous Page | Contents | Next Page

Fun With Graphics

• One of the biggest uses of the API is for graphics, whether it be background scrolling, sprite animation, or many other special effects. A very versatile API function is BitBlt, which stands for Bit Block Transfer. It is used to copy a section of one bitmap from one place (the source) to another (the destination).

• Let’s look at the Declaration statement for BitBlt (from the API Text Viewer):

PrivateDeclare Function BitBlt Lib "gdi32" Alias "BitBlt" (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

Lots of stuff here, but fairly straightforward. hDestDC is the device context handle, or hDC of the destination bitmap. The coordinate pair (X, Y) specifies the upper left corner in the destination bitmap to copy the source. The parameters nWidth and nHeight are, respectively, the width and height of the copied bitmap. hSrcDC is the device context handle for the source bitmap and (Xsrc, Ysrc) is the upper left corner of the region of the source bitmap being copied. Finally, dwRop is a constant that defines how the bitmap is to be copied. We will do a direct copy or set dwRop equal to the constant SRCCOPY. The BitBlt function expects all geometric units to be pixels.

• BitBlt returns an long integer value -- we won’t be concerned with its use right now. So, the syntax for using BitBlt is:

Dim RtnValue As Long
RtnValue = BitBlt(Dest.hDC, X, Y, Width, Height,
Src.hDC, Xsrc, Ysrc, SRCCOPY)

This function call takes the Src bitmap, located at (Xsrc, Ysrc), with width Width and height Height, and copies it directly to the Dest bitmap at (X, Y).

Quick Example 9 - Bouncing Ball With Sound!

We’ll build an application with a ball bouncing from the top to the bottom as an illustration of the use of BitBlt.

  1. Start a new application. Add two picture boxes, a shape (inside the smaller picture box), a timer control, and a command button.:

  2. For Picture1 (the destination), set the ScaleMode property to Pixel. For Shape1, set the FillStyle property to Solid, the Shape property to Circle, and choose a FillColor. For Picture2 (the ball), set the ScaleMode property to Pixel and the BorderStyle property to None. For Timer1, set the Enabled property to False and the Interval property to 100.

  3. Copy and paste constants for the BitBlt Declare statement and constants. Also copy and paste the necessary sndPlaySound statements and declare some variables. The general declarations area is thus:

    Option Explicit
    Dim BongSound As String
    Dim BallY As Long, BallDir As Integer
    Private Declare Function sndPlaySound Lib "winmm.dll" Alias "sndPlaySoundA" (ByVal lpszSoundName As String, ByVal uFlags As Long) As Long
    Private Const SND_ASYNC = &H1
    Private Const SND_SYNC = &H0
    Private Const SND_MEMORY = &H4
    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 Const SRCCOPY = &HCC0020

  4. Add a Form_Load procedure:

    Private Sub Form_Load()
    BallY = 0
    BallDir = 1
    BongSound = StoreSound("bong.wav")
    End Sub

  5. Write a Command1_Click event procedure to toggle the timer:

    Private Sub Command1_Click()
    Timer1.Enabled = Not (Timer1.Enabled)
    End Sub

  6. The Timer1_Timer event controls the bouncing ball position:

    Private Sub Timer1_Timer()
    Static BallY As Long
    Dim RtnValue As Long
    BallY = BallY + BallDir * Picture1.ScaleHeight / 50
    If BallY < 0 Then
    BallY = 0
    BallDir = 1
    Call sndPlaySound(BongSound, SND_ASYNC Or SND_MEMORY)
    ElseIf BallY + Picture2.ScaleHeight > Picture1.ScaleHeight Then
    BallY = Picture1.ScaleHeight - Picture2.ScaleHeight
    BallDir = -1
    Call sndPlaySound(BongSound, SND_ASYNC Or SND_MEMORY)
    End If
    RtnValue = BitBlt(Picture1.hDC, CLng(0.5 * (Picture1.ScaleWidth - Picture2.ScaleWidth)), _
    BallY, CLng(Picture2.ScaleWidth), CLng(Picture2.ScaleHeight), Picture2.hDC, CLng(0), CLng(0), SRCCOPY)
    End Sub

  7. We also need to make sure we include the StoreSound procedure from the last example so we can hear the bong when the ball bounces.

  8. Once everything is together, run it and follow the bouncing ball!


Tutorial Main Page | Previous Page | Contents | Next Page


Home | About Us | Privacy Policy | Contact Us

Copyright © | All Rights Reserved