Results 1 to 23 of 23

Thread: VB 6 - BitBlt

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Talking VB 6 - BitBlt

    Hey All i noticed there was no tutorial on this forum for BiBlt so here one I made (please note this was origionally made for another forum, but i began to dislike using it so i came here )

    Comments and improvements are welcome


    So you want to learn BitBlt but dont know where to start? Well hopefully this tutorial will get you on the road to becoming a BitBlt master

    So what is BitBlt?

    Well BitBlt is one of many windows API's

    Getting Started

    So lets jump right in and get some coding done

    Ok first things first open up a new VB project and we are going to add a module, name it 'mdlBitblt' and name the form 'frmbitblt' In the module we are going to place the BitBlt API

    so in the module enter the following...



    VB Code:
    1. Public Declare Function BitBlt Lib"gdi32" _
    2.  (ByVal hDestDC As Long, ByVal X As Long,_
    3.  ByVal Y As Long, ByVal nWidth As Long, _
    4.  ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc _
    5.  As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long



    Ok dont be put off bye the way it looks, it looks ALOT harder than it actually is!

    Next Stage

    So we have the API in place now all we need to do is use it

    As you will see I have attached 3 pictures, Hero, Hero Mask, and Land
    You will need to draw up 3 picture boxes on your form and call them the following

    Hero
    HeroMask
    Land

    Set the 'autoredraw' property to True, and the 'visible' property to false, it is also a good idea to set the 'scalemode' to 3 – Pixels (also set the forms 'scalemode' to pixels)

    Ok now into each picture box load its respective image

    Next draw 3 command buttons and add the following names and captions –

    Name = cmddrawland caption = Draw Land
    Name = cmddrawmask caption = Draw Mask
    Name = cmddrawhero caption = Draw Hero

    Were all set up now all we need is the code

    The Land Code

    we will start by drawing the land....


    VB Code:
    1. Private Sub cmdDrawLand_click()
    2.  drawLand ' goes to the ‘drawland’ sub
    3.  end sub



    In the module…

    VB Code:
    1. Public Sub drawLand()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 300, 300, frmBitBlt.Land.hDC, 0, 0, vbSrcCopy
    3.  End Sub



    Ok so lets go through that code….

    BitBlt - is saying we want to use the BitBlt Api,
    frmBitBlt.hDC - is where we are going to BitBlt to,
    50 is the X position on the surface your are BitBlt'ing on
    50 is the Y position on the surface your are BitBlt'ing on
    300 is the width
    300 is the height
    frmbitblt.land.hDC this is what we are actually going to Copy from
    0 and 0 - this is the area of the picture u wish to ‘cut out’ for now don’t worry about this
    VBSrcCopy - this is the type of bitblt for the background we will just copy it!

    You may have noticed this .HDC well heres a definition...
    This is an upgrade from previous versions of Visual Basic which allows you to interface with the windows api functions. You will find it on certain controls and all forms as the property .hdc or even hWnd.

    The term hdc is the handle to the device context of that window or control.

    The Hero Mask Code
    And Now we will draw our Hero Mask....

    But what is a mask? Well a mask in terms of BitBlt is basicly a copy of the actuall image, in our case all black, which we put on the background first to stop the background colours leaking through and spoiling our image


    VB Code:
    1. Private Sub cmdDrawmask_click()
    2.  drawMask ' this will go to the ‘drawMask’ sub in the module
    3.  End Sub



    In the module….

    VB:
    VB Code:
    1. Public Sub drawMask()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.HeroMask.hDC, 0, 0, vbSrcAnd
    3.  End Sub



    The only differance in the one above is vbSrcAnd which is what we use for masking

    The Hero Code

    And now we will draw our hero on top of the mask....


    VB Code:
    1. Private Sub cmdDrawHero_click()
    2.  drawHero ' this will go to the ‘drawhero’ sub in the module
    3.  End Sub


    In the module….


    VB Code:
    1. Public Sub drawhero()
    2.  BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.Hero.hDC, 0, 0, vbSrcPaint
    3.  End Sub




    Again this time we have vbSrcPaint which is what we use for painting on top of the mask!

    Please make a note to the fact that the hero mask X and Y position must be exactly the same as the Hero’s X and Y

    And that’s it I hope you have now begin to understand how powerfull BitBlt can be

    I have also made an example in the Zip file below



    ‘Good Luck’

    PINO-

    Please note the image of the 'hero' is not made by me, i found while searching, if you are the owner and do not wish me to use it then please say and i will remove it
    Last edited by Pino; Jun 16th, 2005 at 07:14 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width