www.pudn.com > MapDB.rar > MapDBView.cpp


// MapDBView.cpp : implementation of the CMapDBView class 
// 
 
#include "stdafx.h" 
#include "MapDB.h" 
 
#include "MapDBDoc.h" 
#include "MapDBView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView 
 
IMPLEMENT_DYNCREATE(CMapDBView, CScrollView) 
 
BEGIN_MESSAGE_MAP(CMapDBView, CScrollView) 
	//{{AFX_MSG_MAP(CMapDBView) 
	ON_WM_ERASEBKGND() 
	ON_COMMAND(ID_ZOOMIN, OnZoomIn) 
	ON_COMMAND(ID_ZOOMOUT, OnZoomOut) 
	ON_WM_LBUTTONDOWN() 
	ON_WM_LBUTTONUP() 
	ON_WM_MOUSEMOVE() 
	ON_WM_SETCURSOR() 
	ON_COMMAND(ID_ZOOM_ORIGINAL, OnZoomOriginal) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView construction/destruction 
 
CMapDBView::CMapDBView() 
{ 
	m_bPaning = FALSE; 
	m_hSelect = AfxGetApp()->LoadCursor(IDC_SELECT_CURSOR); 
} 
 
CMapDBView::~CMapDBView() 
{ 
} 
 
BOOL CMapDBView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CScrollView::PreCreateWindow(cs); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView drawing 
 
void CMapDBView::OnDraw(CDC* pDC) 
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	ASSERT_VALID(pDoc); 
	if ( pDoc->IsInited() ) 
	{ 
		pDC->BitBlt(0,0,pDoc->m_sizeDraw.cx,pDoc->m_sizeDraw.cy,&pDoc->m_dc,0,0,SRCCOPY); 
		DrawVector(pDC); 
		if ( pDoc->m_bSelQuery ) 
			DrawSelect(); 
	} 
} 
 
void CMapDBView::OnInitialUpdate() 
{ 
	CScrollView::OnInitialUpdate(); 
	CMapDBDoc* pDoc = GetDocument(); 
	pDoc->m_pbitmap = GetDC()->GetCurrentBitmap(); 
	pDoc->m_dc.DeleteDC(); 
	pDoc->m_dc.CreateCompatibleDC(GetDC()); 
	pDoc->m_dc.SetStretchBltMode(COLORONCOLOR);  
	SetImage(); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView printing 
 
BOOL CMapDBView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CMapDBView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CMapDBView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView diagnostics 
 
#ifdef _DEBUG 
void CMapDBView::AssertValid() const 
{ 
	CScrollView::AssertValid(); 
} 
 
void CMapDBView::Dump(CDumpContext& dc) const 
{ 
	CScrollView::Dump(dc); 
} 
 
CMapDBDoc* CMapDBView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapDBDoc))); 
	return (CMapDBDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMapDBView message handlers 
 
void CMapDBView::DrawVector(CDC *pDC) 
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	CPen pen(PS_SOLID,1,RGB(255,0,0)),* oldpen; 
	oldpen = pDC->SelectObject(&pen); 
	double sx = (double)pDoc->m_sizeDraw.cx / (double)pDoc->m_sizePhoto.cx; 
	double sy = (double)pDoc->m_sizeDraw.cy / (double)pDoc->m_sizePhoto.cy; 
	for (long count = 0; count < pDoc->m_objsel.GetSize(); count ++) 
	{ 
		BITS sel = pDoc->m_objsel[count]; 
		for (int bitcount = 0;bitcount < 8;bitcount ++) 
		{ 
			if ( sel[bitcount] ) 
				pDoc->m_mapobjs[count * 8 + bitcount]->DrawVector(pDC,sx,sy); 
		} 
	} 
	pDC->SelectObject(oldpen); 
	pen.DeleteObject(); 
} 
 
void CMapDBView::SetImage() 
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	SetScrollSizes(MM_TEXT, CSize(pDoc->m_sizeDraw.cx,pDoc->m_sizeDraw.cy)); 
	if ( pDoc->m_dc.m_hDC ) 
	{ 
		pDoc->m_dc.SelectObject(pDoc->m_pbitmap); 
		pDoc->m_bitmap.DeleteObject(); 
		pDoc->m_bitmap.CreateCompatibleBitmap(GetDC(),pDoc->m_sizeDraw.cx,pDoc->m_sizeDraw.cy); 
		pDoc->m_dc.SelectObject(&pDoc->m_bitmap); 
		StretchDIBits(pDoc->m_dc.m_hDC,0,0,pDoc->m_sizeDraw.cx,pDoc->m_sizeDraw.cy,0,0, 
			pDoc->m_sizePhoto.cx,pDoc->m_sizePhoto.cy,pDoc->m_text,pDoc->m_lpbiText,DIB_RGB_COLORS,SRCCOPY); 
	} 
} 
 
BOOL CMapDBView::OnEraseBkgnd(CDC* pDC)  
{ 
	CMapDBDoc* pDoc = GetDocument(); 
//	if ( ! pDoc->IsInited() ) 
		return CScrollView::OnEraseBkgnd(pDC); 
	CRect rect; 
	pDC->GetClipBox(&rect); 
	CRect rectb(0,min(rect.bottom,pDoc->m_sizeDraw.cy),rect.right,rect.bottom); 
	CRect rectr(min(pDoc->m_sizeDraw.cx,rect.right),0,rect.right,min(rect.bottom,pDoc->m_sizeDraw.cy)); 
	pDC->FillSolidRect(&rectb,pDC->GetBkColor()); 
	pDC->FillSolidRect(&rectr,pDC->GetBkColor()); 
	return TRUE; 
} 
 
void CMapDBView::OnZoomOut()  
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	double rate = pDoc->GetRate(); 
	pDoc->m_sizeDraw.cx = (long)(pDoc->m_sizeDraw.cx / rate); 
	pDoc->m_sizeDraw.cy = (long)(pDoc->m_sizeDraw.cy / rate);	 
	SetImage(); 
	Invalidate(); 
} 
 
void CMapDBView::OnZoomOriginal()  
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	pDoc->m_sizeDraw = pDoc->m_sizePhoto; 
	SetImage(); 
	Invalidate();	 
} 
 
void CMapDBView::OnZoomIn()  
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	double rate = pDoc->GetRate(); 
	pDoc->m_sizeDraw.cx = (long)(pDoc->m_sizeDraw.cx * rate); 
	pDoc->m_sizeDraw.cy = (long)(pDoc->m_sizeDraw.cy * rate);	 
	SetImage(); 
	Invalidate(false); 
} 
 
void CMapDBView::OnLButtonDown(UINT nFlags, CPoint point)  
{ 
	SetCapture(); 
	CMapDBDoc* pDoc = GetDocument(); 
	if ( pDoc->m_bSelQuery ) 
	{ 
		CClientDC dc(this); 
		OnPrepareDC(&dc); 
		dc.DPtoLP(&point); 
		ReleaseDC(&dc); 
		double sx = (double)pDoc->m_sizePhoto.cx / (double)pDoc->m_sizeDraw.cx; 
		double sy = (double)pDoc->m_sizePhoto.cy / (double)pDoc->m_sizeDraw.cy; 
		point.x = (int)(point.x * sx + 0.5); 
		point.y = (int)(point.y * sy + 0.5); 
		pDoc->m_selpt[0] = point; 
		pDoc->m_selpt[1] = point; 
		m_bPaning = TRUE; 
	} 
	 
	CScrollView::OnLButtonDown(nFlags, point); 
} 
 
void CMapDBView::OnLButtonUp(UINT nFlags, CPoint point)  
{ 
	ReleaseCapture(); 
	CMapDBDoc* pDoc = GetDocument(); 
	if ( pDoc->m_bSelQuery ) 
	{ 
		CRect rect; 
		GetClientRect(&rect); 
		if ( rect.PtInRect(point) ) 
		{ 
			CClientDC dc(this); 
			OnPrepareDC(&dc); 
			dc.DPtoLP(&point); 
			ReleaseDC(&dc); 
			double sx = (double)pDoc->m_sizePhoto.cx / (double)pDoc->m_sizeDraw.cx; 
			double sy = (double)pDoc->m_sizePhoto.cy / (double)pDoc->m_sizeDraw.cy; 
			point.x = (int)(point.x * sx + 0.5); 
			point.y = (int)(point.y * sy + 0.5); 
			pDoc->m_selpt[1] = point; 
			pDoc->QueryGrid(); 
		} 
		else 
		{ 
			pDoc->m_selpt[1] = pDoc->m_selpt[0]; 
			Invalidate(); 
		} 
		m_bPaning = FALSE; 
	} 
	 
	CScrollView::OnLButtonUp(nFlags, point); 
} 
 
void CMapDBView::OnMouseMove(UINT nFlags, CPoint point)  
{ 
	CMapDBDoc* pDoc = GetDocument(); 
	if ( pDoc->m_bSelQuery && m_bPaning ) 
	{ 
		CClientDC dc(this); 
		OnPrepareDC(&dc); 
		dc.DPtoLP(&point); 
		ReleaseDC(&dc); 
		double sx = (double)pDoc->m_sizePhoto.cx / (double)pDoc->m_sizeDraw.cx; 
		double sy = (double)pDoc->m_sizePhoto.cy / (double)pDoc->m_sizeDraw.cy; 
		point.x = (int)(point.x * sx + 0.5); 
		point.y = (int)(point.y * sy + 0.5); 
		pDoc->m_selpt[1] = point; 
		Invalidate(false); 
	} 
	 
	CScrollView::OnMouseMove(nFlags, point); 
} 
 
void CMapDBView::DrawSelect() 
{ 
//	if ( ! m_bPaning ) 
//		return; 
 
	CMapDBDoc* pDoc = GetDocument(); 
	CPen pen(PS_DASH,1,RGB(0,0,255)),* oldpen; 
	CDC * pDC = GetDC(); 
	oldpen = pDC->SelectObject(&pen); 
	double sx = (double)pDoc->m_sizeDraw.cx / (double)pDoc->m_sizePhoto.cx; 
	double sy = (double)pDoc->m_sizeDraw.cy / (double)pDoc->m_sizePhoto.cy; 
	CPoint pt[2]; 
	pt[0] = pDoc->m_selpt[0]; 
	pt[1] = pDoc->m_selpt[1]; 
	pt[0].x = (int)(pt[0].x * sx + 0.5); 
	pt[0].y = (int)(pt[0].y * sy + 0.5); 
	pt[1].x = (int)(pt[1].x * sx + 0.5); 
	pt[1].y = (int)(pt[1].y * sy + 0.5); 
	CClientDC dc(this); 
	OnPrepareDC(&dc); 
	dc.LPtoDP(&pt[0]); 
	dc.LPtoDP(&pt[1]); 
	ReleaseDC(&dc); 
	pDC->MoveTo(pt[0].x,pt[0].y);	 
	pDC->LineTo(pt[1].x,pt[0].y);	 
	pDC->LineTo(pt[1].x,pt[1].y);	 
	pDC->LineTo(pt[0].x,pt[1].y);	 
	pDC->LineTo(pt[0].x,pt[0].y);	 
	pDC->SelectObject(oldpen); 
	pen.DeleteObject();	 
} 
 
BOOL CMapDBView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)  
{ 
	if ( nHitTest == HTCLIENT ) 
	{ 
		CMapDBDoc* pDoc = GetDocument(); 
		if ( pDoc->m_bSelQuery ) 
		{ 
			SetCursor(m_hSelect); 
			return TRUE; 
		} 
	} 
	 
	return CScrollView::OnSetCursor(pWnd, nHitTest, message); 
}