📄 modparsers.bas
字号:
Attribute VB_Name = "modParsers"
Option Explicit
' Module contains functions that are required by two or more classes.
' No APIs are declared public. This is to prevent possibly, differently
' declared APIs or different versions, of the same API, from conflciting
' with any APIs you declared in your project. Same rule for UDTs.
Private Type SafeArrayBound
cElements As Long
lLbound As Long
End Type
Private Type SafeArray ' used as DMA overlay on a DIB
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
rgSABound(0 To 1) As SafeArrayBound
End Type
Private Type PictDesc
Size As Long
Type As Long
hHandle As Long
hPal As Long
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function VarPtrArray Lib "msvbvm60.dll" Alias "VarPtr" (ByRef Ptr() As Any) As Long
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, ByRef Source As Any, ByVal Length As Long)
' used to create a stdPicture from a byte array
Private Declare Function CreateStreamOnHGlobal Lib "ole32" (ByVal hGlobal As Long, ByVal fDeleteOnRelease As Long, ppstm As Any) As Long
Private Declare Function GlobalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal dwBytes As Long) As Long
Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function GlobalSize Lib "kernel32" (ByVal hMem As Long) As Long
Private Declare Function OleLoadPicture Lib "olepro32" (pStream As Any, ByVal lSize As Long, ByVal fRunmode As Long, riid As Any, ppvObj As Any) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (lpPictDesc As PictDesc, riid As Any, ByVal fPictureOwnsHandle As Long, iPic As IPicture) As Long
' used to see if DLL exported function exists
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
' GDI32 APIs
Private Declare Function CombineRgn Lib "gdi32.dll" (ByVal hDestRgn As Long, ByVal hSrcRgn1 As Long, ByVal hSrcRgn2 As Long, ByVal nCombineMode As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function ExtCreateRegion Lib "gdi32" (lpXform As Any, ByVal nCount As Long, lpRgnData As Any) As Long
Private Declare Function GetRegionData Lib "gdi32.dll" (ByVal hRgn As Long, ByVal dwCount As Long, ByRef lpRgnData As Any) As Long
Private Declare Function GetRgnBox Lib "gdi32.dll" (ByVal hRgn As Long, ByRef lpRect As RECT) As Long
Private Declare Function CreateRectRgn Lib "gdi32.dll" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
' User32 APIs
Private Declare Function SetRect Lib "user32" (lpRect As RECT, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
' Kernel32/User32 APIs for Unicode Filename Support
Private Declare Function CreateFileW Lib "kernel32" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As Long, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function DeleteFileW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
Private Declare Function DeleteFile Lib "kernel32.dll" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Declare Function SetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long, ByVal dwFileAttributes As Long) As Long
Private Declare Function SetFileAttributes Lib "kernel32.dll" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Private Declare Function GetFileAttributesW Lib "kernel32.dll" (ByVal lpFileName As Long) As Long
Private Declare Function GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function GetDesktopWindow Lib "user32.dll" () As Long
Private Declare Function IsWindowUnicode Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Const FILE_ATTRIBUTE_NORMAL = &H80&
Public Function iparseCreateShapedRegion(cHost As c32bppDIB, regionStyle As eRegionStyles) As Long
'*******************************************************
' FUNCTION RETURNS A HANDLE TO A REGION IF SUCCESSFUL.
' If unsuccessful, function retuns zero.
' The fastest region from bitmap routines around, custom
' designed by LaVolpe. This version modified to create
' regions from alpha masks.
'*******************************************************
' Note: See c32bppDIB.CreateRegion for description of the regionStyle parameter
' declare bunch of variables...
Dim rgnRects() As RECT ' array of rectangles comprising region
Dim rectCount As Long ' number of rectangles & used to increment above array
Dim rStart As Long ' pixel that begins a new regional rectangle
Dim X As Long, Y As Long, Z As Long ' loop counters
Dim bDib() As Byte ' the DIB bit array
Dim tSA As SafeArray ' array overlay
Dim rtnRegion As Long ' region handle returned by this function
Dim Width As Long, Height As Long
Dim lScanWidth As Long ' used to size the DIB bit array
' Simple sanity checks
If cHost.Alpha = False Then
iparseCreateShapedRegion = CreateRectRgn(0&, 0&, cHost.Width, cHost.Height)
Exit Function
End If
Width = cHost.Width
If Width < 1& Then Exit Function
Height = cHost.Height
If Height < 1& Then Exit Function
On Error GoTo CleanUp
lScanWidth = Width * 4& ' how many bytes per bitmap line?
With tSA ' prepare array overlay
.cbElements = 1 ' byte elements
.cDims = 2 ' two dim array
.pvData = cHost.BitsPointer ' data location
.rgSABound(0).cElements = Height
.rgSABound(1).cElements = lScanWidth
End With
' overlay now
CopyMemory ByVal VarPtrArray(bDib()), VarPtr(tSA), 4&
If regionStyle = regionShaped Then
ReDim rgnRects(0 To Width * 3&) ' start with an arbritray number of rectangles
' begin pixel by pixel comparisons
For Y = Height - 1 To 0& Step -1&
' the alpha byte is every 4th byte
For X = 3& To lScanWidth - 1& Step 4&
' test to see if next pixel is 100% transparent
If bDib(X, Y) = 0 Then
If Not rStart = 0& Then ' we're currently tracking a rectangle,
' so let's close it, but see if array needs to be resized
If rectCount + 1& = UBound(rgnRects) Then _
ReDim Preserve rgnRects(0 To UBound(rgnRects) + Width * 3&)
' add the rectangle to our array
SetRect rgnRects(rectCount + 2&), rStart \ 4, Height - Y - 1&, X \ 4 + 1&, Height - Y
rStart = 0& ' reset flag
rectCount = rectCount + 1& ' keep track of nr in use
End If
Else
' non-transparent, ensure start value set
If rStart = 0& Then rStart = X ' set start point
End If
Next X
If Not rStart = 0& Then
' got to end of bitmap without hitting another transparent pixel
' but we're tracking so we'll close rectangle now
' see if array needs to be resized
If rectCount + 1& = UBound(rgnRects) Then _
ReDim Preserve rgnRects(0 To UBound(rgnRects) + Width * 3&)
' add the rectangle to our array
SetRect rgnRects(rectCount + 2&), rStart \ 4, Height - Y - 1&, Width, Height - Y
rStart = 0& ' reset flag
rectCount = rectCount + 1& ' keep track of nr in use
End If
Next Y
ElseIf regionStyle = regionEnclosed Then
ReDim rgnRects(0 To Width * 3&) ' start with an arbritray number of rectangles
' begin pixel by pixel comparisons
For Y = Height - 1 To 0& Step -1&
' the alpha byte is every 4th byte
For X = 3& To lScanWidth - 1& Step 4&
' test to see if next pixel has any opaqueness
If Not bDib(X, Y) = 0 Then
' we got the left side of the scan line, check the right side
For Z = lScanWidth - 1 To X + 4& Step -4&
' when we hit a non-transparent pixel, exit loop
If Not bDib(Z, Y) = 0 Then Exit For
Next
' see if array needs to be resized
If rectCount + 1& = UBound(rgnRects) Then _
ReDim Preserve rgnRects(0 To UBound(rgnRects) + Width * 3&)
' add the rectangle to our array
SetRect rgnRects(rectCount + 2&), X \ 4, Height - Y - 1&, Z \ 4 + 1&, Height - Y
rectCount = rectCount + 1& ' keep track of nr in use
Exit For
End If
Next X
Next Y
ElseIf regionStyle = regionBounds Then
ReDim rgnRects(0 To 0) ' we will only have 1 regional rectangle
' set the min,max bounding parameters
SetRect rgnRects(0), Width * 4, Height, 0, 0
With rgnRects(0)
' begin pixel by pixel comparisons
For Y = Height - 1 To 0& Step -1&
' the alpha byte is every 4th byte
For X = 3& To lScanWidth - 1& Step 4&
' test to see if next pixel has any opaqueness
If Not bDib(X, Y) = 0 Then
' we got the left side of the scan line, check the right side
For Z = lScanWidth - 1 To X + 4& Step -4&
' when we hit a non-transparent pixel, exit loop
If Not bDib(Z, Y) = 0 Then Exit For
Next
rStart = 1& ' flag indicating we have opaqueness on this line
' resize our bounding rectangle's left/right as needed
If X < .Left Then .Left = X
If Z > .Right Then .Right = Z
Exit For
End If
Next X
If rStart = 1& Then
' resize our bounding rectangle's top/bottom as needed
If Y < .Top Then .Top = Y
If Y > .Bottom Then .Bottom = Y
rStart = 0& ' reset flag indicating we do not have any opaque pixels
End If
Next Y
End With
If rgnRects(0).Right > rgnRects(0).Left Then
rtnRegion = CreateRectRgn(rgnRects(0).Left \ 4, Height - rgnRects(0).Bottom - 1&, rgnRects(0).Right \ 4 + 1&, _
(rgnRects(0).Bottom - rgnRects(0).Top) + (Height - rgnRects(0).Bottom))
End If
End If
' remove the array overlay
CopyMemory ByVal VarPtrArray(bDib()), 0&, 4&
On Error Resume Next
' check for failure & engage backup plan if needed
If Not rectCount = 0 Then
' there were rectangles identified, try to create the region in one step
rtnRegion = CreatePartialRegion(rgnRects(), 2&, rectCount + 1&, 0&, Width)
' ok, now to test whether or not we are good to go...
' if less than 2000 rectangles, region should have been created & if it didn't
' it wasn't due O/S restrictions -- failure
If rtnRegion = 0& Then
If rectCount > 2000& Then
' Win98 has limitation of approximately 4000 regional rectangles
' In cases of failure, we will create the region in steps of
' 2000 vs trying to create the region in one step
rtnRegion = CreateWin98Region(rgnRects, rectCount + 1&, 0&, Width)
End If
End If
End If
CleanUp:
Erase rgnRects()
If Err Then ' failure; probably low on resources
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -