# 请教safeaarray 与varptr的使用

Posted by ching on July 20, 1999 at 10:54:41:

以下程式实在体会不出来，请帮忙，谢谢
程式码如下：
Private Type SAFEARRAYBOUND
cElements As Long
lLbound As Long
End Type

Private Type SAFEARRAY1D
cDims As Integer
fFeatures As Integer
cbElements As Long
cLocks As Long
pvData As Long
Bounds(0 To 0) As SAFEARRAYBOUND
End TypePrivate Declare Function VarPtrArray Lib "msvbvm50.dll" Alias "VarPtr" (Ptr() As Any) As Long
﹝部份宣告省略﹞
Sub DoFlame()
' these are used to address the pixel using matrices
Dim pict() As Byte
Dim sa As SAFEARRAY1D, bmp As BITMAP
Dim r As Integer, c As Integer, value As Byte
' get bitmap info
GetObjectAPI Pictbox.Picture, Len(bmp), bmp
' exit if not a supported bitmap
If bmp.bmPlanes <> 1 Or bmp.bmBitsPixel <> 8 Then
MsgBox " 256-color bitmaps only", vbCritical
Exit Sub
End If
' have the local matrix point to bitmap pixels
With sa
.cbElements = 1
.cDims = 2
.Bounds(0).lLbound = 0
.Bounds(0).cElements = bmp.bmHeight
.Bounds(1).lLbound = 0
.Bounds(1).cElements = bmp.bmWidthBytes
.pvData = bmp.bmBits
End With
CopyMemory ByVal VarPtrArray(pict), VarPtr(sa), 4
' swap colors - note that col/row order is inverted
' because VB arrays are stored in column-wise order
For c = 0 To UBound(pict, 1)
pict(c, 0) = Int(Rnd(1) * 200) + 55
Next
For c = 1 To UBound(pict, 1) - 1
For r = 1 To UBound(pict, 2)
pict(c, r) = div3_lookup2(pict(c, r - 1)) + div3_lookup1(pict(c - 1, r - 1)) + div3_lookup3(pict(c + 1, r - 1))
Next
Next
' clear the temporary array descriptor
' without destroying the local temporary array
CopyMemory ByVal VarPtrArray(pict), 0&, 4
' inform VB that something has changed
Pictbox.Refresh

End Sub

Follow Ups: