Heute stellen wir Ihnen eine Funktion vor, mit der sich ein Bild anhand einer frei definierbaren Farben transparent darstellen lässt. Für die Ausgabe des transparenten Bildes wird ein Control mit einer hDC-Eigenschaft benötigt (bspw. Form oder PictureBox). Fügen Sie nachfolgenden Code in ein Modul ein: Option Explicit ' benötigte API-Deklarationen Private Declare Function GdiTransparentBlt Lib "gdi32.dll" ( _ ByVal hDC 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 nSrcWidth As Long, _ ByVal nSrcHeight As Long, _ ByVal crTransparent As Long) As Boolean Private Declare Function GetObject Lib "gdi32" _ Alias "GetObjectA" ( _ ByVal hObject As Long, _ ByVal nCount As Long, _ lpObject As Any) As Long Private Declare Function GetDesktopWindow Lib "user32" () As Long Private Declare Function GetDC Lib "user32.dll" ( _ ByVal hWnd As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32.dll" ( _ ByVal hDC As Long) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" ( _ ByVal hDC As Long, _ ByVal nWidth As Long, _ ByVal nHeight As Long) As Long Private Declare Function SelectObject Lib "gdi32.dll" ( _ ByVal hDC As Long, _ ByVal hObject As Long) As Long Private Declare Function DrawState Lib "user32" _ Alias "DrawStateA" ( _ ByVal hDC As Long, _ ByVal hBrush As Long, _ ByVal lpDrawStateProc As Long, _ ByVal lData As Any, _ ByVal wData As Long, _ ByVal x As Long, _ ByVal y As Long, _ ByVal cx As Long, _ ByVal cy As Long, _ ByVal fuFlags As Long) As Long Private Declare Function DeleteObject Lib "gdi32.dll" ( _ ByVal hObject As Long) As Long Private Declare Function DeleteDC Lib "gdi32.dll" ( _ ByVal hDC As Long) As Long Private Declare Function ReleaseDC Lib "user32.dll" ( _ ByVal hWnd As Long, ByVal hDC As Long) As Long ' Bitmap-Inforamtionen Private Type Bitmap bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Private Const DST_BITMAP = &H4 ' Erstellt ein transparentes Bitmap und zeichnet dieses ' in den angegebenen Device-Context ' ' Optionale Parameter: x, y ' = Ausgabeposition (linkere obere Ecke) Private Function DrawTransparentBmp(ByVal hDC As Long, ByVal hBitmap As Long, _ ByVal TransColor As Long, Optional ByVal x As Long = 0, Optional ByVal y = 0) Dim bmp As Bitmap Dim DeskDC As Long Dim TempDC As Long Dim TmpBmp As Long Dim OldBmp As Long ' Bitmap-Struktur ermitteln Call GetObject(hBitmap, Len(bmp), bmp) ' Device-Context erstellen DeskDC = GetDC(GetDesktopWindow) TempDC = CreateCompatibleDC(DeskDC) ' Kompatibles Bitmap erstellen TmpBmp = CreateCompatibleBitmap(DeskDC, bmp.bmWidth, bmp.bmHeight) OldBmp = SelectObject(TempDC, TmpBmp) ' Bitmap zeichnen Call DrawState(TempDC, 0, 0, hBitmap, 0, 0, 0, 0, 0, DST_BITMAP) ' Transaprenz erzeugen GdiTransparentBlt hDC, x, y, bmp.bmWidth, bmp.bmHeight, TempDC, 0, 0, _ bmp.bmWidth, bmp.bmHeight, TransColor ' Objekte freigeben DeleteObject SelectObject(TempDC, OldBmp) DeleteDC TempDC ReleaseDC GetDesktopWindow, DeskDC End Function Aufrufbeispiel: ' Bild mit "Magenta" als transparente Farbe anzeigen DrawTransparentBmp Picture1.hDC, LoadPicture("d:\bild1.bmp"), vbMagenta Dieser Tipp wurde bereits 10.273 mal aufgerufen. Voriger Tipp | Zufälliger Tipp | Nächster Tipp
Anzeige
Diesen und auch alle anderen Tipps & Tricks finden Sie auch auf unserer aktuellen vb@rchiv Vol.6 (einschl. Beispielprojekt!) Ein absolutes Muss - Geballtes Wissen aus mehr als 8 Jahren vb@rchiv! - nahezu alle Tipps & Tricks und Workshops mit Beispielprojekten - Symbol-Galerie mit mehr als 3.200 Icons im modernen Look Weitere Infos - 4 Entwickler-Vollversionen (u.a. sevFTP für .NET), Online-Update-Funktion u.v.m. |
vb@rchiv CD Vol.6 Geballtes Wissen aus mehr als 8 Jahren vb@rchiv! Online-Update-Funktion Entwickler-Vollversionen u.v.m. Tipp des Monats April 2024 Skyfloy Chart von Microsoft und dazu noch gratis Tutorial für Microsoft Chart Controls für Microsoft .NET Framework 3.5 Neu! sevEingabe 3.0 Einfach stark! Ein einziges Eingabe-Control für alle benötigten Eingabetypen und -formate, inkl. Kalender-, Taschenrechner und Floskelfunktion, mehrspaltige ComboBox mit DB-Anbindung, ImageComboBox u.v.m. |
||||||||||||||||
Microsoft, Windows und Visual Basic sind entweder eingetragene Marken oder Marken der Microsoft Corporation in den USA und/oder anderen Ländern. Weitere auf dieser Homepage aufgeführten Produkt- und Firmennamen können geschützte Marken ihrer jeweiligen Inhaber sein. |