Adding and Displaying Vector Objects

Start with the project you created in Implementing Hit Testing.

Take the following steps to start adding new layer and objects to the vector handle.

Note:

This tutorial will only use 3 types of objects (Line, Ellipse and Polygon) to keep things simple. For more information about the vector object types supported by the toolkit, refer to the VECTOROBJECT structure.

 

1.

Comment out these local variables from the WndProc function:

static OPENFILENAME OpenFileName;
static L_TCHAR szFileName[ _MAX_PATH ];
static L_TCHAR szFileTitle[ _MAX_PATH ];
static L_TCHAR *szFilter[] = { TEXT("All Files\0*.*\0") };

2.

Comment out these lines from WM_CREATE handler:

// reset the OPENFILENAME structure
memset( &OpenFileName, 0, sizeof( OPENFILENAME ) );
OpenFileName.lStructSize = sizeof( OPENFILENAME );
OpenFileName.hwndOwner = hWnd;
OpenFileName.lpstrFilter = szFilter[ 0 ];
OpenFileName.lpstrFile = szFileName;
OpenFileName.nMaxFile = _MAX_PATH;
OpenFileName.lpstrFileTitle = szFileTitle;
OpenFileName.nMaxFileTitle = _MAX_PATH;

3.

Comment out these 2 cases from the switch( wParam ) statement in WM_COMMAND handle:

case IDM_FILE_OPEN:
case IDM_FILE_SAVE:

4.

Add these local variables under the VECTORPEN Pen; statement in the WndProc function:

VECTORLINE Line;  // line vector object
VECTORELLIPSE Ellipse;  // circle vector object
VECTORPOLYGON Polygon;  // polygon vector object
L_INT i;

5.

Add the following lines in WM_CREATE handler after L_VecAttachToWindow and before ResetView:

// fill in the layer descriptor structure and add new layer to the vector handle
LayerDesc.nSize = sizeof( VECTORLAYERDESC );
lstrcpy( LayerDesc.szName, TEXT("Default Layer"));
LayerDesc.bVisible = TRUE;
LayerDesc.bLocked = FALSE;
LayerDesc.dwTag = 0L;
L_VecAddLayer
 ( &Vector, &LayerDesc, &Layer, 0L );

// make this the active layer in the vector handle
L_VecSetActiveLayer
 ( &Vector, &Layer );

6.

Add the following code to the WM_CHAR handler, after the break; statement of case ‘-‘:

case 'l':
case 'L':
   // add a new vector line
   L_VecInitObject ( &Line.Object );
   Line.Object.nSize = sizeof( VECTORLINE );
   Line.Object.nType = VECTOR_LINE;

   // set the line pen
   Line.Pen.nSize = sizeof( VECTORPEN );
   Line.Pen.LogPen.lopnColor = RGB( rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
   Line.Pen.LogPen.lopnStyle = PS_SOLID;
   Line.Pen.LogPen.lopnWidth.x = 1;
   Line.Pen.LogPen.lopnWidth.y = 1;

   // set the line start and end points
   for( i = 0; i < 2; i++ )
   {
      Line.Point[ i ].x = rand() % 100;
      Line.Point[ i ].y = rand() % 100;
      Line.Point[ i ].z = rand() % 100;
   }

   // add the object to the active layer
   L_VecAddObject ( &Vector, NULL, VECTOR_LINE, &Line, NULL );

   // update view parameters and re-paint window
   ResetView( hWnd, &Vector );
   break;

case 'e':
case 'E':
   // add a new ellipse object
   L_VecInitObject ( &Ellipse.Object );
   Ellipse.Object.nSize = sizeof( VECTORELLIPSE );
   Ellipse.Object.nType = VECTOR_ELLIPSE;

   // set the ellipse pen
   Ellipse.Pen.nSize = sizeof( VECTORPEN );
   Ellipse.Pen.LogPen.lopnColor = RGB( rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
   Ellipse.Pen.LogPen.lopnStyle = PS_SOLID;
   Ellipse.Pen.LogPen.lopnWidth.x = 1;
   Ellipse.Pen.LogPen.lopnWidth.y = 1;

   // set the ellipse brush
   Ellipse.Brush.nSize = sizeof( VECTORBRUSH );
   Ellipse.Brush.LogBrush.lbColor = RGB( rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
   Ellipse.Brush.LogBrush.lbStyle = BS_SOLID;
   Ellipse.Brush.LogBrush.lbHatch = 0;

   // set the ellipse center, x radius and y radius
   Ellipse.Point.x = rand() % 100;
   Ellipse.Point.y = rand() % 100;
   Ellipse.Point.z = rand() % 100;

   Ellipse.xRadius = rand() % 10;
   Ellipse.yRadius = rand() % 10;

   // add the object to the active layer
   L_VecAddObject ( &Vector, NULL, VECTOR_ELLIPSE, &Ellipse, NULL );

   // update view parameters and re-paint window
   ResetView( hWnd, &Vector );
   break;

case 'g':
case 'G':
   // add a new polygon object
   L_VecInitObject ( &Polygon.Object );
   Polygon.Object.nSize = sizeof( VECTORPOLYGON );
   Polygon.Object.nType = VECTOR_POLYGON;

   // set the polygon pen
   Polygon.Pen.nSize = sizeof( VECTORPEN );
   Polygon.Pen.LogPen.lopnColor = RGB( rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
   Polygon.Pen.LogPen.lopnStyle = PS_SOLID;
   Polygon.Pen.LogPen.lopnWidth.x = 1;
   Polygon.Pen.LogPen.lopnWidth.y = 1;

   // set the polygon brush
   Polygon.Brush.nSize = sizeof( VECTORBRUSH );
   Polygon.Brush.LogBrush.lbColor = RGB( rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
   Polygon.Brush.LogBrush.lbStyle = BS_SOLID;
   Polygon.Brush.LogBrush.lbHatch = 0;

   // set the polygon points
   Polygon.nPointCount = rand() % 5 + 3;
   Polygon.Point = (pVECTORPOINT) malloc( Polygon.nPointCount * sizeof( VECTORPOINT ) );
   for( i = 0; i < Polygon.nPointCount; i++ )
   {
      Polygon.Point[ i ].x = rand() % 100;
      Polygon.Point[ i ].y = rand() % 100;
      Polygon.Point[ i ].z = rand() % 100;
   }

   // set polygon fill mode
   Polygon.nPolyFillMode = VECTOR_POLY_ALTERNATE;

   // add the object to the active layer
   L_VecAddObject ( &Vector, NULL, VECTOR_POLYGON, &Polygon, NULL );

   free( Polygon.Point );

   // update view parameters and re-paint window
   ResetView( hWnd, &Vector );
   break;

7.

Compile and run the project. You will be able to add new line objects by pressing L on your keyboard, new ellipse objects by pressing E and new polygon objects by pressing G. You can still perform hit testing and transform your drawing as shown in the previous tutorials.