www.pudn.com > Poly.rar > PolyTopView.cpp
// PolyTopView.cpp : implementation of the CPolyTopView class
//
#include "stdafx.h"
#include "PolyTop.h"
#include "MainFrm.h"
#include "PolyTopDoc.h"
#include "PolyTopView.h"
#include "DefineZoneDlg.h"
#include "datasdef.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView
IMPLEMENT_DYNCREATE(CPolyTopView, CView)
BEGIN_MESSAGE_MAP(CPolyTopView, CView)
//{{AFX_MSG_MAP(CPolyTopView)
ON_WM_CREATE()
ON_WM_HSCROLL()
ON_WM_VSCROLL()
ON_WM_MOUSEMOVE()
ON_WM_RBUTTONDOWN()
ON_COMMAND(ID_ALLVIEW, OnAllview)
ON_COMMAND(ID_NORMALVIEW, OnNormalview)
ON_WM_LBUTTONDOWN()
ON_UPDATE_COMMAND_UI(ID_ALLVIEW, OnUpdateAllview)
ON_UPDATE_COMMAND_UI(ID_NORMALVIEW, OnUpdateNormalview)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
ON_WM_INITMENUPOPUP()
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView construction/destruction
CPolyTopView::CPolyTopView()
{
// TODO: add construction code here
m_zoomRate = 1.0;
m_dirMode = 0;
m_nState = 0;
}
CPolyTopView::~CPolyTopView()
{
}
BOOL CPolyTopView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView drawing
void CPolyTopView::OnDraw(CDC* pDC)
{
CPolyTopDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
CSize size = pDoc->m_size;
CRect rect;
GetClientRect(&rect);
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
// TODO: add draw code for native data here
//StretchBlt (hDc, 0, 0, 0, 0, hDc, 0, 0, size.cx , size.cy,SRCCOPY );
int i;
Point p1,p2;
if(pDoc->m_disPt)
{
pointList::iterator it;
for(it=pDoc->m_ptList.begin();it!=pDoc->m_ptList.end();it++)
{
Point pt = *it;
DrawPt(pt,RGB(20,20,20));
}
//画边框
}
if(pDoc->m_disPoly)
{
intIntList::iterator it1;
intList ptlist;
for(it1=pDoc->m_vPolygons.begin();it1!=pDoc->m_vPolygons.end();it1++)
{
ptlist = *it1;
for(i=0;im_poly.GetPointByNum(ptlist[i]);
p2 = pDoc->m_poly.GetPointByNum(ptlist[i+1]);
DrawLine(p1,p2,RGB(255,0,0));
//Sleep(200);
}
p1 = pDoc->m_poly.GetPointByNum(ptlist[ptlist.size()-1]);
p2 = pDoc->m_poly.GetPointByNum(ptlist[0]);
DrawLine(p1,p2,RGB(255,0,0));
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView printing
BOOL CPolyTopView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CPolyTopView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CPolyTopView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView diagnostics
#ifdef _DEBUG
void CPolyTopView::AssertValid() const
{
CView::AssertValid();
}
void CPolyTopView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPolyTopDoc* CPolyTopView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPolyTopDoc)));
return (CPolyTopDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPolyTopView message handlers
void CPolyTopView::DrawPt(Point p,COLORREF color)
{
int x,y;
CClientDC dc(this);
CPen pen(PS_SOLID,3,color);
CPen *pOld;
CRect rect;
CPoint pt;
char as[10];
sprintf(as,"%d",p.num);
WorldToClient(p.x,p.y,pt);
pOld = dc.SelectObject(&pen);
x = pt.x; y = pt.y;
rect.left = x-2; rect.right = x+2;
rect.top = y-2; rect.bottom= y+2;
dc.TextOut(x+2,y+2,as);
dc.Ellipse(&rect);
}
void CPolyTopView::DrawLine(Point p1, Point p2,COLORREF color)
{
CClientDC dc(this);
CPen pen(PS_SOLID,3,color);
CPen *pOld;
CPoint pt1,pt2;
WorldToClient(p1.x,p1.y,pt1);
WorldToClient(p2.x,p2.y,pt2);
pOld = dc.SelectObject(&pen);
dc.MoveTo(pt1);
dc.LineTo(pt2);
}
int CPolyTopView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
CDC* pDC = GetDC();
m_Assist.OnCreate(this);
m_Assist.SetStep( 100 );
ReleaseDC(pDC);
return 0;
}
void CPolyTopView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
ASSERT_VALID(pDoc);
CRect rect;
GetClientRect(&rect);
CDC* pDC=GetDC();
m_Assist.Change( rect.right,rect.bottom,pDoc->m_size,m_zoomRate );
ReleaseDC(pDC);
}
void CPolyTopView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if( m_Assist.OnHScroll( nSBCode,nPos ) )
{
Invalidate(TRUE);
}
CView::OnHScroll(nSBCode, nPos, pScrollBar);
}
void CPolyTopView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: Add your message handler code here and/or call default
if( m_Assist.OnVScroll( nSBCode,nPos ) )
{
Invalidate(TRUE);
}
CView::OnVScroll(nSBCode, nPos, pScrollBar);
}
void CPolyTopView::ClientToWorld(CPoint point, int ¢er_x, int ¢er_y)
{
int xPos = m_Assist.GetScrollPos(SB_HORZ);
int yPos = m_Assist.GetScrollPos(SB_VERT);
center_x = int(float(xPos+point.x)/m_zoomRate+0.5);
center_y = int(float(yPos+point.y)/m_zoomRate+0.5);
}
void CPolyTopView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CMainFrame* pFrm = (CMainFrame*)AfxGetMainWnd();
CStatusBar* pStatus = &pFrm->m_wndStatusBar;
CString str;
int x = 0,y = 0;
ClientToWorld(point,x,y);
if(pStatus)
{
str.Format("%d %5d",x,y);
pStatus->SetPaneText(1,str);
}
CView::OnMouseMove(nFlags, point);
}
void CPolyTopView::OnRButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//弹出显示方式菜单
CMenu menu;
CPoint temPt;
temPt = point;
if(!menu.LoadMenu(IDR_MENU_MODE))
{
AfxMessageBox("Load menu fail!");
CView::OnRButtonDown(nFlags, point);
return;
}
ClientToScreen(&temPt);
menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBUTTON,temPt.x,temPt.y,this);
CView::OnRButtonDown(nFlags, point);
}
void CPolyTopView::OnAllview()
{
// TODO: Add your command handler code here
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
if(m_dirMode) return;
CRect rect;
CSize size = pDoc->m_size;
float scale_x,scale_y,scale;
GetClientRect(&rect);
scale_x = float(rect.Width())/float(size.cx);
scale_y = float(rect.Height())/float(size.cy);
scale = (scale_x>scale_y)?scale_x:scale_y;
m_zoomRate = scale;
m_dirMode = 1;
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
Invalidate(FALSE);
}
void CPolyTopView::OnNormalview()
{
// TODO: Add your command handler code here
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
if(!m_dirMode) return;
CSize size = pDoc->m_size;
// 获取此时鼠标所在位置
CPoint point;
CRect rect;
int cur_x,cur_y; // 当前点在影象上的坐标
int xPos,yPos;
GetClientRect(&rect);
int br = ::GetCursorPos(&point);
if(!br) return;
ClientToWorld(point,cur_x,cur_y);
m_zoomRate = 1.0f;
xPos = (int)cur_x - rect.Width()/2; yPos = (int)cur_y - rect.Height()/2;
m_Assist.SetBothPos(xPos,yPos);
m_dirMode = 0;
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
Invalidate(FALSE);
}
void CPolyTopView::ZoomSource()
{
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
ASSERT_VALID(pDoc);
m_zoomRate = 1;
CRect rect;
CSize size = pDoc->m_size;
GetClientRect( &rect );
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
// 设置滚动条位置
m_Assist.SetBothPos(0,0);
Invalidate(TRUE);
}
void CPolyTopView::WorldToClient(float center_x, float center_y, CPoint &point)
{
int xPos = m_Assist.GetScrollPos(SB_HORZ);
int yPos = m_Assist.GetScrollPos(SB_VERT);
point.x = int(center_x*m_zoomRate-xPos+0.5);
point.y = int(center_y*m_zoomRate-yPos+0.5);
}
void CPolyTopView::ZoomIn(CPoint point)
{
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
ASSERT_VALID(pDoc);
int x,y;
CPoint pt;
// 获取当前鼠标位置
ClientToWorld(point,x,y);
m_zoomRate *= 0.5;
CRect rect;
CSize size = pDoc->m_size;
GetClientRect( &rect );
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
// 设置滚动条位置
m_Assist.SetBothPos(x*m_zoomRate-rect.Width()/2,y*m_zoomRate-rect.Height()/2);
Invalidate(TRUE);
}
void CPolyTopView::ZoomOut(CPoint point)
{
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
ASSERT_VALID(pDoc);
int x,y;
CPoint pt;
// 获取当前鼠标位置
ClientToWorld(point,x,y);
m_zoomRate *= 2;
CRect rect;
CSize size = pDoc->m_size;
GetClientRect( &rect );
m_Assist.Change( rect.right,rect.bottom,size,m_zoomRate );
// 设置滚动条位置
m_Assist.SetBothPos(x*m_zoomRate-rect.Width()/2,y*m_zoomRate-rect.Height()/2);
Invalidate(TRUE);
}
void CPolyTopView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CPolyTopDoc* pDoc = (CPolyTopDoc*)GetDocument();
switch(pDoc->m_chooseMode) {
case mode_zoom_in:
ZoomIn(point);
break;
case mode_zoom_out:
ZoomOut(point);
break;
default:
break;
}
CView::OnLButtonDown(nFlags, point);
}
void CPolyTopView::OnUpdateAllview(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
pCmdUI->SetCheck( m_dirMode == 1 );
}
void CPolyTopView::OnUpdateNormalview(CCmdUI* pCmdUI)
{
pCmdUI->SetCheck( m_dirMode == 0 );
}
void CPolyTopView::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
ASSERT(pPopupMenu != NULL);
// check the enabled state of various menu items
CCmdUI state;
state.m_pMenu = pPopupMenu;
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pParentMenu == NULL);
// determine if menu is popup in top-level menu and set m_pOther to
// it if so (m_pParentMenu == NULL indicates that it is secondary popup)
HMENU hParentMenu;
if (AfxGetThreadState()->m_hTrackingMenu == pPopupMenu->m_hMenu)
state.m_pParentMenu = pPopupMenu; // parent == child for tracking popup
else if ((hParentMenu = ::GetMenu(m_hWnd)) != NULL)
{
CWnd* pParent = this;
// child windows don't have menus -- need to go to the top!
if (pParent != NULL &&
(hParentMenu = ::GetMenu(pParent->m_hWnd)) != NULL)
{
int nIndexMax = ::GetMenuItemCount(hParentMenu);
for (int nIndex = 0; nIndex < nIndexMax; nIndex++)
{
if (::GetSubMenu(hParentMenu, nIndex) == pPopupMenu->m_hMenu)
{
// when popup is found, m_pParentMenu is containing menu
state.m_pParentMenu = CMenu::FromHandle(hParentMenu);
break;
}
}
}
}
state.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for (state.m_nIndex = 0; state.m_nIndex < state.m_nIndexMax;
state.m_nIndex++)
{
state.m_nID = pPopupMenu->GetMenuItemID(state.m_nIndex);
if (state.m_nID == 0)
continue; // menu separator or invalid cmd - ignore it
ASSERT(state.m_pOther == NULL);
ASSERT(state.m_pMenu != NULL);
if (state.m_nID == (UINT)-1)
{
// possibly a popup menu, route to first item of that popup
state.m_pSubMenu = pPopupMenu->GetSubMenu(state.m_nIndex);
if (state.m_pSubMenu == NULL ||
(state.m_nID = state.m_pSubMenu->GetMenuItemID(0)) == 0 ||
state.m_nID == (UINT)-1)
{
continue; // first item of popup can't be routed to
}
state.DoUpdate(this, TRUE); // popups are never auto disabled
}
else
{
// normal menu item
// Auto enable/disable if frame window has 'm_bAutoMenuEnable'
// set and command is _not_ a system command.
state.m_pSubMenu = NULL;
state.DoUpdate(this, FALSE);
}
// adjust for menu deletions and additions
UINT nCount = pPopupMenu->GetMenuItemCount();
if (nCount < state.m_nIndexMax)
{
state.m_nIndex -= (state.m_nIndexMax - nCount);
while (state.m_nIndex < nCount &&
pPopupMenu->GetMenuItemID(state.m_nIndex) == state.m_nID)
{
state.m_nIndex++;
}
}
state.m_nIndexMax = nCount;
}
}