![]() |
Tipp 0295
|
Surface mit Bitmap aus Ressource erstellen
|
 |
|
Autor/Einsender: Datum: |
|
Alexander Csadek 17.12.2002 |
|
Entwicklungsumgebung:
DirectX-Version: |
|
VB 6
DirectX 7 |
|
|
DirectDraw stellt zwar eine Funktion CreateSurfaceFromResource zur Verfügung, diese funktioniert aber leider nicht. Um dieses Problem zu umgehen, können die Bitmaps auch manuell aus der Ressource geladen werden. Hierbei muss man aber beachten, dass die Bitmaps als "Benutzerdefinierte Ressource" in die Ressource geladen wird und nicht als Bitmap. In der Ressource ist das die Kategorie "CUSTOM".
|
Die DirectDraw-Surface wird mit der Funktion CreateSurface erstellt. Mittels der Funktion
LoadResData von VB wird das Bitmap ausgelesen. Da das Bitmap dann nur als Byte-Ansammlung zur Verfügung steht, ist es am einfachsten, wenn man die Informationen zum Bitmap manuell setzt. Diese Informationen werden zum Kopieren des Bitmaps in die
Surface Surface benötigt.
|
Das Kopieren des Bitmaps aus der Byte-Ansammlung in die DirectDraw-Surface übernimmt die API-Funktion
StretchDIBits. Wobei hier zu beachten ist, dass die ersten 54 Bytes vom Bitmap nicht kopiert werden. In diesen befindet sich der Bitmap-Header mit den Bitmap-Informationen.
|
Folgender Code zeigt, wie man manuell ein 24-Bit-Bitmap aus der Ressource in eine
DirectDraw-Surface laden kann.
|
Der abgebildete Code bezieht sich auf die wichtigsten Funktionen zum Laden einer 24-Bit-Bitmaps aus der Ressource in eine
DirectDraw-Surface. Da u.a. der Code für die Initialisierung von
DirectX meistens gleich bleibt, wird dieser hier nicht mehr gesondert abgebildet. Das Download-Beispiel enthält jedoch das komplette Projekt.
|
|
Code im Codebereich des Moduls |
|
|
Option Explicit
Public Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Public Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Public Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors(0 To 255) As RGBQUAD
End Type
Public Declare Function StretchDIBits Lib "gdi32" ( _
ByVal hdc As Long, ByVal X As Long, ByVal Y As Long, _
ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, _
ByVal SrcY As Long, ByVal wSrcWidth As Long, _
ByVal wSrcHeight As Long, lpBits As Any, _
lpBitsInfo As BITMAPINFO, ByVal wUsage As Long, _
ByVal dwRop As Long) As Long
Public Const SRCCOPY = &HCC0020
Public Const DIB_RGB_COLORS = 0
|
|
|
Code im Codebereich der Form |
|
|
Option Explicit
Sub BitmapLaden()
Dim BmpDesc As DDSURFACEDESC2
BmpDesc.lFlags = DDSD_CAPS Or DDSD_HEIGHT Or DDSD_WIDTH
BmpDesc.ddsCaps.lCaps = DDSCAPS_OFFSCREENPLAIN
BmpDesc.lWidth = 73
BmpDesc.lHeight = 85
Set bmpBild1 = DD7.CreateSurface(BmpDesc)
LoadBitmapFromRes 101, 73, 85, bmpBild1
BmpDesc.lWidth = 218
BmpDesc.lHeight = 36
Set bmpBild2 = DD7.CreateSurface(BmpDesc)
LoadBitmapFromRes 102, 218, 36, bmpBild2
End Sub
Sub LoadBitmapFromRes(ByVal ResID As Long, ByVal w As Long, _
ByVal h As Long, ByRef SurfBitmap As DirectDrawSurface7)
Dim lngTemp As Long
Dim BMPData() As Byte
Dim WidthHelper As Long
Dim BMPInfo As BITMAPINFO
BMPData = LoadResData(ResID, "CUSTOM")
WidthHelper = (w * 3) + (w Mod 4)
With BMPInfo.bmiHeader
.biBitCount = 24
.biClrImportant = 0
.biClrUsed = 0
.biCompression = 0
.biHeight = h
.biPlanes = 1
.biSize = 40
.biSizeImage = (WidthHelper * h)
.biWidth = w
.biXPelsPerMeter = 3780
.biYPelsPerMeter = 3780
End With
lngTemp = SurfBitmap.GetDC
StretchDIBits lngTemp, 0, 0, w, h, 0, 0, w, h, BMPData(54), _
BMPInfo, DIB_RGB_COLORS, SRCCOPY
SurfBitmap.ReleaseDC lngTemp
End Sub
|
|
|
|
|
|
Um dieses Beispiel ausführen zu können, wird die DirectX 7
for Visual Basic Type Library
benötigt (siehe dazu die Erläuterungen in der DirectX-Rubrik).
|
|
Windows-Version |
95 |
 |
|
98/SE |
 |
|
ME |
 |
|
NT |
 |
|
2000 |
 |
|
XP |
 |
|
Vista |
 |
|
Win
7 |
 |
|
|
VB-Version |
VBA 5 |
 |
|
VBA 6 |
 |
|
VB 4/16 |
 |
|
VB 4/32 |
 |
|
VB 5 |
 |
|
VB 6 |
 |
|
|
|
Download (27,5
kB)
|
Downloads bisher: [ 892 ]
|
|
|