Rubrik: Grafik & Zeichnen | 22.03.05 |
CreateCompatibleBitmap-Funktion Diese Funktion erstellt ein Bitmap, das zu einem angegeben Device kompatibel ist. | ||
Betriebssystem: Win95, Win98, WinNT 3.1, Win2000, WinME | Views: 10.652 |
Deklaration:
Declare Function CreateCompatibleBitmap Lib "gdi32" ( _ ByVal hdc As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long) As Long
Beschreibung:
Diese Funktion erstellt ein Bitmap, das zu einem angegeben Device kompatibel ist.
Parameter:
hdc | Erwartet den Devicekontext, zu dem das Bitmapkompatibel sein soll. |
nWidth | Erwartet die Breite, die das Bitmap besitzen soll. |
nHeight | Erwartet die Höhe, die das Bitmap besitzen soll. |
Rückgabewert:
Ist die Funktion erfolgreich, so liefert sie das Handle zu dem kompatiblen Bitmap, andernfallswird "0" zurückgegeben. Unter Windows NT, 2000 und XP können Sie die GetLastError-Funktion für erweiterte Fehlerinformationenaufrufen.
Beispiel:
Private Declare Function CreateCompatibleDC Lib "gdi32" ( _ ByVal hdc As Long) As Long Private Declare Function CreateBitmap Lib "gdi32" ( _ ByVal nWidth As Long, _ ByVal nHeight As Long, _ ByVal nPlanes As Long, _ ByVal nBitCount As Long, _ lpBits As Any) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32" ( _ ByVal hdc As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long) As Long Private Declare Function SelectObject Lib "gdi32" ( _ ByVal hdc As Long, _ ByVal hObject As Long) As Long Private Declare Function SetMapMode Lib "gdi32" ( _ ByVal hdc As Long, _ ByVal nMapMode As Long) As Long Private Declare Function GetMapMode Lib "gdi32" (ByVal hdc As Long) As Long 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 Declare Function SetBkColor Lib "gdi32" ( _ ByVal hdc As Long, _ ByVal crColor As Long) As Long Private Declare Function GetBkColor Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function DeleteObject Lib "gdi32" ( _ ByVal hObject As Long) As Long ' BitBlt dwRop-Konstantem Private Const SRCCOPY = &HCC0020 Private Const NOTSRCCOPY = &H330008 Private Const SRCAND = &H8800C6 Private Const SRCPAINT = &HEE0086
' Transparente Farbe wählen Private Sub Command2_Click() On Error GoTo Err_Dlg With Common1 .CancelError = True .ShowColor SetBkColor Picture3.hdc, .Color End With End Sub
' Transparente Farbe anhand der Koordinate ermitteln Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As _ Single, Y As Single) Picture3.BackColor = Picture1.Point(X, Y) End Sub
' Startet das Kopieren der Maske oder des Bitmaps mit einer transparenten Farbe Private Sub Command1_Click() DrawTransparent Picture1, Picture2, GetBkColor(Picture3.hdc), Option1.Value End Sub
' Transparente Bitmaps kopieren (CopyBitmap = False um nur die Maske zu kopieren) Private Function DrawTransparent(ByVal Src As PictureBox, ByVal Dest As _ PictureBox, ByVal TransColor As Long, ByVal CopyBitmap As Boolean) Dim hdcBack As Long, hdcObject As Long, hdcMem As Long, hdcSave As Long Dim bmAndBack As Long, bmAndObject As Long, bmAndMem As Long, bmSave _ As Long Dim bmBackOld As Long, bmObjectOld As Long, bmMemOld As Long, _ bmSaveOld As Long Dim OldColor As Long ' Zielbitmap löschen Dest.Cls ' Einige temporäre Devicekontexte erstellen hdcBack = CreateCompatibleDC(Dest.hdc) hdcObject = CreateCompatibleDC(Dest.hdc) hdcMem = CreateCompatibleDC(Dest.hdc) hdcSave = CreateCompatibleDC(Dest.hdc) ' 2 Schwarzweißbitmaps erstellen bmAndBack = CreateBitmap(Dest.ScaleWidth, Dest.ScaleHeight, 1, 1, _ ByVal 0&) bmAndObject = CreateBitmap(Dest.ScaleWidth, Dest.ScaleHeight, 1, 1, _ ByVal 0&) ' 2 Normale Bitmaps erstellen bmAndMem = CreateCompatibleBitmap(Src.hdc, Dest.ScaleWidth, Dest.ScaleHeight) bmSave = CreateCompatibleBitmap(Src.hdc, Dest.ScaleWidth, Dest.ScaleHeight) ' Die Bitmap den Devices zuweisen SelectObject hdcBack, bmAndBack SelectObject hdcObject, bmAndObject SelectObject hdcMem, bmAndMem SelectObject hdcSave, bmSave ' Mapmode des Ziels einstellen SetMapMode Src.hdc, GetMapMode(Dest.hdc) ' Sicherheitskopie des Originals anlegen BitBlt hdcSave, 0, 0, Src.ScaleWidth, Src.ScaleHeight, Src.hdc, 0, 0, SRCCOPY ' Hintergrund des Originalbitmaps mit der transparenten Farbe füllen OldColor = SetBkColor(Src.hdc, TransColor) ' Schwarzweißkopie (Objektmaske) des Bitmaps erstellen BitBlt hdcObject, 0, 0, Src.ScaleWidth, Src.ScaleHeight, Src.hdc, 0, _ 0, SRCCOPY ' Original Hintergrundfarbe wiederherstellen SetBkColor Src.hdc, OldColor ' Schwarzweißkopie (Objektmaske) invertieren BitBlt hdcBack, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcObject, 0, _ 0, NOTSRCCOPY If CopyBitmap = False Then ' nur die Maske kopieren ' Hintergrund des Quellbildes kopieren BitBlt hdcMem, 0, 0, Src.ScaleWidth, Src.ScaleHeight, Src.hdc, 0, _ 0, SRCCOPY ' Maskieren des Bereiches, in dem das Bitmap platziert werden soll BitBlt hdcMem, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcObject, _ 0, 0, SRCAND ' Maskieren der transparenten Farbe des Ziels BitBlt Dest.hdc, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcBack, _ 0, 0, SRCAND ' XOR Operation mit dem Hintergrund des Ziels durchführen BitBlt hdcMem, 0, 0, Src.ScaleWidth, Src.ScaleHeight, Dest.hdc, _ 0, 0, SRCPAINT Else ' Bitmap Kopieren ' Hintergrund des Ziels kopieren BitBlt hdcMem, 0, 0, Dest.ScaleWidth, Dest.ScaleHeight, Dest.hdc, _ 0, 0, SRCCOPY ' Maskieren des Bereiches, in dem das Bitmap platziert werden soll BitBlt hdcMem, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcObject, _ 0, 0, SRCAND ' Maskieren der transparenten Farbe BitBlt Src.hdc, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcBack, _ 0, 0, SRCAND ' XOR Operation mit dem Hintergrund der Quelle durchführen BitBlt hdcMem, 0, 0, Src.ScaleWidth, Src.ScaleHeight, Src.hdc, 0, _ 0, SRCPAINT End If ' Kopieren des Bitmaps in das Ziel BitBlt Dest.hdc, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcMem, 0, 0, SRCCOPY ' Sicherheitskopie des Originals wiederherstellen BitBlt Src.hdc, 0, 0, Src.ScaleWidth, Src.ScaleHeight, hdcSave, 0, 0, SRCCOPY ' Temporäre Bitmaps zerstören DeleteObject SelectObject(hdcBack, bmBackOld) DeleteObject SelectObject(hdcObject, bmObjectOld) DeleteObject SelectObject(hdcMem, bmMemOld) DeleteObject SelectObject(hdcSave, bmSaveOld) ' Erstellte Devices zerstören DeleteDC hdcMem DeleteDC hdcBack DeleteDC hdcObject DeleteDC hdcSave DeleteDC hdcTemp End Function