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

📄 zmap.cpp

📁 一个微型的地图浏览工具
💻 CPP
字号:
// zMap.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "zMap.h"

#include "MainFrm.h"
#include "zMapDoc.h"
#include "zMapView.h"
#include "FileDialogEx.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CZMapApp

BEGIN_MESSAGE_MAP(CZMapApp, CWinApp)
	//{{AFX_MSG_MAP(CZMapApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CZMapApp construction

CZMapApp::CZMapApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CZMapApp object

CZMapApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CZMapApp initialization

BOOL CZMapApp::InitInstance()
{
	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox("OLE初始化失败");
		return FALSE;
	}
	
	m_nRecCount = 0;
	m_dScale = 1.0;
	m_dZoomRatio = 1.0;
	m_geoInfos.SetSize(0,10);

	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register the application's document templates.  Document templates
	//  serve as the connection between documents, frame windows and views.

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CZMapDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CZMapView));
	AddDocTemplate(pDocTemplate);

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;

	// The one and only window has been initialized, so show and update it.
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	m_strFileName = "china.geo";
	ReadFileInfo();
	
	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CZMapApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CZMapApp message handlers

BOOL CZMapApp::ReadFileInfo()
{
	CFile mFile, idxFile;
	BOOL bOpened = mFile.Open(m_strFileName,CFile::modeRead);
	
	if(!bOpened)
	{
		AfxMessageBox("文件无法打开");
		return FALSE;
	}
	
	CString strIdxFile = m_strFileName;
	strIdxFile.MakeLower();
	
	strIdxFile.Replace(".geo",".idx");
	
	idxFile.Open(strIdxFile,CFile::modeRead);
	
	idxFile.Read(&m_nRecCount,sizeof(int));
	
	idxFile.Read(&m_rc2DtBounds.m_dLeft,sizeof(double));
	idxFile.Read(&m_rc2DtBounds.m_dTop,sizeof(double));
	idxFile.Read(&m_rc2DtBounds.m_dRight,sizeof(double));
	idxFile.Read(&m_rc2DtBounds.m_dBottom,sizeof(double));
	
	for(int i=0;i<m_nRecCount;i++)
	{
		CGeoInfo info;
		idxFile.Read(&info.nPos,sizeof(int));
		idxFile.Read(&info.rc2Bounds.m_dLeft,sizeof(double));
		idxFile.Read(&info.rc2Bounds.m_dTop,sizeof(double));
		idxFile.Read(&info.rc2Bounds.m_dRight,sizeof(double));
		idxFile.Read(&info.rc2Bounds.m_dBottom,sizeof(double));
		m_geoInfos.Add(info);
	}
	
	mFile.Close();
	idxFile.Close();
	
	
	m_ptDtCenter = m_rc2DtBounds.CenterPoint();
	
	((CMainFrame *)AfxGetMainWnd())->GetActiveView()->GetClientRect(&m_rcViewBounds);
	
	double dXRatio = m_rc2DtBounds.Width()/m_rcViewBounds.Width();
	double dYRatio = m_rc2DtBounds.Height()/m_rcViewBounds.Height();
	
	m_dScale = dXRatio>dYRatio ? dXRatio : dYRatio;
	
	m_dZoomRatio = 1.0;
	
	
	
	AfxGetMainWnd()->GetActiveWindow()->Invalidate();
	return TRUE;
}

void CZMapApp::OnFileOpen() 
{
	CFileDialogEx dlgFileSdb(true, "txt", NULL,
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, 
		"地图文件 (*.geo)|*.geo|所有文件 (*.*)|*.*||" );
	dlgFileSdb.m_ofn.lpstrFile = new TCHAR[65535];
	memset(dlgFileSdb.m_ofn.lpstrFile, 0, 65535);
	dlgFileSdb.m_ofn.nMaxFile = 65535;
	CString strFileName;
	if( dlgFileSdb.DoModal() == IDOK )
	{
		POSITION pos = dlgFileSdb.GetStartPosition();
		CString strPathName(_T(""));
		long nItemCount = 0;
		while( pos )
		{
			strPathName = dlgFileSdb.GetNextPathName( pos );
			strFileName = strPathName;
		}
	}
	delete dlgFileSdb.m_ofn.lpstrFile;
	dlgFileSdb.m_ofn.lpstrFile = NULL;
	m_strFileName = strFileName;
	
	
	m_nRecCount = 0;
	m_dScale = 1.0;
	m_dZoomRatio = 1.0;
	m_geoInfos.SetSize(0,10);
	
	ReadFileInfo();
}

⌨️ 快捷键说明

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