Start with the project you created in Using the Camera.
Take the following steps to add support for hit testing different vector objects:
Click the "Class View" tab of the project workspace.
Select the CMainFrame() branch.
Right click, and choose "Add Function".
For the return type enter "void".
For the Function name enter "StatusBarDisplay", add the following parameter (LPCTSTR pszMsg).
Keep it Public in the Access combo box.
Click Finish button.
Add the following code to the newly created member function StatusBarDisplay():
void CMainFrame::StatusBarDisplay(LPCTSTR pszMsg)
{
m_wndStatusBar.SetPaneText(0, pszMsg, TRUE);
}
Click to open the "MyVectorWindow" branch in the project workspace.
Double click any of the MyVectorWindow member functions to open the file "MyVectorWindow.cpp".
Add the following two lines after #include "StdAfx.h":
#include "tutorial.h"
#include "MainFrm.h"
extern CTutorialApp theApp;
Click the "Class View" tab of the project workspace.
Double click the MsgProcCallBack() member function of the MyVectorWindow class.
Add the following variables local to MsgProcCallBack():
POINT pt;
L_INT nRet;
LVectorObject Object;
L_TCHAR szBuffer[200];
VECTORPEN Pen;
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;
Compile and run the demo.
From the program menu, browse to the "images" folder of your LEAD installation. Open the image random.dxf and click OK.
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.