Events for Visual Basic 6 COM interoperability |
|
As mentioned inside the tutorial How to synchronize the container application with the API, Visual Basic 6 results unreliable when dealing with delegates and callbacks in general so, when using the API in conjunctions with this development environment, a set of COM compatible events is provided.
In order to enable the usage of the provided events you need to invoke the COMEventEnable method, for each type of event you are going to manage, immediately after initializing the API through the InitEditor method, then you need to manually enter the event management routine.
Inside the snippet below you can see an example of management of events generated by the editor through the OnPercentage event handler when a sound file is loaded and through the OnVuMeterValueChange event when VU Meter values are changed; the text of a label is modified when monitored events are reported and a progress bar is managed as well to inform about operations advancement:
Visual Basic 6 |
' declare the API Public WithEvents m_SoundEditorApi As AudioSoundEditorApi.AudioSoundEditorApiObj
Private Sub m_SoundEditorApi_OnPercentage(ByVal nOperation As enumOperationsWithPercentage, ByVal nPercentage As Integer) If ProgressBar1.Value = nPercentage Then ' no change Exit Sub End If ProgressBar1.Visible = True ProgressBar1.Value = nPercentage
If nOperation = enumOperationsWithPercentage_OPERATION_SOUND_LOADING Then LabelStatus.Caption = "Status: Loading... " & nPercentage & "%" ElseIf nOperation = enumOperationsWithPercentage_OPERATION_WAVE_ANALYSIS Then LabelStatus.Caption = "Status: Analyzing waveform... " & nPercentage & "%" End If
ProgressBar1.Refresh LabelStatus.Refresh End Sub
Private Sub m_SoundEditorApi_OnVUMeterValueChange(ByVal nPeakLeft As Integer, ByVal nPeakRight As Integer) ' draw VU-Meter's bands using the graphic bars m_SoundEditorApi.GraphicBarsManagerGet().SetValue m_hWndVuMeterLeft, nPeakLeft m_SoundEditorApi.GraphicBarsManagerGet().SetValue m_hWndVuMeterRight, nPeakRight End Sub
Private Sub Form_Load() ' instance the API Set m_SoundEditorApi = New AudioSoundEditorApi.AudioSoundEditorApiObj
' init the control m_SoundEditorApi.InitEditor
' enable events percentage of advancement events and VU Meter events for COM clients like VB6 m_SoundEditorApi.COMEventEnable enumEventTypesEdt_EVENT_TYPE_EDT_PERC m_SoundEditorApi.COMEventEnable enumEventTypesEdt_EVENT_TYPE_EDT_VU_METER
LabelStatus.Caption = "Status: Idle" ProgressBar1.Visible = False
' load the new song m_SoundEditorApi.SetLoadingMode enumLoadingModes_LOAD_MODE_NEW Dim nResult As enumErrorCodes nResult = m_SoundEditorApi.LoadSound(strPathname) If nResult <> enumErrorCodes_ERR_NOERROR Then MsgBox "Cannot load the file due to the following error: " & nResult Exit Sub End If
ProgressBar1.Value = 0
' request full waveform analysis using default resolution m_SoundEditorApi.DisplayWaveformAnalyzerGet().AnalyzeFullSound
ProgressBar1.Visible = False LabelStatus.Caption = "Status: Idle"
End Sub
|
as seen for regular ActiveX controls, the name of the event management routine is made of a number of parts: the name of the component (in this case "m_SoundEditorApi" which was originally declared with the "WithEvents" attribute) followed by the underscore "_" character and by the name of the event (in this case "OnPercentage") and related parameters.