// dibview.cpp : implementation of the CDibView class
//
// This is a part of the Microsoft Foundation Classes C++ library.
// Copyright (C) 1992-1997 Microsoft Corporation
// All rights reserved.
//
// This source code is only intended as a supplement to the
// Microsoft Foundation Classes Reference and related
// electronic documentation provided with the library.
// See these sources for detailed information regarding the
// Microsoft Foundation Classes product.
#include "stdafx.h"
#include "diblook.h"
#include "dibdoc.h"
#include "dibview.h"
#include "dibapi.h"
#include "mainfrm.h"
#include "SelectKB.h"
#include "matrix.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDibView
IMPLEMENT_DYNCREATE(CDibView, CScrollView)
BEGIN_MESSAGE_MAP(CDibView, CScrollView)
//{{AFX_MSG_MAP(CDibView)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_COMMAND(ID_InnerOri, OnInnerOri)
ON_COMMAND(ID_LPtoKB, OnLPtoKB)
ON_COMMAND(ID_SurveKB, OnSurveKB)
ON_COMMAND(ID_FORSTNER, OnForstner)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDibView construction/destruction
CDibView::CDibView()
{
m_HArrow=AfxGetApp()->LoadStandardCursor(IDC_ARROW);
m_HCross=AfxGetApp()->LoadStandardCursor(IDC_CROSS);
//m_HCup=AfxGetApp()->LoadStandardCursor(IDC_CUP);
m_WorkStatus=0; //无工作
m_Resolution=0.045;//扫描间隔为0.045mm
//各框标的标准坐标
m_KBxy[0][0]=-106.003;
m_KBxy[0][1]=106.003;
m_KBxy[1][0]=-0.004;
m_KBxy[1][1]=110.004;
m_KBxy[2][0]=106.001;
m_KBxy[2][1]=106.001;
m_KBxy[3][0]=-110.001;
m_KBxy[3][1]=0.002;
m_KBxy[4][0]=109.997;
m_KBxy[4][1]=0.000;
m_KBxy[5][0]=-106.003;
m_KBxy[5][1]=-106.003;
m_KBxy[6][0]=0.002;
m_KBxy[6][1]=-109.998;
m_KBxy[7][0]=106.003;
m_KBxy[7][1]=-106.005;
//把各框标的屏幕逻辑坐标初始化为零
for(int i=0;i<8;i++)
for(int j=0;j<2;j++)
{
m_LPxy[i][j]=0;
}
}
CDibView::~CDibView()
{
}
/////////////////////////////////////////////////////////////////////////////
// CDibView drawing
void CDibView::OnDraw(CDC* pDC)
{
CDibDoc* pDoc = GetDocument();
HDIB hDIB = pDoc->GetHDIB();
if (hDIB != NULL)
{
LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
int cxDIB = (int) ::DIBWidth(lpDIB); // Size of DIB - x
int cyDIB = (int) ::DIBHeight(lpDIB); // Size of DIB - y
::GlobalUnlock((HGLOBAL) hDIB);
CRect rcDIB;
rcDIB.top = rcDIB.left = 0;
rcDIB.right = cxDIB;
rcDIB.bottom = cyDIB;
CRect rcDest;
if (pDC->IsPrinting()) // printer DC
{
// get size of printer page (in pixels)
int cxPage = pDC->GetDeviceCaps(HORZRES);
int cyPage = pDC->GetDeviceCaps(VERTRES);
// get printer pixels per inch
int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
//
// Best Fit case -- create a rectangle which preserves
// the DIB's aspect ratio, and fills the page horizontally.
//
// The formula in the "->bottom" field below calculates the Y
// position of the printed bitmap, based on the size of the
// bitmap, the width of the page, and the relative size of
// a printed pixel (cyInch / cxInch).
//
rcDest.top = rcDest.left = 0;
rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
/ ((double)cxDIB * cxInch));
rcDest.right = cxPage;
}
else // not printer DC
{
rcDest = rcDIB;
}
::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
&rcDIB, pDoc->GetDocPalette());
}
}
/////////////////////////////////////////////////////////////////////////////
// CDibView printing
BOOL CDibView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
/////////////////////////////////////////////////////////////////////////////
// CDibView commands
LRESULT CDibView::OnDoRealize(WPARAM wParam, LPARAM)
{
ASSERT(wParam != NULL);
CDibDoc* pDoc = GetDocument();
if (pDoc->GetHDIB() == NULL)
return 0L; // must be a new document
CPalette* pPal = pDoc->GetDocPalette();
if (pPal != NULL)
{
CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
ASSERT_KINDOF(CMainFrame, pAppFrame);
CClientDC appDC(pAppFrame);
// All views but one should be a background palette.
// wParam contains a handle to the active view, so the SelectPalette
// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
if (oldPalette != NULL)
{
UINT nColorsChanged = appDC.RealizePalette();
if (nColorsChanged > 0)
pDoc->UpdateAllViews(NULL);
appDC.SelectPalette(oldPalette, TRUE);
}
else
{
TRACE0("\tSelectPalette failed in CDibView::OnPaletteChanged\n");
}
}
return 0L;
}
void CDibView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
ASSERT(GetDocument() != NULL);
SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
}
void CDibView::OnActivateView(BOOL bActivate, CView* pActivateView,
CView* pDeactiveView)
{
CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
if (bActivate)
{
ASSERT(pActivateView == this);
OnDoRealize((WPARAM)m_hWnd, 0); // same as SendMessage(WM_DOREALIZE);
}
}
void CDibView::OnEditCopy()
{
CDibDoc* pDoc = GetDocument();
// Clean clipboard of contents, and copy the DIB.
if (OpenClipboard())
{
BeginWaitCursor();
EmptyClipboard();
SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
CloseClipboard();
EndWaitCursor();
}
}
void CDibView::OnUpdateEditCopy(CCmdUI* pCmdUI)
{
pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
}
void CDibView::OnEditPaste()
{
HDIB hNewDIB = NULL;
if (OpenClipboard())
{
BeginWaitCursor();
hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
CloseClipboard();
if (hNewDIB != NULL)
{
CDibDoc* pDoc = GetDocument();
pDoc->ReplaceHDIB(hNewDIB); // and free the old DIB
pDoc->InitDIBData(); // set up new size & palette
pDoc->SetModifiedFlag(TRUE);
SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
OnDoRealize((WPARAM)m_hWnd,0); // realize the new palette
pDoc->UpdateAllViews(NULL);
}
EndWaitCursor();
}
}
void CDibView::OnUpdateEditPaste(CCmdUI* pCmdUI)
{
pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
}
void CDibView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/////坐标转换/////////////
CClientDC dc(this); //定义设备对象
OnPrepareDC(&dc); //初始化设备对象
CPoint local=point; //定义CPoint类实体,并初始化为设备坐标
dc.DPtoLP(&local); //将设备坐标转为逻辑坐标
CString msg;
msg.Format(" X = %4d, Y = %4d ",
local.x,local.y);
double LPx,LPy,KBx,KBy;
//将该段程序放入OnLButtonDown(UINT nFlags, CPoint point)
//和OnMouseMove(UINT nFlags, CPoint point)中
switch(m_WorkStatus)
{
case 0: //无工作;2-;3-
{break;}
case 1://测量光标;
{
CSelectKB dlg;
if(dlg.DoModal()==IDOK)
{
m_LPxy[dlg.m_Index-1][0]=local.x;
m_LPxy[dlg.m_Index-1][1]=local.y;
}
}
case 2://内定向
{
break;
}
case 3://屏幕坐标转为框标坐标
{
LPx=m_Resolution * (double)local.x;
LPy=m_Resolution * (double)local.y;
LPtoKB(LPx,LPy,&KBx,&K