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
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...
Public 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
Ok dont be put off bye the way it looks, it looks ALOT harder than it actually is!
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
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....
Private Sub cmdDrawLand_click()
drawLand ' goes to the ‘drawland’ sub
In the module…
Public Sub drawLand()
BitBlt frmBitBlt.hDC, 50, 50, 300, 300, frmBitBlt.Land.hDC, 0, 0, vbSrcCopy
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
Private Sub cmdDrawmask_click()
drawMask ' this will go to the ‘drawMask’ sub in the module
In the module….
Public Sub drawMask()
BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.HeroMask.hDC, 0, 0, vbSrcAnd
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....
Private Sub cmdDrawHero_click()
drawHero ' this will go to the ‘drawhero’ sub in the module
In the module….
Public Sub drawhero()
BitBlt frmBitBlt.hDC, 50, 50, 30, 30, frmBitBlt.Hero.hDC, 0, 0, vbSrcPaint
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
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