Implementing Hit Testing

To add support for hit testing different vector objects:

 

1.

Add the following local variables right under the VECTORPOINT Point; statement in the WndProc function:

 

POINT pt;
VECTOROBJECT Object;
L_TCHAR szBuffer[ 80 ];
VECTORPEN Pen;

 

2.

Add the following lines right after the break; statement of case WM_KEYDOWN:

case WM_MOUSEMOVE:
   // hit test object under mouse cursor
   pt.x = LOWORD( lParam );
   pt.y = HIWORD( lParam );
   nRet = L_VecHitTest( &Vector, &pt, &Object );
   if( nRet == SUCCESS )
   {
      // theres an object, show its type in main window caption
      switch( Object.nType )
      {
         case VECTOR_VERTEX: lstrcpy( szBuffer, TEXT("Vertex ")); break;
         case VECTOR_LINE: lstrcpy( szBuffer, TEXT("Line ")); break;
         case VECTOR_RECTANGLE: lstrcpy( szBuffer, TEXT("Rectangle ")); break;
         case VECTOR_POLYLINE: lstrcpy( szBuffer, TEXT("Polyline")); break;
         case VECTOR_POLYBEZIER: lstrcpy( szBuffer, TEXT("Polybezier")); break;
         case VECTOR_POLYGON: lstrcpy( szBuffer, TEXT("Polygon")); break;
         case VECTOR_ELLIPSE: lstrcpy( szBuffer, TEXT("Ellipse")); break;
         case VECTOR_CIRCLE: lstrcpy( szBuffer, TEXT("Circle")); break;
         case VECTOR_ARC: lstrcpy( szBuffer, TEXT("Arc")); break;
         case VECTOR_TEXT: lstrcpy( szBuffer, TEXT("Text")); break;
         case VECTOR_PIE: lstrcpy( szBuffer, TEXT("Pie")); break;
         case VECTOR_CHORD: lstrcpy( szBuffer, TEXT("Chord")); break;
         case VECTOR_RASTER: lstrcpy( szBuffer, TEXT("Raster ")); break;
      }
   }
   else
      lstrcpy( szBuffer, TEXT("No object under mouse cursor"));

   SetWindowText( hWnd, szBuffer );
    break;

case WM_LBUTTONDOWN:
   // hit test object under mouse cursor
   pt.x = LOWORD( lParam );
   pt.y = HIWORD( lParam );
   nRet = L_VecHitTest ( &Vector, &pt, &Object );
   if( nRet == SUCCESS )
   {
      // theres an object, flip its pen width between 1 and 4
      L_VecGetObjectAttributes ( &Vector, &Object, NULL, &Pen, NULL, NULL );
      Pen.NewPen.LogPen.lopnWidth.x = ( Pen.NewPen.LogPen.lopnWidth.x == 1 ) ? 4 : 1;
      L_VecSetObjectAttributes ( &Vector, &Object, NULL, &Pen, NULL, NULL, 0L );

      // reflect our changes
      InvalidateRect( hWnd, NULL, FALSE );
   }
   break;

case WM_RBUTTONDOWN:
   // hit test object under mouse cursor
   pt.x = LOWORD( lParam );
   pt.y = HIWORD( lParam );
   nRet = L_VecHitTest ( &Vector, &pt, &Object );
   if( nRet == SUCCESS )
   {
      // theres an object, delete it
      L_VecDeleteObject ( &Vector, &Object, 0L );

      // reflect our changes
      InvalidateRect( hWnd, NULL, FALSE );
   }
    break;

 

3.

Compile and run the demo.

4.

Select File/Open… from the program menu. Browse to c:\lead\images\random.dxf and click OK.

5.

You should be able now to see the object type under the mouse cursor on the main window caption. Left click an object to flip its pen width size between 1 and 4. Right click an object to delete it.