



Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors As RGBQUAD
End Type
Private Const DIB_RGB_COLORS = 0&
Private Const BI_RGB = 0&
Private Const pixR As Integer = 3
Private Const pixG As Integer = 2
Private Const pixB As Integer = 1
Private 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
Private Type BITMAPINFOHEADER '40 bytes
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
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Declare Function CreateRectRgn Lib "gdi32" (ByVal x1 As Long, ByVal y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
Private Declare Function CombineRgn Lib "gdi32" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
' DIB stuff.
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function GetDIBits Lib "gdi32" (ByVal aHDC As Long, ByVal hBitmap As Long, ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, ByVal wUsage As Long) As Long
' Fir the form to the pixels that don't match the color.
Private Sub ShapeForm(ByVal pic As PictureBox, ByVal transparent_color As Long)
Const RGN_OR = 2
Dim bytes_per_scanLine As Integer
Dim wid As Long
Dim hgt As Long
Dim bitmap_info As BITMAPINFO
Dim pixels() As Byte
Dim buffer() As Byte
Dim transparent_r As Byte
Dim transparent_g As Byte
Dim transparent_b As Byte
Dim border_width As Single
Dim title_height As Single
Dim x0 As Long
Dim y0 As Long
Dim start_c As Integer
Dim stop_c As Integer
Dim R As Integer
Dim C As Integer
Dim combined_rgn As Long
Dim new_rgn As Long
ScaleMode = vbPixels
pic.ScaleMode = vbPixels
pic.AutoRedraw = True
pic.Picture = pic.Image
' Prepare the bitmap description.
wid = pic.ScaleWidth
hgt = pic.ScaleHeight
With bitmap_info.bmiHeader
.biSize = 40
.biWidth = wid
' Use negative height to scan top-down.
.biHeight = -hgt
.biPlanes = 1
.biBitCount = 32
.biCompression = BI_RGB
bytes_per_scanLine = ((((.biWidth * .biBitCount) + 31) \ 32) * 4)
.biSizeImage = bytes_per_scanLine * hgt
End With
' Load the bitmap's data.
ReDim pixels(1 To 4, 1 To wid, 1 To hgt)
GetDIBits pic.hDC, pic.Image, _
0, hgt, pixels(1, 1, 1), _
bitmap_info, DIB_RGB_COLORS
' Process the pixels.
' Break the tansparent color apart.
UnRGB transparent_color, transparent_r, transparent_g, transparent_b
' Find the form's corner.
border_width = (ScaleX(Width, vbTwips, vbPixels) - ScaleWidth) / 2
title_height = ScaleX(Height, vbTwips, vbPixels) - border_width - ScaleHeight
' Find the picture's corner.
x0 = pic.Left + border_width
y0 = pic.Top + title_height
' Create the form's regions.
For R = 1 To hgt
' Create a region for this row.
C = 1
Do While C <= wid
start_c = 1
stop_c = 1
' Find the next non-white column.
Do While C <= wid
If pixels(pixR, C, R) <> transparent_r Or _
pixels(pixG, C, R) <> transparent_g Or _
pixels(pixB, C, R) <> transparent_b _
Then
Exit Do
End If
C = C + 1
Loop
start_c = C
' Find the next white column.
Do While C <= wid
If pixels(pixR, C, R) = transparent_r And _
pixels(pixG, C, R) = transparent_g And _
pixels(pixB, C, R) = transparent_b _
Then
Exit Do
End If
C = C + 1
Loop
stop_c = C
' Make a region from start_c to stop_c.
If start_c <= wid Then
If stop_c > wid Then stop_c = wid
' Create the region.
new_rgn = CreateRectRgn( _
start_c + x0, R + y0, _
stop_c + x0, R + y0 + 1)
' Add it to what we have so far.
If combined_rgn = 0 Then
combined_rgn = new_rgn
Else
CombineRgn combined_rgn, _
combined_rgn, new_rgn, RGN_OR
DeleteObject new_rgn
End If
End If
Loop
Next R
' Restrict the form to the region.
SetWindowRgn hWnd, combined_rgn, True
DeleteObject combined_rgn
End Sub
'Usage:
' Fit the form to the picture.
ShapeForm picShape, &HFFFFFF
Сейчас этот форум просматривают: Google-бот, Yandex-бот и гости: 1