函数调用问题
有一个画波形函数,如下所示,在调用的时候直接写DrawWavesPC ,程序可以通过。
Public Sub DrawWavesPC()
Dim x As Long ' current X position
Dim leftYOffset As Long ' Y offset for left channel graph
Dim rightYOffset As Long ' Y offset for right channel graph
Dim curLeftY As Long ' current left channel Y value
Dim curRightY As Long ' current right channel Y value
Dim lastX As Long ' last X position
Dim lastLeftY As Long ' last left channel Y value
Dim lastRightY As Long ' last right channel Y value
Dim maxAmplitude As Long ' the maximum amplitude for a wavegraph on the form
Dim leftVol As Double ' buffer for retrieving the left volume level
Dim rightVol As Double ' buffer for retrieving the right volume level
Dim scaleFactor As Double ' samples per pixel on the wave graph
Dim xStep As Double ' pixels per sample on the wave graph
Dim curSample As Long ' current sample number
Dim H As Integer
Dim W As Integer
H = Int(Me.VoicePC.ScaleHeight / 2)
W = Int(Me.VoicePC.ScaleWidth / 2)
' clear the screen
VoicePC.Cls
' if no file is loaded, don 't try to draw graph
If (ModuleWave.fFileLoaded = False) Then
Exit Sub
End If
' calculate drawing parameters
scaleFactor = (ModuleWave.drawTo - ModuleWave.drawFrom) / VoicePC.Width
If (scaleFactor < 1) Then
xStep = 1 / scaleFactor
Else
xStep = 1
End If
' Draw the graph
maxAmplitude = VoicePC.Height / 2
leftYOffset = maxAmplitude
For x = 0 To VoicePC.Width Step xStep
curSample = scaleFactor * x + ModuleWave.drawFrom
If (ModuleWave.format.wBitsPerSample = 16) Then
GetMono16Sample curSample, leftVol
Else
GetMono8Sample curSample, leftVol
End If
curLeftY = CLng(leftVol * maxAmplitude)
VoicePC.PSet (lastX, curLeftY + H)
VoicePC.Line (lastX, curLeftY + H)-(lastX, lastLeftY + H)
lastLeftY = curLeftY
lastX = x
Next
End Sub
但是由于此函数有多处需要调用,故想将此函数改为有参数的形式,如下:
将VoicePC改为参数picbox
Public Sub DrawWaves(picbox As PictureBox)
Dim x As Long ' current X position
Dim leftYOffset As Long ' Y offset for left channel graph
Dim rightYOffset As Long ' Y offset for right channel graph
Dim curLeftY As Long ' current left channel Y value
Dim curRightY As Long ' current right channel Y value
Dim lastX As Long ' last X position
Dim lastLeftY As Long ' last left channel Y value
Dim lastRightY As Long ' last right channel Y value
Dim maxAmplitude As Long ' the maximum amplitude for a wavegraph on the form
Dim leftVol As Double ' buffer for retrieving the left volume level
Dim rightVol As Double ' buffer for retrieving the right volume level
Dim scaleFactor As Double ' samples per pixel on the wave graph
Dim xStep As Double ' pixels per sample on the wave graph
Dim curSample As Long ' current sample number
Dim H As Integer
Dim W As Integer
H = Int(picbox.ScaleHeight / 2)
W = Int(picbox.ScaleWidth / 2)
' clear the screen
picbox.Cls
' if no file is loaded, don 't try to draw graph
If (ModuleWave.fFileLoaded = False) Then
Exit Sub
End If
' calculate drawing parameters
scaleFactor = (ModuleWave.drawTo - ModuleWave.drawFrom) / picbox.width
If (scaleFactor < 1) Then
xStep = 1 / scaleFactor
Else
xStep = 1
End If
' Draw the graph
maxAmplitude = picbox.height / 2
leftYOffset = maxAmplitude
For x = 0 To picbox.width Step xStep
curSample = scaleFactor * x + ModuleWave.drawFrom
If (ModuleWave.format.wBitsPerSample = 16) Then
GetMono16Sample curSample, leftVol
Else
GetMono8Sample curSample, leftVol
End If
curLeftY = CLng(leftVol * maxAmplitude)
picbox.PSet (lastX, curLeftY + H)
picbox.Line (lastX, curLeftY + H)-(lastX, lastLeftY + H)
lastLeftY = curLeftY
lastX = x
Next
End Sub
然后在调用的时候写 DrawWaves (VoicePC),程序报错object required ,有谁知道这是怎么回事吗?
[解决办法]
检查picbox传入是不是Nothing
[解决办法]
估计VoicePC跟调用DrawWaves (VoicePC)的过程不在同一个Form中,这个时候要这样调用[比如该Form叫Frm1]DrawWaves(Frm1.VoicePC)
[解决办法]
同意楼上两位
需要检查传入的是否是nothing,调用的时候写 call DrawWaves(Form1.VoicePC)
[解决办法]
不是因为
估计VoicePC跟调用DrawWaves (VoicePC)的过程不在同一个Form中,这个时候要这样调用[比如该Form叫Frm1]DrawWaves(Frm1.VoicePC)
原因的,以为是传地址发式,传入VoicePC ,所以没有FRM1也不会错的
我想你的原因是DrawWaves (VoicePC)语句前面没有CALL 或者多了一对括号
