Adding and Displaying Vector Objects
To add a new layer and objects to the vector handle:
Start with the project you created in Implementing Hit Testing.
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_OPEN:
case IDM_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 ‘B‘: |
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.NewPen.LogPen.lopnColor = RGB( rand() % 246 + 10,
rand() % 246 + 10, rand() % 246 + 10 );
Line.Pen.NewPen.LogPen.lopnStyle = PS_SOLID;
Line.Pen.NewPen.LogPen.lopnWidth.x = 1;
Line.Pen.NewPen.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.NewPen.LogPen.lopnColor = RGB( rand() % 246 +
10, rand() % 246 + 10, rand() % 246 + 10 );
Ellipse.Pen.NewPen.LogPen.lopnStyle = PS_SOLID;
Ellipse.Pen.NewPen.LogPen.lopnWidth.x = 1;
Ellipse.Pen.NewPen.LogPen.lopnWidth.y = 1;
// set the ellipse brush
Ellipse.Brush.nSize = sizeof( VECTORBRUSH );
Ellipse.Brush.BrushType.StandardBrush.LogBrush.lbColor = RGB(
rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
Ellipse.Brush.BrushType.StandardBrush.LogBrush.lbStyle = BS_SOLID;
Ellipse.Brush.BrushType.StandardBrush.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.NewPen.LogPen.lopnColor = RGB( rand() % 246 +
10, rand() % 246 + 10, rand() % 246 + 10 );
Polygon.Pen.NewPen.LogPen.lopnStyle = PS_SOLID;
Polygon.Pen.NewPen.LogPen.lopnWidth.x = 1;
Polygon.Pen.NewPen.LogPen.lopnWidth.y = 1;
// set the polygon brush
Polygon.Brush.nSize = sizeof( VECTORBRUSH );
Polygon.Brush.BrushType.StandardBrush.LogBrush.lbColor = RGB(
rand() % 246 + 10, rand() % 246 + 10, rand() % 246 + 10 );
Polygon.Brush.BrushType.StandardBrush.LogBrush.lbStyle = BS_SOLID;
Polygon.Brush.BrushType.StandardBrush.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. |