⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hotspvw.cpp

📁 VisualC++多媒体开发指南》一书的光盘源代码
💻 CPP
字号:
// hotspvw.cpp : implementation of the CHotspot2View class
//

#include "stdafx.h"
#include "hotspot2.h"

#include "hotspdoc.h"
#include "hotspvw.h"
#include "linktext.h"
#include "editlink.h"


#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHotspot2View

IMPLEMENT_DYNCREATE(CHotspot2View, CView)

BEGIN_MESSAGE_MAP(CHotspot2View, CView)
	//{{AFX_MSG_MAP(CHotspot2View)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_MODE_TEST, OnModeTest)
	ON_COMMAND(ID_MODE_DEFINE, OnModeDefine)
	ON_UPDATE_COMMAND_UI(ID_MODE_DEFINE, OnUpdateModeDefine)
	ON_UPDATE_COMMAND_UI(ID_MODE_TEST, OnUpdateModeTest)
	ON_UPDATE_COMMAND_UI(ID_FILE_SAVEHOTSPOT, OnUpdateFileSavehotspot)
	ON_COMMAND(ID_FILE_SAVEHOTSPOT, OnFileSavehotspot)
	ON_UPDATE_COMMAND_UI(ID_EDIT_EDITHOTSPOTS, OnUpdateEditEdithotspots)
	ON_COMMAND(ID_EDIT_EDITHOTSPOTS, OnEditEdithotspots)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHotspot2View construction/destruction

CHotspot2View::CHotspot2View()
{
}

CHotspot2View::~CHotspot2View()
{
}

/////////////////////////////////////////////////////////////////////////////
// CHotspot2View drawing

void CHotspot2View::OnDraw(CDC* pDC)
{
	CHotspot2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	HDIB hDIB = pDoc->GetHDIB();
	if (hDIB == NULL)
		return;
					
	//Display the bitmap.
	CRect rectDIB(0,0, pDoc->GetDocSize()->cx, pDoc->GetDocSize()->cy);
	pDC->SelectPalette(pDoc->GetDIBPalette(), FALSE);
	pDC->RealizePalette();
	::PaintDIB(pDC->m_hDC, &rectDIB, hDIB, &rectDIB, pDoc->GetDIBPalette());

	if (pDoc->m_modeTesting == FALSE)
		SetStatusText("Defining");
	else
		SetStatusText("Testing");		

	//If we are in define mode and a region is defined,
	//draw its outline.
	if (!pDoc->m_modeTesting && pDoc->m_regionDefined)
		OutlineRegion();
}

/////////////////////////////////////////////////////////////////////////////
// CHotspot2View diagnostics

#ifdef _DEBUG
void CHotspot2View::AssertValid() const
{
	CView::AssertValid();
}

void CHotspot2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CHotspot2Doc* CHotspot2View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CHotspot2Doc)));
	return (CHotspot2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CHotspot2View message handlers

void CHotspot2View::OnLButtonDown(UINT nFlags, CPoint point) 
{
	CHotspot2Doc* pDoc = GetDocument();
	CClientDC dc(this);
	//Process depending on whether we are in
	//defining or testing mode.

	if (pDoc->m_modeTesting == FALSE)		//Defining.
		{
		//Erase any previously drawn rectangle.
		OutlineRegion();
		
		//Save mouse coordinates.
		pDoc->m_currentHS.region.left = pDoc->m_currentHS.region.right = point.x;
		pDoc->m_currentHS.region.top = pDoc->m_currentHS.region.bottom = point.y;

		//Capture the mouse and set the drawing flag.
		//SetCapture();
		m_drawing = TRUE;
		}
	else		//Testing.
		{
		 if (pDoc->m_currentHS.region.PtInRect(point))
		 	MessageBox("A Hit!");
		else
			MessageBox("A Miss.");
		}
}

void CHotspot2View::OnMouseMove(UINT nFlags, CPoint point) 
{
	CHotspot2Doc* pDoc = GetDocument();

	//Process only if drawing flag is set.
	if (m_drawing == TRUE)
		{		
		//Draw over the current rectangle to erase it.
		OutlineRegion();

		//Save new mouse position.
		pDoc->m_currentHS.region.right = point.x;
		pDoc->m_currentHS.region.bottom = point.y;

		//Draw new rectangle.
		OutlineRegion();
		}
}

void CHotspot2View::OnLButtonUp(UINT nFlags, CPoint point) 
{
	CHotspot2Doc* pDoc = GetDocument();

	//Process only if drawing flag is set.
	if (m_drawing == TRUE)
		{		
		m_drawing = FALSE;

		//Release the mouse.
		ReleaseCapture();

		//Set the region defined flag only if
		//a rectangle has been defined.
		if ((pDoc->m_currentHS.region.left != pDoc->m_currentHS.region.right) &&
			(pDoc->m_currentHS.region.top != pDoc->m_currentHS.region.bottom))
				pDoc->m_regionDefined = TRUE;
		}		
}

void CHotspot2View::OnModeTest() 
{
	CHotspot2Doc* pDoc = GetDocument();
	pDoc->m_modeTesting = TRUE;
 	InvalidateRect(pDoc->m_currentHS.region, FALSE);
	SetStatusText("Testing");	
}

void CHotspot2View::OnModeDefine() 
{
	CHotspot2Doc* pDoc = GetDocument();
	pDoc->m_modeTesting = FALSE;	
	pDoc->m_currentHS.region = CRect(0,0,0,0);
	SetStatusText("Defining");		
}

void CHotspot2View::SetStatusText(CString message)
{
	//Sets the status bar message to the argument text.
	CStatusBar* pStatus = (CStatusBar*) AfxGetApp()->
		m_pMainWnd->GetDescendantWindow(AFX_IDW_STATUS_BAR);
	pStatus->SetPaneText(0, message);
}

void CHotspot2View::OnFileSavehotspot() 
{
	CHotspot2Doc* pDoc = GetDocument();
	
	//Display the dialog box for entry 
	//of the target text.
	CLinkTextDlg dlg;
	dlg.m_linkText = pDoc->m_currentHS.target;
	int ret = dlg.DoModal();

	//If user selected Cancel, return.
	if (ret != IDOK)
		return;

	//Erase the hotspot outline.
	OutlineRegion();

	//Save the hot spot
	strcpy(pDoc->m_currentHS.target, dlg.m_linkText);
	strcpy(pDoc->m_currentHS.image, pDoc->GetTitle());
	pDoc->SaveHotspot();
}

void CHotspot2View::OutlineRegion(void)
{
	CClientDC dc(this);
	CHotspot2Doc* pDoc = GetDocument();
	m_oldMode = dc.SetROP2(R2_NOT);
	m_oldBrush = (CBrush*) dc.SelectStockObject(NULL_BRUSH);
	m_oldPen = (CPen*) dc.SelectStockObject(BLACK_PEN);
	dc.Rectangle(pDoc->m_currentHS.region);
	//Reselect original brush, pen, and mode.
	dc.SelectObject(m_oldBrush);
	dc.SelectObject(m_oldPen);
	dc.SetROP2(m_oldMode);
}

void CHotspot2View::OnEditEdithotspots() 
{
	CEditLinkDlg dlg;
	int ret = dlg.DoModal();

	//If user did not select a hotspot to edit, or
	//did not select the Edit button, return.
	if (ret != IDOK || dlg.m_selectedLink == -1)
		{
		MessageBox("Nothing Selected");
		return;
		}

	//Get the record to edit.
	CHotspot2Doc* pDoc = GetDocument();
	pDoc->m_editRecord = pDoc->m_recordNum[dlg.m_selectedLink];
	long pos = sizeof(HotSpotRecord) * pDoc->m_editRecord;
	pDoc->m_datafile.Seek(pos, CFile::begin);
	pDoc->m_datafile.Read(&pDoc->m_currentHS, sizeof(HotSpotRecord));
	SetStatusText("Editing");
	OutlineRegion();
}

void CHotspot2View::OnUpdateEditEdithotspots(CCmdUI* pCmdUI) 
{
	CHotspot2Doc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->m_toEdit);
}

void CHotspot2View::OnUpdateModeDefine(CCmdUI* pCmdUI) 
{
	CHotspot2Doc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->GetHDIB() != NULL);
}

void CHotspot2View::OnUpdateModeTest(CCmdUI* pCmdUI) 
{
	CHotspot2Doc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->m_regionDefined);		
}

void CHotspot2View::OnUpdateFileSavehotspot(CCmdUI* pCmdUI) 
{
	CHotspot2Doc* pDoc = GetDocument();
	pCmdUI->Enable(pDoc->m_regionDefined);			 	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -