📄 hotspvw.cpp
字号:
// hotspvw.cpp : implementation of the CHotspot3View class
//
#include "stdafx.h"
#include "hotspot3.h"
#include "hotspdoc.h"
#include "hotspvw.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHotspot3View
IMPLEMENT_DYNCREATE(CHotspot3View, CView)
BEGIN_MESSAGE_MAP(CHotspot3View, CView)
//{{AFX_MSG_MAP(CHotspot3View)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_MODE_TEST, OnModeTest)
ON_COMMAND(ID_MODE_DEFINE, OnModeDefine)
ON_UPDATE_COMMAND_UI(ID_MODE_TEST, OnUpdateModeTest)
ON_COMMAND(ID_NEW_REGION, OnNewRegion)
ON_UPDATE_COMMAND_UI(ID_NEW_REGION, OnUpdateNewRegion)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHotspot3View construction/destruction
CHotspot3View::CHotspot3View()
{
}
CHotspot3View::~CHotspot3View()
{
}
/////////////////////////////////////////////////////////////////////////////
// CHotspot3View drawing
void CHotspot3View::OnDraw(CDC* pDC)
{
CHotspot3Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
for (int i = 0; i < pDoc->m_numPoints-1; i++)
DrawLine(pDoc->m_points[i], pDoc->m_points[i+1]);
DrawLine( pDoc->m_points[i], pDoc->m_points[0]);
}
/////////////////////////////////////////////////////////////////////////////
// CHotspot3View diagnostics
#ifdef _DEBUG
void CHotspot3View::AssertValid() const
{
CView::AssertValid();
}
void CHotspot3View::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CHotspot3Doc* CHotspot3View::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHotspot3Doc)));
return (CHotspot3Doc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHotspot3View message handlers
void CHotspot3View::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
}
void CHotspot3View::OnRButtonDown(UINT nFlags, CPoint point)
{
SetCapture();
}
void CHotspot3View::OnLButtonUp(UINT nFlags, CPoint point)
{
ReleaseCapture();
CHotspot3Doc* pDoc = GetDocument();
//If we're defining...
if (pDoc->m_testing == FALSE)
{
//If not drawing, then we are placing first point.
if (pDoc->m_drawing == FALSE)
if (pDoc->m_numPoints == 0)
pDoc->m_drawing = TRUE;
else
//Polygon is complete - display message.
{
MessageBox("Polygon closed - use Test mode to try it out.");
return;
}
//Add the new point to the point array.
pDoc->m_points[pDoc->m_numPoints++] = point;
m_oldPoint = point;
//If we have filled the point array, finish the polygon
//and display a message.
if (pDoc->m_numPoints == MAX_POINTS)
{
pDoc->m_drawing = FALSE;
//Initialize the CRgn object with our points.
if (!pDoc->m_region->CreatePolygonRgn(pDoc->m_points,
pDoc->m_numPoints,
0 ))
{
MessageBox("Could not create region.");
return;
}
pDoc->m_regionDefined = TRUE;
//Draw the last leg of the polygon.
DrawLine(pDoc->m_points[pDoc->m_numPoints-1], point);
DrawLine(pDoc->m_points[0], pDoc->m_points[pDoc->m_numPoints-1]);
MessageBox("Maximum points reached - polygon completed.");
}
}
else //If we're testing...
{
//Display "hit" or "miss" message.
if (pDoc->m_region->PtInRegion(point))
MessageBox("A hit!");
else
MessageBox("A miss!");
}
}
void CHotspot3View::OnMouseMove(UINT nFlags, CPoint point)
{
CHotspot3Doc* pDoc = GetDocument();
//If we are drawing and at least one point has
//been placed, erase the old line and draw the new.
if (pDoc->m_drawing && (pDoc->m_numPoints > 0))
{
DrawLine(pDoc->m_points[pDoc->m_numPoints-1], m_oldPoint);
m_oldPoint = point;
DrawLine(pDoc->m_points[pDoc->m_numPoints-1], m_oldPoint);
}
}
void CHotspot3View::OnRButtonUp(UINT nFlags, CPoint point)
{
ReleaseCapture();
CHotspot3Doc* pDoc = GetDocument();
//Respond to right click only if we are drawing and at
//least 3 points have been placed.
if ((pDoc->m_drawing == TRUE) && (pDoc->m_numPoints > 2))
{
pDoc->m_drawing = FALSE;
//Initialize the CRgn object with our points.
if (!pDoc->m_region->CreatePolygonRgn(pDoc->m_points,
pDoc->m_numPoints,
0 ))
{
MessageBox("Could not create region.");
return;
}
pDoc->m_regionDefined = TRUE;
//Draw the last leg of the polygon.
DrawLine(pDoc->m_points[pDoc->m_numPoints-1], point);
DrawLine(pDoc->m_points[0], pDoc->m_points[pDoc->m_numPoints-1]);
}
else
MessageBox("You must define at least 3 points before completing the polygon.");
}
void CHotspot3View::DrawLine(CPoint point1, CPoint point2)
{
//Draws a line in "invert" mode between
//the two specified points.
CClientDC dc(this);
m_oldMode = dc.SetROP2(R2_NOT);
m_oldPen = (CPen*) dc.SelectStockObject(BLACK_PEN);
dc.MoveTo(point1);
dc.LineTo(point2);
//Reselect original pen and mode.
dc.SelectObject(m_oldPen);
dc.SetROP2(m_oldMode);
}
void CHotspot3View::OnModeTest()
{
//Switch to testing mode.
CHotspot3Doc* pDoc = GetDocument();
pDoc->m_testing = TRUE;
}
void CHotspot3View::OnModeDefine()
{
//Switch to defining mode.
CHotspot3Doc* pDoc = GetDocument();
pDoc->m_testing = FALSE;
}
void CHotspot3View::OnUpdateModeTest(CCmdUI* pCmdUI)
{
//Enable the Test command only if
//a region is defined/
CHotspot3Doc* pDoc = GetDocument();
pCmdUI->Enable(pDoc->m_regionDefined);
}
void CHotspot3View::OnNewRegion()
{
CHotspot3Doc* pDoc = GetDocument();
//If a region is already defined, draw over it to erase it.
if (pDoc->m_regionDefined)
{
for (int i = 0; i < pDoc->m_numPoints-1; i++)
DrawLine(pDoc->m_points[i], pDoc->m_points[i+1]);
DrawLine( pDoc->m_points[i], pDoc->m_points[0]);
pDoc->m_regionDefined = FALSE;
}
//Delete the existing CRgn object and create a new one.
delete pDoc->m_region;
pDoc->m_region = new CRgn;
//Initialize document variables.
pDoc->m_numPoints = 0;
pDoc->m_testing = FALSE;
pDoc->m_regionDefined = FALSE;
}
void CHotspot3View::OnUpdateNewRegion(CCmdUI* pCmdUI)
{
//Enable the New Region command only if
//a region is defined.
CHotspot3Doc* pDoc = GetDocument();
pCmdUI->Enable(pDoc->m_regionDefined);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -