7,762
社区成员
发帖
与我相关
我的任务
分享
Private Sub Command1_Click()
Dim tBitMapInfo As tpBitMapInfo256
Dim tPixels() As Byte
Dim tBuffer() As Byte
Dim tIndex As Long
Dim tGDIMs As Long
ReDim tBuffer(511)
tBitMapInfo = BitMapCreate()
With tBitMapInfo.bmiHeader
ReDim tPixels(.biWidth * .biHeight - 1)
End With
picWaveScreen.Line (0, 0)-(512, 255)
For tIndex = 0 To 511
tBuffer(tIndex) = 127 + Int(Rnd * (Sin(tIndex * 20 * 3.14 / 180) * Sin((tIndex + Timer * 100) * 3.14 / 180) * 64))
Next
PixelsGetByBuffer tBuffer(), tPixels(), 255, 512
BitMapShow picWaveScreen.hDC, tBitMapInfo, tPixels()
End Sub
Private Sub Form_Load()
Form1.Show
End Sub
Option Explicit
Public Type tpBitMapFileHeader
bfType As Integer
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Public Type tpBitMapInfoHeader
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
Public Type tpRGBQuad
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Public Type tpBitMapHeader
bhFileHeader As tpBitMapFileHeader
bhInfoHeader As tpBitMapInfoHeader
End Type
Public Type tpBitMapInfo256
bmiHeader As tpBitMapInfoHeader
bmiColors(255) As tpRGBQuad
End Type
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal dx As Long, ByVal dy As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal wSrcWidth As Long, ByVal wSrcHeight As Long, lpBits As Any, lpBitsInfo As tpBitMapInfo256, ByVal wUsage As Long, ByVal dwRop As Long) As Long
Public Const DIB_PAL_COLORS = 1
Public Const DIB_RGB_COLORS = 0
Public Const SRCCOPY = &HCC0020
Public Sub PixelsGetByBuffer(ByRef pBuffer() As Byte, ByRef pPixel() As Byte, ByVal pColor As Byte, Optional ByVal pW As Long = 512)
Dim tBuffer_Index As Long, tBuffer_Length As Long
tBuffer_Length = UBound(pBuffer())
For tBuffer_Index = 1 To tBuffer_Length
PixelsDrawRow pPixel(), pColor, pW, tBuffer_Index, pBuffer(tBuffer_Index - 1), pBuffer(tBuffer_Index)
Next
End Sub
Public Sub PixelsDrawRow(ByRef pPixels() As Byte, ByVal pColor As Byte, ByVal pW As Long, ByVal pX As Long, ByVal pYs As Long, ByVal pYe As Long)
Dim tY As Long, tI As Long
If Not pYe = pYs Then
For tY = pYs To pYe Step Sgn(pYe - pYs)
tI = tY * pW + pX
pPixels(tI) = pColor
Next
Else
tI = pYs * pW + pX
pPixels(tI) = pColor
End If
End Sub
Public Function BitMapCreate(Optional ByVal pWidth As Long = 512, Optional ByVal pHeight As Long = 256) As tpBitMapInfo256
Dim tBitMapInfo As tpBitMapInfo256
Dim tIndex As Long
With tBitMapInfo
For tIndex = 0 To 255
.bmiColors(tIndex).rgbBlue = 0
.bmiColors(tIndex).rgbGreen = tIndex
.bmiColors(tIndex).rgbRed = 0
Next
End With
With tBitMapInfo.bmiHeader
.biBitCount = 8
.biClrImportant = 0
.biClrUsed = 0
.biCompression = 0
.biHeight = pHeight
.biPlanes = 1
.biSize = 40
.biSizeImage = 0
.biWidth = pWidth
.biXPelsPerMeter = 0
.biYPelsPerMeter = 0
End With
BitMapCreate = tBitMapInfo
End Function
Public Function BitMapShow(ByVal pDC As Long, ByRef pBitMapInfo As tpBitMapInfo256, ByRef pPixels() As Byte)
With pBitMapInfo.bmiHeader
BitMapShow = StretchDIBits(pDC, 0, 0, .biWidth, .biHeight, 0, 0, .biWidth, .biHeight, pPixels(0), pBitMapInfo, DIB_RGB_COLORS, SRCCOPY)
End With
End Function