📄 simplemapviewer.cpp
字号:
// SimpleMapViewer.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include <objbase.h>
#include <commdlg.h>
enum CurrentTool
{
currentToolZoomIn,
currentToolZoomOut,
currentToolPan,
};
IMapPtr ipMap;
ITrackCancelPtr ipCancelTracker;
// Cursors
HCURSOR hCursorZoomIn;
HCURSOR hCursorZoomInMove;
HCURSOR hCursorZoomOut;
HCURSOR hCursorZoomOutMove;
HCURSOR hCursorPan;
HCURSOR hCursorPanDrag;
CurrentTool g_currentTool = currentToolZoomIn;
BOOL SelectShapeFile(HWND hWnd, TCHAR** pszDirectory, TCHAR** pszFiles)
{
OPENFILENAME ofn; // common dialog box structure
TCHAR szFile[MAX_PATH] = ("\0"); // buffer for file name
TCHAR defaultDir[MAX_PATH];
TCHAR* title = "Select Shape files";
DWORD len = MAX_PATH;
// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFilter = ("SHP\0*.shp\0");
ofn.hwndOwner = (HWND) hWnd;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_ALLOWMULTISELECT | OFN_EXPLORER;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFile = szFile;
ofn.lpstrTitle = title;
ofn.lpstrInitialDir = defaultDir;
ofn.nFilterIndex = 1;
// Display the Open dialog box.
if (::GetOpenFileName(&ofn))
{
*pszFiles = (char *) malloc(MAX_PATH);
*pszDirectory = (char *) malloc(ofn.nFileOffset + 1);
strncpy(*pszDirectory, ofn.lpstrFile, ofn.nFileOffset);
(*pszDirectory)[ofn.nFileOffset] = '\0';
memcpy(*pszFiles, (ofn.lpstrFile)+ofn.nFileOffset, ofn.nMaxFile-ofn.nFileOffset);
}
else
return false;
return true;
}
HRESULT AddClassToMap(IMap* pMap, IFeatureClass* pFeatureClass)
{
IFeatureLayerPtr ipFeatureLayer(CLSID_FeatureLayer);
HRESULT hr = ipFeatureLayer->putref_FeatureClass(pFeatureClass);
if (FAILED(hr)) return hr;
hr = ipFeatureLayer->put_Visible(VARIANT_FALSE);
if (FAILED(hr)) return hr;
CComBSTR bstrName;
hr = pFeatureClass->get_AliasName(&bstrName);
if (FAILED(hr)) return hr;
hr = ipFeatureLayer->put_Name(bstrName);
if (FAILED(hr)) return hr;
hr = pMap->AddLayer((ILayerPtr) ipFeatureLayer);
if (FAILED(hr)) return hr;
hr = ipFeatureLayer->put_Visible(VARIANT_TRUE);
if (FAILED(hr)) return hr;
return S_OK;
}
HRESULT AddClassesToMap(IMap* pMap, ISet* pSet)
{
HRESULT hr = pSet->Reset();
if (FAILED(hr)) return hr;
//First get all the Envelope and Polygon Layers
IUnknownPtr ipUnknown;
IFeatureClassPtr ipFeatureClass;
esriGeometryType eGeometryType;
while (pSet->Next(&ipUnknown) == S_OK)
{
ipFeatureClass = ipUnknown;
hr = ipFeatureClass->get_ShapeType(&eGeometryType);
if (FAILED(hr)) return hr;
if ((eGeometryType == esriGeometryPolygon) || (eGeometryType == esriGeometryEnvelope))
{
hr = AddClassToMap(pMap, ipFeatureClass);
if (FAILED(hr)) return hr;
}
ipFeatureClass = 0;
}
// Next get the linear layers
hr = pSet->Reset();
while (pSet->Next(&ipUnknown) == S_OK)
{
ipFeatureClass = ipUnknown;
hr = ipFeatureClass->get_ShapeType(&eGeometryType);
if (FAILED(hr)) return hr;
if ((eGeometryType == esriGeometryLine) ||
(eGeometryType == esriGeometryPath) ||
(eGeometryType == esriGeometryBezier3Curve) ||
(eGeometryType == esriGeometryCircularArc) ||
(eGeometryType == esriGeometryEllipticArc) ||
(eGeometryType == esriGeometryPolyline))
{
hr = AddClassToMap(pMap, ipFeatureClass);
if (FAILED(hr)) return hr;
}
ipFeatureClass = 0;
}
// Finally get the point layers
hr = pSet->Reset();
while (pSet->Next(&ipUnknown) == S_OK)
{
ipFeatureClass = ipUnknown;
hr = ipFeatureClass->get_ShapeType(&eGeometryType);
if (FAILED(hr)) return hr;
if ((eGeometryType == esriGeometryMultipoint) ||
(eGeometryType == esriGeometryPoint))
{
hr = AddClassToMap(pMap, ipFeatureClass);
if (FAILED(hr)) return hr;
}
ipFeatureClass = 0;
}
// Cleanup the set
hr = pSet->RemoveAll();
if (FAILED(hr)) return hr;
return S_OK;
}
HRESULT AddShapeFiles(IMap* pMap, HWND hWnd)
{
HRESULT hr;
IWorkspaceFactoryPtr ipWorkspaceFactory(CLSID_ShapefileWorkspaceFactory);
IFeatureWorkspacePtr ipFeatureWorkspace;
// Build a set to hold the classes as we open them
ISetPtr ipSet(CLSID_Set);
// Select the Files to load
char* pszFiles;
char* pszDirectory;
if (SelectShapeFile(hWnd, &pszDirectory, &pszFiles))
{
BOOL bGotDir = false;
IWorkspacePtr ipWorkspace;
// Open up the Shapefile workspace factory using the selected directory
hr = ipWorkspaceFactory->OpenFromFile(CComBSTR(pszDirectory), (OLE_HANDLE) hWnd, &ipWorkspace);
if (FAILED(hr)) return hr;
ipFeatureWorkspace = ipWorkspace;
char* temp = pszFiles;
while (strcmp(temp, "") != 0)
{
// Loop through each of the shape files and open them
IFeatureClassPtr ipFeatureClass;
hr = ipFeatureWorkspace->OpenFeatureClass(CComBSTR(temp), &ipFeatureClass);
if (FAILED(hr)) return hr;
// Add the featue class to the set so we can traverse them later
hr = ipSet->Add((IUnknownPtr) ipFeatureClass);
if (FAILED(hr)) return hr;
int iLength = ::strlen(temp);
temp += iLength + 1;
}
// Add the Feature classes to the map by building layers foreach class in this function
hr = AddClassesToMap(pMap, ipSet);
if (FAILED(hr)) return hr;
// Get the extents of the data and set the ActiveView to show the data at its full extent
IActiveViewPtr ipActiveView(pMap);
IEnvelopePtr ipEnv;
hr = ipActiveView->get_FullExtent(&ipEnv);
if (FAILED(hr)) return hr;
hr = ipActiveView->put_Extent(ipEnv);
if (FAILED(hr)) return hr;
hr = ipActiveView->Refresh();
if (FAILED(hr)) return hr;
}
return S_OK;
}
HRESULT AddData(IMap* pMap, HWND hWnd)
{
// Use the GxDialog to select the data to add to the map
IGxDialogPtr ipGxDialog(CLSID_GxDialog);
ISetPtr ipSet(CLSID_Set);
// Set the Filter to FeatureDatasets and FeatureClasses
IGxObjectFilterPtr ipGxObjectFilter(CLSID_GxFilterFeatureDatasetsAndFeatureClasses);
HRESULT hr = ipGxDialog->putref_ObjectFilter(ipGxObjectFilter);
if (FAILED(hr)) return hr;
hr = ipGxDialog->put_AllowMultiSelect(VARIANT_TRUE);
if (FAILED(hr)) return hr;
hr = ipGxDialog->put_Title(CComBSTR("Select Data"));
if (FAILED(hr)) return hr;
IEnumGxObjectPtr ipEnumGxObject;
VARIANT_BOOL bResult;
hr = ipGxDialog->DoModalOpen((OLE_HANDLE) hWnd, &ipEnumGxObject, &bResult);
if (FAILED(hr)) return hr;
if (bResult == VARIANT_FALSE)
return S_OK;
hr = ipEnumGxObject->Reset();
if (FAILED(hr)) return hr;
// Enumerate through the GxObjects
IGxObjectPtr ipGxObject;
while (ipEnumGxObject->Next(&ipGxObject) == S_OK)
{
IGxDatasetPtr ipGxDataset(ipGxObject);
if (ipGxDataset != 0)
{
// If the GxObject contains is a esriDTFeatureClass add the class to the set
esriDatasetType eDatasetType;
hr = ipGxDataset->get_Type(&eDatasetType);
if (FAILED(hr)) return hr;
if (eDatasetType == esriDTFeatureClass)
{
IDatasetPtr ipDataset;
hr = ipGxDataset->get_Dataset(&ipDataset);
if (FAILED(hr)) return hr;
hr = ipSet->Add((IUnknownPtr) ipDataset);
if (FAILED(hr)) return hr;
}
else if (eDatasetType == esriDTFeatureDataset)
{
// If the OxObject is a DTFeatureDataset get the feature classes
// from add them to the set
IDatasetPtr ipDataset;
hr = ipGxDataset->get_Dataset(&ipDataset);
if (FAILED(hr)) return hr;
IFeatureClassContainerPtr ipFCCont(ipDataset);
long lNumClasses;
hr = ipFCCont->get_ClassCount(&lNumClasses);
if (FAILED(hr)) return hr;
IFeatureClassPtr ipFeatureClass;
for (long i = 0; i < lNumClasses; i++)
{
hr = ipFCCont->get_Class(i, &ipFeatureClass);
if (FAILED(hr)) return hr;
hr = ipSet->Add((IUnknownPtr) ipFeatureClass);
if (FAILED(hr)) return hr;
}
}
}
}
// Add all the feature classes to the Map
hr = AddClassesToMap(pMap, ipSet);
if (FAILED(hr)) return hr;
// Update the Map view
IActiveViewPtr ipActiveView(pMap);
IEnvelopePtr ipEnv;
hr = ipActiveView->get_FullExtent(&ipEnv);
if (FAILED(hr)) return hr;
hr = ipActiveView->put_Extent(ipEnv);
if (FAILED(hr)) return hr;
hr = ipActiveView->Refresh();
if (FAILED(hr)) return hr;
return S_OK;
}
HRESULT RemoveAllData(IMap* pMap)
{
HRESULT hr;
// Clear out the Layers from the Map
hr = pMap->ClearLayers();
if (FAILED(hr)) return hr;
IStandaloneTableCollectionPtr ipTables(pMap);
if (ipTables)
{
hr = ipTables->RemoveAllStandaloneTables();
if (FAILED(hr)) return hr;
}
IActiveViewPtr ipActiveView(pMap);
hr = ipActiveView->Refresh();
if (FAILED(hr)) return hr;
return S_OK;
}
void PaintDialog(HDC hDC, IMap* pMap, ITrackCancel* pTrackCancel)
{
if (pMap == 0)
return;
IActiveViewPtr ipActiveView(pMap);
ipActiveView->Draw((OLE_HANDLE) hDC, 0);
pTrackCancel->Reset();
}
HRESULT ToolEventMouseDown(HWND hWndPicBox, long X, long Y, IPoint** pPoint)
{
// Delete with the Mouse down event depending on which tool is active
IActiveViewPtr ipActiveView(ipMap);
IScreenDisplayPtr ipScreenDisplay;
HRESULT hr = ipActiveView->get_ScreenDisplay(&ipScreenDisplay);
if (FAILED(hr)) return hr;
IDisplayTransformationPtr ipDisplayTransformation;
hr = ipScreenDisplay->get_DisplayTransformation(&ipDisplayTransformation);
if (FAILED(hr)) return hr;
hr = ipDisplayTransformation->ToMapPoint(X, Y, pPoint);
if (FAILED(hr)) return hr;
if (g_currentTool == currentToolPan)
{
hr = ipScreenDisplay->PanStart(*pPoint);
if (FAILED(hr)) return hr;
}
return S_OK;
}
HRESULT ToolEventMouseUp(HWND hWndPicBox, long X, long Y, INewEnvelopeFeedback *pDisplayFeedback)
{
// Complete the action of the tool depending upon which tool is currently active
IActiveViewPtr ipActiveView(ipMap);
IScreenDisplayPtr ipScreenDisplay;
HRESULT hr = ipActiveView->get_ScreenDisplay(&ipScreenDisplay);
if (FAILED(hr)) return hr;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -