Step by step procedures for creating the BD/Ex2 example

  1. Make sure you have register the ActiveCDX.OCX, see install.htm for more information on this. 
  2. Create a new application. 
  3. Insert the CDXVisual and CDXDraw Control, rename them to CDXVisual and CDXDraw. 
  4. Put 5 buttons on the form, name them "Stop", "Line", "Rect", "Circle", and "Exit". Set their captions to be equal with their names.
  5. Still in the form, put a Timer control and set the interval to 10 ms. 
  6. Add the procedure of CDXError to the TForm1 (the form class):
        procedure TForm1.CDXError(msg:PChar);
        begin
            MessageBox(0, msg, 'CDX Error', MB_OK);
            CDXVisual.Destroy;
            Application.Terminate;
        end;
    This procedure will be called when there is an error and will terminate the application.
  7. Add a variable to the TForm1 called Demo. This variable will indicate which shape to draw. The possible values are 0 (none), 1 (line), 2 (rect) and 3 (fillcircle). 
  8. Like the previous example, add these 2 procedures to initiate and destroy the CDXVisual object:
        procedure TForm1.FormCreate(Sender: TObject);
        begin
            Randomize;
            if not CDXVisual.CreateWindowed(Handle, 640, 480) then
            CDXError('Failed creating windowed CDX');
            Demo:=0;
        end;
        procedure TForm1.FormDestroy(Sender: TObject);
        begin
            CDXVisual.Destroy;
        end;
  9. Add these 5 procedures to handle the 5 buttons you have created before:
        procedure TForm1.StopClick(Sender: TObject);
        begin
            Demo:=0;
            CDXVisual.Fill(CDXVisual.GetBack, 0);
        end;
        procedure TForm1.LineClick(Sender: TObject);
        begin
            Demo:=1;
            CDXVisual.Fill(CDXVisual.GetBack, 0);
        end;
        procedure TForm1.RectClick(Sender: TObject);
        begin
            Demo:=2;
            CDXVisual.Fill(CDXVisual.GetBack, 0);
        end;
        procedure TForm1.CircleClick(Sender: TObject);
        begin
            Demo:=3;
            CDXVisual.Fill(CDXVisual.GetBack, 0);
        end;
        procedure TForm1.ExitClick(Sender: TObject);
        begin
            Application.Terminate;
        end;
    The first 4 procedures 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. 
  10. Double click the timer and add this code:
        procedure TForm1.Timer1Timer(Sender: TObject);
        var BackBuffer:longint;
              x1, y1, x2, y2, radius, color:longint;
        begin
            {Exit if no button selected}
            if demo <> 0 then
            begin
                {Get and lock the backbuffer}
                BackBuffer := CDXVisual.GetBack;
                CDXVisual.Lock (BackBuffer);

                {Generate random position and color}
                x1 := Random(640);
                y1 := Random(480);
                x2 := Random(640);
                y2 := Random(480);
                radius := Random(50);
                color := Random(60000);

                {Draw something}
                Case Demo of
                    1 : CDXDraw.Line2(BackBuffer, x1, y1, x2, y2, color);
                    2 : CDXDraw.Rect(BackBuffer, x1, y1, x2, y2, color);
                    3 : CDXDraw.FillCircle(BackBuffer, x1, y1, radius, color);
                End;

                {Unlock and flip}
                CDXVisual.Unlock (BackBuffer);
                CDXVisual.Flip;
            end
        end;
    The code will first get the back buffer, lock it, draw something, unlock it and flip.