Implementing Hit Testing

Start with the project you created in Using the Camera.

Take the following steps to add support for hit testing different vector objects:

1.

Click the "Class View" tab of the project workspace.

2.

Select the CMainFrame() branch.

3.

Right click, and choose "Add Function".

4.

For the return type enter "void".

5.

For the Function name enter "StatusBarDisplay", and add the following parameter (LPCTSTR pszMsg)".

6.

Keep it Public in the Access combo box.

7.

Click Finish button.

8.

Add the following code to the newly created member function StatusBarDisplay():

void CMainFrame::StatusBarDisplay(LPCTSTR pszMsg)
{
   m_wndStatusBar.SetPaneText(0, pszMsg, TRUE);
}

9.

Click to open the "MyVectorWindow" branch in the project workspace.

10.

Double click any of the MyVectorWindow member functions to open the file "MyVectorWindow.cpp".

11.

Add the following two lines after #include "StdAfx.h":

#include "tutorial.h"

#include "MainFrm.h"

extern CTutorialApp theApp;

 

12.

Click the "Class View" tab of the project workspace.

13.

Double click the MsgProcCallBack () member function of the MyVectorWindow class.

14.

Add the following variables local to MsgProcCallBack ():

POINT pt;

L_INT nRet;

LVectorObject Object;

L_TCHAR szBuffer[200];

VECTORPEN Pen;

 

15.

Immediately before the "case WM_CHAR" line, add the following code:

case WM_MOUSEMOVE:
      // hit test object under mouse cursor
      pt.x = LOWORD( lParam );
      pt.y = HIWORD( lParam );
      nRet = HitTest(&pt, &Object );
      if( nRet == SUCCESS )
      {
         // theres an object, show its type in main window caption
         switch( Object.GetType() )
         {
         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") );
            
      ((CMainFrame *)(theApp.GetMainWnd()))->StatusBarDisplay(szBuffer);
      break;
      
   case WM_LBUTTONDOWN:
      // hit test object under mouse cursor
      pt.x = LOWORD( lParam );
      pt.y = HIWORD( lParam );
      nRet = HitTest(&pt, &Object );
      if( nRet == SUCCESS )
      {
         // theres an object, flip its pen width between 1 and 4
         Object.GetObjectAttributes(NULL, &Pen, NULL, NULL );
         Pen.NewPen.LogPen.lopnWidth.x = ( Pen.NewPen.LogPen.lopnWidth.x == 1 ) ? 4 : 1;
         Object.SetObjectAttributes(NULL, &Pen, NULL, NULL);
      }
      break;
      
   case WM_RBUTTONDOWN:
      // hit test object under mouse cursor
      pt.x = LOWORD( lParam );
      pt.y = HIWORD( lParam );
      nRet = HitTest(&pt, &Object );
      if( nRet == SUCCESS )
      {
         // there is an object, delete it
         Object.DeleteObject();
      }
   break;

16.

Compile and run the demo.

17.

From the program menu, browse to the "images" folder of your LEAD installation. Open the image random.dxf and click OK.

18.

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