Version 2.0!
Features
Tutorials
Files
Glossary
Projects
Contact
Links
Message Board
Extras
LuckyCam
Old News
Sign Guestbook
View Guestbook
VB Horoscope
VB Photo Album
.
ATTENTION READERS! Lucky's VB Gaming Site is no longer active. For updated game programming information and tutorials, please visit The Game Programming Wiki!

Transparante Blits zonder Masks - door Brian Clark (Translated by Pieterjan Vandegaer van Eyesight Development)

Dit is de eerste tutorial die ik ooit heb geschreven, dus wees vrij om commentaar te sturen naar webtroll@aol.com.

Download de bron code hier: TransBlit.zip

Opmerking: Dit API-extensie DLL zit standaard in Win98 en hoger, maar het kan GEBRUIKT worden in Win95. Download gewoon deze DLL en zet het in je windows\system map.

Stap 1. Statements Declareren

Ok. Het eerste ding wat je zal moeten doen is om de volgende statements in een nieuwe MODULE plaatsen.

Declare Function TransparentBlt Lib "msimg32" (ByVal hdcDest As Long, _
ByVal nXOriginDest As Long, ByVal nYOriginDest As Long, ByVal nWidthDest _
As Long, ByVal nHeightDest As Long, ByVal hdcSrc As Long, ByVal nXOriginSrc _
As Long, ByVal nYOriginSrc As Long, ByVal nWidthSrc As Long, ByVal _
nHeightSrc As Long, ByVal crTransparent As Long) As Long

Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal X As Long, _
ByVal Y As Long) As Long

Step 2. Een Form Maken

1. Nu moeten we een form aan ons project toevoegen, frmBlt genoemd, en 2 pictureboxes, de eerste noem je picSource de tweede picDest.
2. Voeg 3 command knoppen toe aan je pagina(form), met de namen: cmdBlit, cmdExit en cmdLoadBitmaps(Enkel als je de 2de optie hieronder gebruikt).
3. Je zult ook 3 variabelen moeten declareren in het declaratie gedeelte van je form:

Dim hDCSrc as Long
Dim hdcWidth as Integer
Dim hdcHeight as Integer

Het zou er nu zoals dit moeten uitzien:

Step 3. Je bitmaps laden

Nadat je je form gemaakt hebt, zal je een bitmap moeten laden in een hDC, dit kan gedaan worden op 2 manieren: via een PictureBox of door de API aan te roepen.

Step 4. Een bitmap in een hDC laden(met een Picturebox)

Je kan je bron-bitmap in je picturebox laden als je je form aan het maken bent(via het properties venster) of je kan het in de code zetten door de LoadPicture functie te gebruiken. Ik hoop dat iedereen weet hoe je een prent kan inladen via het properties venster!!! Dus zal ik mij toespitsen op het gebruik van de LoadPicture functie. Zet deze code in de cmdLoadBitmap knop:


    picSource.Picture = LoadPicture("Pad naar de bitmap")
    picSource.Refresh
    hDCSrc = picSource.hDC
    GetDimensions '(Dit is voor later)

Dat was nu toch niet zo moeilijk?

Step 4.5. Een bitmap in een hDC laden(via de API)

Deze manier is een beetje moeilijker maar over verloop van tijd is het veel nuttiger!!

1. Je zal nog 3 andere declaraties in je Module moeten toevoegen:

Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal _
hObject As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long

2. Zet deze code in de cmdLoadBitmaps knop:


    hdcSrc = CreateCompatibleDC(frmBlt.hdc)
    SelectObject hdcSrc, LoadPicture("Path to Bitmap")
    GetDimensions

3. Zet wat code in de Form_Unload procedure:


    DeleteDC hdcSrc

Step 5. De GetDimensions Functie

1. Zet deze 'Type' in je Module:


Public Type Bitmap '14 bytes
    bmType As Long
    bmWidth As Long   
    bmHeight As Long
    bmWidthBytes As Long
    bmPlanes As Integer
    bmBitsPixel As Integer
    bmBits As Long
End Type

Public Const LR_CREATEDIBSECTION = &H2000
Public Const LR_LOADFROMFILE = &H10

En nog wat declaraties:


Public Declare Function LoadImage Lib "user32" (ByVal hInst As Long, ByVal lpsz _
As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As _
Long) As Long

Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

2. Zet deze code in de declaratie sectie:


Sub GetDimensions()

Dim dbitmap As Bitmap
Dim hbitmap As Long
Dim filename As String

    filename = "pad naar je bitmap"
    hbitmap = LoadImage(ByVal 0&, filename, 0, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)
	
    ' Bitmap information opvragen
    GetObject hbitmap, Len(dbitmap), dbitmap
    hdcHeight = dbitmap.bmHeight
    hdcWidth = dbitmap.bmWidth
    DeleteObject hbitmap
	
End Sub

Step 6. De Transparante Blit Zonder Masks

1. Zet de eigenlijke code in de cmdBlit knop:


'Neemt de kleur van de pixel op 0,0 van je bitmap
Dim TransparentColor As Long

    TransparentColor = GetPixel(hdcSrc, 0, 0)
    TransparentBlt picDest.hdc, 0, 0, hdcWidth, _
    hdcHeight, hdcSrc, 0, 0, hdcWidth, _ 
    hdcHeight, TransparentColor

Step 7. Om te eindigen

1. Kuis je code op door deze code bij het Form_Unload event te zetten:


    DeleteDC hdcSrc

2. Vergeet niet de code voor de EXIT knop toe te voegen:


    Unload Me

PROFICIAT, JE BENT KLAAR!