c32bppdib.cls
来自「AeroSuite--一组非常漂亮的VISTA控件集」· CLS 代码 · 共 1,364 行 · 第 1/5 页
CLS
1,364 行
End Property
Public Property Get Width() As Long
Width = m_Width ' width of image in pixels
End Property
Public Property Get Height() As Long
Height = m_Height ' height of image in pixels
End Property
Public Property Get BitsPointer() As Long
BitsPointer = m_Pointer ' pointer to the bits of the image
End Property
Public Property Get scanWidth() As Long
scanWidth = m_Width * 4& ' number of bytes per scan line
End Property
Public Property Get Handle() As Long
Handle = m_Handle ' the picture handle of the image
End Property
Public Function LoadDIBinDC(ByVal bLoad As Boolean) As Long
' Purpose: Select/Unselect the DIB into a DC.
' Returns the DC handle when image is loaded
' Called by image parser if it needs to paint the image into the DIB
If bLoad = True Then
Dim tDC As Long
If Not m_Handle = 0& Then ' do we have an image?
If m_hDC = 0& Then ' do we have a DC?
tDC = GetDC(0&) ' if not create one
m_hDC = CreateCompatibleDC(tDC)
ReleaseDC 0&, tDC
End If
If m_prevObj = 0& Then
m_prevObj = SelectObject(m_hDC, m_Handle)
End If
LoadDIBinDC = m_hDC
End If
Else
If Not m_prevObj = 0& Then
SelectObject m_hDC, m_prevObj
If m_ManageDC = False Then
DeleteObject m_hDC
m_hDC = 0&
End If
m_prevObj = 0&
End If
End If
End Function
Public Property Let ManageOwnDC(bManage As Boolean)
' Determines whether or not this class will manage its own DC
' If false, then a DC is created each time the image needs to be Rendered
Dim tDC As Long
If bManage = False Then ' removing management of DC
If Not m_hDC = 0& Then ' DC does exist, destroy it
' first remove the dib, if one exists
If Not m_Handle = 0& Then SelectObject m_hDC, m_prevObj
m_prevObj = 0&
End If
DeleteDC m_hDC
m_hDC = 0&
Else ' allowing creation of dc
If m_hDC = 0& Then ' create DC only if we have a dib to put in it
If Not m_Handle = 0& Then
tDC = GetDC(0&)
m_hDC = CreateCompatibleDC(tDC)
ReleaseDC 0&, tDC
End If
End If
End If
m_ManageDC = bManage
End Property
Public Property Get ManageOwnDC() As Boolean
ManageOwnDC = m_ManageDC
End Property
Public Property Get isAlphaBlendFriendly() As Boolean
isAlphaBlendFriendly = ((m_osCAP And 1) = 1)
' WinNT4 & below and Win95 are not shipped with msimg32.dll (AlphaBlend API)
' Win98 has bugs & would believe that WinME is buggy too but don't know for sure
' Therefore, the Rendering in this class will not use AlphaBlend on these
' operating systems even if the DLL exists, but will use GDI+ if available
' Can be overridden by setting this property to True
End Property
Public Property Let isAlphaBlendFriendly(Enabled As Boolean)
' This has been provided to override safety of using AlphaBlend on Win9x systems.
' Caution. Only set this when rendering to a known device dependent bitmap (DDB)
' Alphablend can crash when rendering DIB to DIB vs DIB to DDB. Be warned.
If Enabled = True Then
' Overriding in play: allow AlphaBlend if system is Win98 or better
' By default this is already set for Win2K or better
If ((m_osCAP And 8) = 8) Then m_osCAP = m_osCAP Or 1
Else
m_osCAP = m_osCAP And Not 1 ' disallow AlphaBlend
End If
End Property
Public Property Get isGDIplusEnabled() As Boolean
' identifies if GDI+ is usable on the system.
' Before this property is set, GDI+ is tested to ensure it is usable
isGDIplusEnabled = ((m_osCAP And 2) = 2)
End Property
Public Property Let isGDIplusEnabled(Enabled As Boolean)
' Sets the property. If set to False by you, GDI+ will not be used
' for any rendering, but still may be used to create PNG files if needed
If Not Enabled = Me.isGDIplusEnabled Then
m_osCAP = (m_osCAP And Not 2)
If Enabled Then
If (m_osCAP And 32) = 0 Then ' else Win95, NT4 SP5 or lower
Dim cGDIp As New cGDIPlus
If cGDIp.isGDIplusOk() = True Then m_osCAP = m_osCAP Or 2
End If
End If
End If
End Property
Public Property Get isZlibEnabled() As Boolean
' Read Only
' To create PNG files, GDI+ or zLib is required. This property informs
' you if zLIB exists in the system's DLL path
isZlibEnabled = iparseValidateZLIB(vbNullString, 0, False, False, True)
End Property
Public Function InitializeDIB(ByVal Width As Long, ByVal Height As Long) As Boolean
' Creates a blank (all black, all transparent) DIB of requested height & width
Dim tBMPI As BITMAPINFO, tDC As Long
DestroyDIB ' clear any pre-existing dib
If Width < 0& Then Exit Function
If Height = 0& Then
Exit Function
ElseIf Height < 0& Then
Height = Abs(Height) ' no top-down dibs
End If
On Error Resume Next
With tBMPI.bmiHeader
.biBitCount = 32
.biHeight = Height
.biWidth = Width
.biPlanes = 1
.biSize = 40&
.biSizeImage = .biHeight * .biWidth * 4&
End With
If Err Then
Err.Clear
' only possible error would be that Width*Height*4& is absolutely huge
Exit Function
End If
tDC = GetDC(0&) ' get screen DC
m_Handle = CreateDIBSection(tDC, tBMPI, 0&, m_Pointer, 0&, 0&)
If m_ManageDC = True Then
' create a DC if class is managing its own & one isn't created yet
If m_hDC = 0& Then m_hDC = CreateCompatibleDC(tDC)
End If
' release the screen DC if we captured it
ReleaseDC 0&, tDC
If Not m_Handle = 0& Then ' let's hope system resources allowed DIB creation
m_Width = Width
m_Height = Height
m_AlphaImage = True
m_Format = imgNone
InitializeDIB = True
End If
End Function
Public Sub DestroyDIB()
' PURPOSE: Destroy any existing image
If Not m_hDC = 0& Then ' do we have a DC?
' do we have an image; if so get it out of the DC
If Not m_prevObj = 0& Then SelectObject m_hDC, m_prevObj
' destroy our DC, no point in keeping it w/o image
DeleteObject m_hDC
m_hDC = 0&
End If
' if we do have an image, destroy it now
If Not m_Handle = 0& Then
DeleteObject m_Handle
Erase m_ImageByteCache
End If
' reset other image attributes
m_Width = 0&
m_Height = 0&
m_Handle = 0&
m_Pointer = 0&
m_prevObj = 0&
m_AlphaImage = False
m_Format = imgError
End Sub
Public Sub EraseDIB()
' Function clears out an existing DIB, making it 100% transparent/black
If Not m_Handle = 0& Then
FillMemory ByVal m_Pointer, m_Width * m_Height * 4&, 0
m_Format = imgNone
m_AlphaImage = True
End If
End Sub
Public Function Render(ByVal destinationDC As Long, _
Optional ByVal destX As Long, Optional ByVal destY As Long, _
Optional ByVal destWidth As Long, Optional ByVal destHeight As Long, _
Optional ByVal SrcX As Long, Optional ByVal SrcY As Long, _
Optional ByVal srcWidth As Long, Optional ByVal srcHeight As Long, _
Optional ByVal Opacity As Long = 100&, _
Optional ByVal Blend As Boolean = True, _
Optional ByVal SetHalfTone As Boolean = True, _
Optional ByRef destHostDIB As c32bppDIB = Nothing, _
Optional ByVal grayScale As eGrayScaleFormulas = gsclNone, _
Optional ByVal LightAdjustment As Single = 0!, _
Optional ByVal Angle As Single = 0!, _
Optional ByVal CenterOnDestXY As Boolean = False) As Boolean
' PURPOSE: Render an existing 32bpp DIB to a target DC
' Angle & CenterOnDestXY added to allow this routine to replace the RotateAtCenterPoint & RotateAtTopLeft calls
' Mirroring: When destWidth & srcWidth are compared, if one is negative and the other positive, mirroring horizontally occurs
' When destHeight & srcHeight are compared, if one is negative and the other positive, mirroring vertically occurs
' All four of those parameters are optional & any that are not passed will default to the image's width/height as needed
' before the mirroring check is applied
' Parameters. Only destinationDC is required
' destinationDC :: target DC to draw to. Ignored if destHostDIB is passed
' destX, destY :: the top/left coordinates to draw to, default is 0,0
' destWidth, destHeight :: the width and height to draw to, default is the image's width & height
' srcX, srcY :: the left & top offset within the DIB
' srcWidth, srcHeight :: the amount of DIB to be rendered
' Opacity :: how opaque to draw the image, default is 100% opaque
' Blend :: no longer used, reserved & left in for backward compatibility
' SetHalfTone :: if True, then the destination DC's stretch mode will be modified to
' produce better quality results. This option is not available on Win9x systems.
' Tip: When AlphaBlending to another DIB set to False
' When AlphaBlending to CompatibleBitmap (DDB) or visible DC set to True
' destHostDIB :: When rendering from DIB class to DIB class, pass the destination
' DIB class to ensure alpha blending occurs correctly on systems that do not
' support GDI+ or AlphaBlend APIs. When passed, destinationDC is ignored
' grayscale :: one of several formulas to grayscale while rendering (optional)
' LightAdjustment :: values between -100 and 100 percent of added pixel darkeness/lightness
' -100% will display a black image & 100 percent will display a white image
' Angle :: between -360 and 360. Rotation is clockwise
' CenterOnDestXY :: If true then rendering is centered on the destX,destY coordinates
Dim lBlendFunc As Long, tDC As Long, hOldImage As Long
Dim lStretchMode As Long
Dim aResizedBytes() As Byte, aMirrorBytes() As Byte
Dim bStretching As Boolean
Dim bMirroring As Boolean
Dim bRotating As Boolean
Dim bCanUseAlphaBlend As Boolean
' validate a few things
If Opacity < 1& Then ' nothing to render if image is 100% transparent
Render = Not (m_Handle = 0)
Exit Function
ElseIf m_Handle = 0& Then
Exit Function
ElseIf destinationDC = 0& Then
If destHostDIB Is Nothing Then Exit Function
End If
' validate optional destination parameters
If destWidth = 0&
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?