Step by step procedures for creating the VB/Ex2 example
- Make sure you have register the ActiveCDX.OCX, see install.htm for more information on this.
- Create a new project, select Standard Exe
- Insert the ActiveCDX component.
In the toolbox, right click, select components, check the ActiveCDX ActiveX Control module, or you can browse and select the file manually.
The succees of this step is indicated when you see 3 new components appeared in the toolbox. They are CDXVisual, CDXDraw and
CDXAudio.
- Put the CDXVisual and CDXDraw on the form and name it as CDXVisual and
CDXDraw respectively.
- Put a button on the form, change the name into "Button". When
the button selected, press Ctrl-C then Ctrl-V. This will create a control
array. Make 3 more buttons, and set the caption of the buttons to
"Stop", "Line", "Rect" and "Circle"
in order of the button index.
- Still in the form, put a Timer control and set the interval to 10 ms.
- Add the subroutine of CDX_Error in the general section:
Private Sub CDX_Error(msg As String)
Rem There is an error, destroy cdx and quit
CDXVisual.Destroy
MsgBox msg
Unload Me
End Sub
This subroutine will be called when there is an error and will terminate the application.
- Declare a variable in general section:
Dim Demo As Integer
This variable, Demo will indicate which shape to draw. The possible values
are 0 (none), 1 (line), 2 (rect) and 3 (fillcircle).
- Like the previous example, add this 2 subroutine to initiate and destroy
the CDXVisual object.
Private Sub Form_Load()
Randomize
Rem Create the windowed CDX
If Not CDXVisual.CreateWindowed(Form1.hWnd, 640, 480) Then
CDX_Error ("Failed creating windowed CDX")
End If
Demo = 0
End Sub
Private Sub Form_Unload(Cancel As Integer)
CDXVisual.Destroy
End Sub
- Double click one of the buttons in the array you have created in step 5 and add the code:
Private Sub Button_Click(Index As Integer)
Demo = Index
Call CDXVisual.Fill(CDXVisual.GetBack, 0)
End Sub
The code will set the demo to a value according to the button that the user
select. The Fill command will erase the backbuffer by filling it with black
pixels.
- Double click the timer and add this code:
Private Sub Timer1_Timer()
Rem Exit if no button selected
If Demo = 0 Then Exit Sub
Dim BackBuffer As Long
Dim x1, y1, x2, y2, radius, color As Long
Rem Get and lock the backbuffer
BackBuffer = CDXVisual.GetBack
CDXVisual.Lock (BackBuffer)
Rem Generate random position and color
x1 = Int(640 * Rnd)
y1 = Int(480 * Rnd)
x2 = Int(640 * Rnd)
y2 = Int(480 * Rnd)
radius = Int(50 * Rnd)
color = Int((60000 * Rnd) + 1)
Rem Draw something
Select Case Demo
Case 1
Call CDXDraw.Line2(BackBuffer, x1, y1, x2, y2, color)
Case 2
Call CDXDraw.Rect(BackBuffer, x1, y1, x2, y2, color)
Case 3
Call CDXDraw.FillCircle(BackBuffer, x1, y1, radius, color)
End Select
Rem Unlock and flip
CDXVisual.Unlock (BackBuffer)
CDXVisual.Flip
End Sub
The code will first get the back buffer, lock it, draw something, unlock it
and flip.