📄 savemap.cpp
字号:
// SaveMap.cpp : Implementation of CSaveMap
#include "stdafx.h"
#include "SaveMap.h"
/////////////////////////////////////////////////////////////////////////////
// CSaveMap
LRESULT CSaveMap::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
// Center the dialog on the screen
CenterWindow();
// Initialize the application
if (CheckOutLicenses(esriLicenseProductCodeEngine) != esriLicenseCheckedOut)
if (CheckOutLicenses(esriLicenseProductCodeArcView) != esriLicenseCheckedOut)
if (CheckOutLicenses(esriLicenseProductCodeArcEditor) != esriLicenseCheckedOut)
if (CheckOutLicenses(esriLicenseProductCodeArcInfo) != esriLicenseCheckedOut)
{
MessageBox(_T("The initialization failed. This application cannot run"), _T("Initialization error"));
return EndDialog(0);
}
CComVariant varTool;
long itemIndex;
// Get the IPageLayoutControl interface from the PageLayout control
GetDlgControl(IDC_PAGELAYOUTCONTROL1, IID_IPageLayoutControl, (void **) &m_ipPLControl);
// Get the ITOCControltControl interface from the TOC control
GetDlgControl(IDC_TOCCONTROL1, IID_ITOCControl, (void **) &m_ipTOCControl);
// Get the IToolBarControl interface from the ToolBar control
GetDlgControl(IDC_TOOLBARCONTROL1, IID_IToolbarControl, (void **) &m_ipToolBarControl);
// Add toolbar definitions to the ToolBarControl
varTool = "esriControlCommands.ControlsPageLayoutToolbar";
m_ipToolBarControl->AddItem(varTool, 0, -1, VARIANT_FALSE, 0, esriCommandStyleIconOnly, &itemIndex);
varTool = "esriControlCommands.ControlsGraphicElementToolbar";
m_ipToolBarControl->AddItem(varTool, 0, -1, VARIANT_TRUE, 0, esriCommandStyleIconOnly, &itemIndex);
// Set buddy control
m_ipToolBarControl->SetBuddyControl(m_ipPLControl);
m_ipTOCControl->SetBuddyControl(m_ipPLControl);
// Disable command buttons
HWND hwndButton = GetDlgItem(IDC_SAVEAS);
::EnableWindow(hwndButton, FALSE);
hwndButton = GetDlgItem(IDC_SAVE);
::EnableWindow(hwndButton, FALSE);
return TRUE; // Let the system set the focus
}
LRESULT CSaveMap::OnLoadMap(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// Open a file dialog for selecting map documents
ICommonDialogPtr ipCommonDialog;
GetDlgControl(IDC_COMMONOPEN, IID_ICommonDialog, (void **) &ipCommonDialog);
ipCommonDialog->put_DialogTitle(CComBSTR("Open Map Document"));
ipCommonDialog->put_Filter(CComBSTR("Map Documents (*.mxd) | *.mxd"));
ipCommonDialog->ShowOpen();
// Exit if no map document is selected
ipCommonDialog->get_FileName(&m_bstrFileName);
if (SysStringLen(m_bstrFileName) == 0) return 0;
VARIANT_BOOL bIsMapDoc;
// Check whether a file is a proper map document
m_ipPLControl->CheckMxFile(m_bstrFileName, &bIsMapDoc);
if (VARIANT_TRUE == bIsMapDoc)
{
// Wait cursor
m_ipPLControl->put_MousePointer(esriPointerHourglass);
Open(m_bstrFileName);
// Enable command buttons
HWND hwndButton = GetDlgItem(IDC_SAVEAS);
::EnableWindow(hwndButton, TRUE);
hwndButton = GetDlgItem(IDC_SAVE);
::EnableWindow(hwndButton, TRUE);
// Default cursor
m_ipPLControl->put_MousePointer(esriPointerDefault);
}
else
MessageBox(_T("The specified file is not a valid ArcMap document"), _T("Not a valid map document"));
return 0;
}
LRESULT CSaveMap::OnSave(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
Save();
return 0;
}
LRESULT CSaveMap::OnSaveAs(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
// Open a file dialog for saving map documents
ICommonDialogPtr ipCommonDialogSave;
GetDlgControl(IDC_COMMONSAVE, IID_ICommonDialog, (void **) &ipCommonDialogSave);
ipCommonDialogSave->put_DialogTitle(CComBSTR("Save Map Document As"));
ipCommonDialogSave->put_Filter(CComBSTR("Map Documents (*.mxd) | *.mxd"));
ipCommonDialogSave->ShowSave();
// Exit if no map document is selected
ipCommonDialogSave->get_FileName(&m_bstrFileName);
if (SysStringLen(m_bstrFileName) == 0) return 0;
BSTR bstrDocument;
m_ipMapDoc->get_DocumentFilename(&bstrDocument);
if (_tcscmp(OLE2T(m_bstrFileName), OLE2T(bstrDocument)) == 0)
{
// Save changes to the current document
Save();
}
else
{
// Wait cursor
m_ipPLControl->put_MousePointer(esriPointerHourglass);
// SaveAs a new document with relative paths
m_ipMapDoc->SaveAs(m_bstrFileName, VARIANT_TRUE, VARIANT_TRUE);
// Open Document
Open(m_bstrFileName);
MessageBox(_T("Document saved successfully"), _T("Saved successfully"));
// Default cursor
m_ipPLControl->put_MousePointer(esriPointerDefault);
}
return 0;
}
LRESULT CSaveMap::Open(BSTR bstrFileName)
{
m_ipMapDoc.CreateInstance(CLSID_MapDocument);
IPageLayoutPtr ipPageLayout;
// Load Mx document
m_ipMapDoc->Open(m_bstrFileName, CComBSTR(L""));
m_ipMapDoc->get_PageLayout(&ipPageLayout);
m_ipPLControl->putref_PageLayout(ipPageLayout);
m_szFileName = OLE2T(m_bstrFileName);
SetDlgItemText(IDC_PATH, m_szFileName);
return 0;
}
LRESULT CSaveMap::Save()
{
VARIANT_BOOL bReadOnly;
// Check that the document is not read only
m_ipMapDoc->get_IsReadOnly(m_bstrFileName, &bReadOnly);
if (VARIANT_TRUE == bReadOnly)
{
MessageBox(_T("This map document is read only!"), _T("Cannot save a map"));
return 0;
}
VARIANT_BOOL bRelativePath;
m_ipMapDoc->get_UsesRelativePaths(&bRelativePath);
// Wait cursor
m_ipPLControl->put_MousePointer(esriPointerHourglass);
// Save with current relative path setting
m_ipMapDoc->Save(bRelativePath, VARIANT_TRUE);
// Default cursor
m_ipPLControl->put_MousePointer(esriPointerDefault);
MessageBox(_T("Changes saved successfully!"), _T("Saved successfully"));
return 0;
}
LRESULT CSaveMap::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
DestroyWindow(); // Destroy this window
PostQuitMessage(0);
return 0;
}
esriLicenseStatus CSaveMap::CheckOutLicenses(esriLicenseProductCode productCode)
{
// Create a new AoInitialize object if necessary
if (m_ipAoInitialize == 0)
{
HRESULT hr = m_ipAoInitialize.CreateInstance(CLSID_AoInitialize);
if (FAILED(hr))
return esriLicenseUnavailable;
}
// Determine if the product is available
esriLicenseStatus licenseStatus;
m_ipAoInitialize->IsProductCodeAvailable(esriLicenseProductCodeEngine, &licenseStatus);
if (esriLicenseAvailable == licenseStatus)
{
m_ipAoInitialize->Initialize(esriLicenseProductCodeEngine, &licenseStatus);
if (esriLicenseFailure == licenseStatus)
return esriLicenseUnavailable;
}
return licenseStatus;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -