📄 mapxsampview.cpp
字号:
// mapxsampView.cpp : implementation of the CMapxSampleView class
//
/*
* This sample application and corresponding sample code is provided
* for example purposes only. It has not undergone rigorous testing
* and as such should not be shipped as part of a final application
* without extensive testing on the part of the organization releasing
* the end-user product.
*/
#include "stdafx.h"
#include "mapxsamp.h"
#include "MainFrm.h"
#include "mapxsampDoc.h"
#include "mapxsampView.h"
#include "InfoDlg.h"
#include "AddThemeDlg.h"
#include "ResolveDatabind.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define IDC_MAP 100
#define MYTOOL_INFO 1
#define MYTOOL_DISTANCE 2
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView
IMPLEMENT_DYNCREATE(CMapxSampleView, CView)
BEGIN_MESSAGE_MAP(CMapxSampleView, CView)
//{{AFX_MSG_MAP(CMapxSampleView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(ID_MAP_ADDDATA, OnMapAdddata)
ON_COMMAND(ID_MAP_ADDTHEME, OnMapAddtheme)
ON_COMMAND(ID_VIEW_PROPERTIES, OnViewProperties)
ON_COMMAND(ID_MAP_ADDMULTIVARTHEME, OnMapAddmultivartheme)
ON_WM_SETFOCUS()
ON_COMMAND(ID_MAP_TOOL_ARROW, OnMapToolArrow)
ON_COMMAND(ID_MAP_TOOL_CENTER, OnMapToolCenter)
ON_COMMAND(ID_MAP_TOOL_PAN, OnMapToolPan)
ON_COMMAND(ID_MAP_TOOL_RADIUSSELECT, OnMapToolRadiusselect)
ON_COMMAND(ID_MAP_TOOL_RECTANGLESELECT, OnMapToolRectangleselect)
ON_COMMAND(ID_MAP_TOOL_SELECT, OnMapToolSelect)
ON_COMMAND(ID_MAP_TOOL_ZOOMOUT, OnMapToolZoomout)
ON_COMMAND(ID_MAP_TOOL_ZOOMIN, OnMapToolZoomin)
ON_COMMAND(ID_CONTEXT_PREVIOUSVIEW, OnContextPreviousview)
ON_UPDATE_COMMAND_UI(ID_CONTEXT_PREVIOUSVIEW, OnUpdateContextPreviousview)
ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_VIEW_VIEWENTIREMAP, OnViewViewentiremap)
ON_COMMAND(ID_VIEW_LAYERCONTROL, OnViewLayercontrol)
ON_UPDATE_COMMAND_UI(ID_VIEW_SCALEBAR, OnUpdateViewScalebar)
ON_COMMAND(ID_VIEW_SCALEBAR, OnViewScalebar)
ON_COMMAND(ID_MAP_ADDDAODATA, OnMapAdddaodata)
ON_COMMAND(ID_MAP_TOOL_DISTANCETOOL, OnMapToolDistancetool)
ON_COMMAND(ID_MAP_TOOL_INFOTOOL, OnMapToolInfotool)
ON_COMMAND(ID_MAP_TOOL_POLYGONSELECT, OnMapToolPolygonselect)
ON_COMMAND(ID_ODBC_NORMAL_AUTO, OnOdbcNormalAuto)
ON_COMMAND(ID_ODBC_NORMAL_FIELDS, OnOdbcNormalFields)
ON_COMMAND(ID_ODBC_XYBIND, OnOdbcXybind)
ON_COMMAND(ID_ODBC_ZIPCODE, OnOdbcZipcode)
ON_COMMAND(ID_ODBC_DYNAMIC, OnOdbcDynamic)
ON_UPDATE_COMMAND_UI(ID_ODBC_DYNAMIC, OnUpdateOdbcDynamic)
ON_UPDATE_COMMAND_UI(ID_ODBC_XYBIND, OnUpdateOdbcXybind)
ON_UPDATE_COMMAND_UI(ID_ODBC_ZIPCODE, OnUpdateOdbcZipcode)
ON_COMMAND(ID_MAP_ADDUNBOUNDDATA, OnMapAddunbounddata)
ON_COMMAND(ID_VIEW_PROJECTION, OnViewProjection)
ON_COMMAND(ID_DATASEST_FROM_LAYER, OnDatasestFromLayer)
ON_COMMAND(ID_MAP_REMOVEALLDATASETS, OnMapRemovealldatasets)
ON_UPDATE_COMMAND_UI(ID_MAP_REMOVEALLDATASETS, OnUpdateMapRemovealldatasets)
ON_COMMAND(ID_MAP_ADDSAFEARRAYDATASET, OnMapAddsafearraydataset)
//}}AFX_MSG_MAP
//#if (_MFC_VER >= 0x0421)
// ON_WM_MOUSEWHEEL()
//#endif
// 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)
END_MESSAGE_MAP()
// you can use the dispid constants from mapx.h
BEGIN_EVENTSINK_MAP(CMapxSampleView, CView)
ON_EVENT(CMapxSampleView, IDC_MAP, DISPID_MOUSEMOVE, OnMouseMoveInMap, VTS_I2 VTS_I2 VTS_XPOS_PIXELS VTS_YPOS_PIXELS)
ON_EVENT(CMapxSampleView, IDC_MAP, 11 /* PolyToolUsed */, OnPolyToolUsedMap, VTS_I2 VTS_I4 VTS_DISPATCH VTS_BOOL VTS_BOOL VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, MAPX_DISPID_TOOLUSED, OnToolUsed, VTS_I2 VTS_R8 VTS_R8 VTS_R8 VTS_R8 VTS_R8 VTS_BOOL VTS_BOOL VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, -600 /* Click */, OnClickMap, VTS_NONE)
ON_EVENT(CMapxSampleView, IDC_MAP, -601 /* DblClick */, OnDblClickMap, VTS_NONE)
ON_EVENT(CMapxSampleView, IDC_MAP, -608 /* Error */, OnErrorMap, VTS_I2 VTS_PBSTR VTS_I4 VTS_BSTR VTS_BSTR VTS_I4 VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, -602 /* KeyDown */, OnKeyDownMap, VTS_PI2 VTS_I2)
ON_EVENT(CMapxSampleView, IDC_MAP, -603 /* KeyPress */, OnKeyPressMap, VTS_PI2)
ON_EVENT(CMapxSampleView, IDC_MAP, -604 /* KeyUp */, OnKeyUpMap, VTS_PI2 VTS_I2)
ON_EVENT(CMapxSampleView, IDC_MAP, -605 /* MouseDown */, OnMouseDownMap, VTS_I2 VTS_I2 VTS_R4 VTS_R4)
ON_EVENT(CMapxSampleView, IDC_MAP, DISPID_MOUSEUP, OnMouseUpInMap, VTS_I2 VTS_I2 VTS_XPOS_PIXELS VTS_YPOS_PIXELS)
ON_EVENT(CMapxSampleView, IDC_MAP, 1 /* SelectionChanged */, OnSelectionChangedMap, VTS_NONE)
ON_EVENT(CMapxSampleView, IDC_MAP, 2 /* ResolveDataBind */, OnResolveDataBindMap, VTS_I2 VTS_I2 VTS_VARIANT VTS_PI2 VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, 4 /* RequestData */, OnRequestDataMap, VTS_BSTR VTS_I4 VTS_I2 VTS_PVARIANT VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, 5 /* DataMismatch */, OnDataMismatchMap, VTS_BSTR VTS_I4 VTS_PBSTR)
ON_EVENT(CMapxSampleView, IDC_MAP, MAPX_DISPID_MAPVIEWCHANGED, OnMapViewChanged, VTS_NONE)
ON_EVENT(CMapxSampleView, IDC_MAP, 7 /* AnnotationAdded */, OnAnnotationAddedMap, VTS_DISPATCH)
ON_EVENT(CMapxSampleView, IDC_MAP, 8 /* AnnotationChanged */, OnAnnotationChangedMap, VTS_I2 VTS_DISPATCH VTS_PBOOL)
ON_EVENT(CMapxSampleView, IDC_MAP, MAPX_DISPID_THEMEMODIFYREQUESTED, OnThemeModifyRequested, VTS_DISPATCH)
ON_EVENT(CMapxSampleView, IDC_MAP, MAPX_DISPID_DRAWUSERLAYER, OnDrawUserLayer, VTS_DISPATCH VTS_I4 VTS_I4 VTS_DISPATCH VTS_DISPATCH)
END_EVENTSINK_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView construction/destruction
CMapxSampleView::CMapxSampleView()
{
m_dPrevZoom = 0;
m_dCurZoom = 0;
m_bScaleBar = FALSE;
m_bODBCDynamic = FALSE;
}
CMapxSampleView::~CMapxSampleView()
{
if (m_daoUSCust.IsOpen()) {
m_daoUSCust.Close();
}
}
void CMapxSampleView::NotifyPaletteChanged(CWnd* pFocusWnd)
{
// we don't care, but our embedded control might
m_ctrlMapX.SendMessage(WM_PALETTECHANGED, (WPARAM)(pFocusWnd->m_hWnd));
}
void CMapxSampleView::NotifyQueryNewPalette()
{
// we don't care, but our embedded control might
m_ctrlMapX.SendMessage(WM_QUERYNEWPALETTE);
}
BOOL CMapxSampleView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView drawing
void CMapxSampleView::OnDraw(CDC* pDC)
{
CMapxSampleDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView printing
BOOL CMapxSampleView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CMapxSampleView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CMapxSampleView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView diagnostics
#ifdef _DEBUG
void CMapxSampleView::AssertValid() const
{
CView::AssertValid();
}
void CMapxSampleView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMapxSampleDoc* CMapxSampleView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMapxSampleDoc)));
return (CMapxSampleDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMapxSampleView message handlers
int CMapxSampleView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// create map with default size
// resize message will cause it to be
// size to the client area of the view
m_ctrlMapX.Create(NULL, WS_VISIBLE, CRect(0,0,100,100), this, IDC_MAP);
// create custom distance tool and info tool
try {
// create some custom tools for object drawing
m_ctrlMapX.CreateCustomTool(MYTOOL_DISTANCE, miToolTypePoly, miCrossCursor);
m_ctrlMapX.CreateCustomTool(MYTOOL_INFO, miToolTypePoint, miCrossCursor);
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
// toggle scalebar on
OnViewScalebar();
return 0;
}
// resize the map to be the same size as our client area
void CMapxSampleView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
if (cx != 0 && cy != 0)
m_ctrlMapX.MoveWindow(0,0,cx,cy,TRUE);
}
// get your data from wherever you like
// this is an example of the format in needs to be in
// quotes around strings, tabs between fields, cr lf at end of record
// note that this is only one of several dataset types that are supported
/* "\"NY\"\t105.34\t100\t1\r\n"
"\"MA\"\t245.19\t200\t2\r\n"
"\"NY\"\t195.0\t300\t3\r\n"
"\"AK\"\t195.0\t125\t4\r\n"
"\"CA\"\t56.453\t200\t5\r\n";
*/
static char BASED_CODE szDataFilter[] = "Map Data Files (*.txt)|*.txt|All Files (*.*)|*.*||";
// get the filename from the file open dialog
// the file must be in the format described above
void CMapxSampleView::OnMapAdddata()
{
HGLOBAL hGlobalData=NULL;
char *psz=NULL;
CFileDialog dlgFile(TRUE, "*.txt", NULL, 0, szDataFilter, this);
if (dlgFile.DoModal() == IDCANCEL)
return;
// read file into a string, and copy it to a global
// memory buffer
CFile fileData(dlgFile.GetPathName(), CFile::modeRead);
CString strBuffer;
fileData.Read(strBuffer.GetBuffer(fileData.GetLength() + 1), fileData.GetLength());
strBuffer.ReleaseBuffer(fileData.GetLength() + 1);
fileData.Close();
hGlobalData = GlobalAlloc(GMEM_MOVEABLE, strBuffer.GetLength()+1);
psz=(char *)GlobalLock(hGlobalData);
strcpy(psz, strBuffer);
GlobalUnlock(hGlobalData);
short Type;
VARIANT SourceData;
VARIANT Name;
VARIANT GeoField;
VARIANT SecondaryGeoField;
VARIANT BindLayerName;
VARIANT Fields;
CString strName= "TestData";
// set up optional parameters
// will not be used
SecondaryGeoField.vt = VT_ERROR;
SecondaryGeoField.scode = DISP_E_PARAMNOTFOUND;
// let mapx auto detect geofield
GeoField.vt = VT_ERROR;
GeoField.scode = DISP_E_PARAMNOTFOUND;
// let mapx find which layer to bind to
BindLayerName.vt = VT_ERROR;
BindLayerName.scode = DISP_E_PARAMNOTFOUND;
// use all fields with defaults
Fields.vt = VT_ERROR;
Fields.scode = DISP_E_PARAMNOTFOUND;
// set the name of our dataset
Name.vt = VT_BSTR;
Name.bstrVal = strName.AllocSysString();
// set up source data - no error checking on alloc
Type = miDataSetGlobalHandle;
SourceData.vt = VT_I4;
SourceData.lVal = (long)hGlobalData;
try {
// now add the dataset to the datasets collection
CMapXDataset ds = m_ctrlMapX.GetDatasets().Add(Type, SourceData, Name, GeoField, SecondaryGeoField, BindLayerName, Fields, COptionalVariant());
// zoom out to see any new layers that are added
//OnViewViewentiremap();
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
}
void CMapxSampleView::OnMapAddtheme()
{
CAddThemeDlg dlgTheme;
dlgTheme.m_pMapX = &m_ctrlMapX;
dlgTheme.DoModal();
}
// you could use this to display the lat/long values on the status bar
BOOL CMapxSampleView::OnMouseMoveInMap(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y)
{
return TRUE;
}
// if right mouse button, display the context menu
BOOL CMapxSampleView::OnMouseUpInMap(short Button, short Shift, OLE_XPOS_PIXELS x, OLE_YPOS_PIXELS y)
{
if (Button == 2) {
CMenu menu; // top-level menu
CMenu *pMenu=NULL; // pop-up menu
// Load the menu resource.
menu.LoadMenu(IDR_CONTEXTMENU);
// TrackPopupMenu cannot display the top-level menu, so get
// the handle of the first pop-up menu.
pMenu = menu.GetSubMenu(0);
if (!pMenu)
return TRUE;
SetMenuDefaultItem(pMenu->m_hMenu, ID_VIEW_PROPERTIES, FALSE);
// Display the floating pop-up menu. Track the right mouse
// button on the assumption that this function is called
// during WM_CONTEXTMENU processing.
POINT pt;
GetCursorPos(&pt);
pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, pt.x, pt.y, this, NULL);
// Destroy the menu.
menu.DestroyMenu();
}
return TRUE;
}
void CMapxSampleView::OnViewProperties()
{
// easest way to bring up property page
try {
m_ctrlMapX.PropertyPage();
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
// create a multi-var theme on the first dataset
// assumes that columns 3,4,5 exist, and are numeric
void CMapxSampleView::OnMapAddmultivartheme()
{
VARIANT Type;
VARIANT Name;
VARIANT Field;
VARIANT Item;
long FieldNums[3] = { 3, 4, 5}; // theme field numbers
int n=3;
Type.vt = VT_I4;
Type.lVal = miThemeBarChart; //auto choose theme type
Name.vt = VT_ERROR; // use default name
Name.scode = DISP_E_PARAMNOTFOUND;
Item.vt = VT_I4; // for getting first dataset
Item.lVal = 1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -