www.pudn.com > GeoExpert-6-1.rar > GeoExpertView.cpp
// GeoExpertView.cpp : implementation of the CGeoExpertView class
//
#include "stdafx.h"
#include "GeoExpert.h"
#include "GeoExpertDoc.h"
#include "GeoExpertView.h"
#include "MainFrm.h"
#define INFO_TOOL 102
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define MYTOOL_INFO 1
#define MYTOOL_DISTANCE 2
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView
IMPLEMENT_DYNCREATE(CGeoExpertView, CView)
BEGIN_MESSAGE_MAP(CGeoExpertView, CView)
//{{AFX_MSG_MAP(CGeoExpertView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_SETFOCUS()
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_MAP_TOOL_ARROW, OnMapToolArrow)
ON_COMMAND(ID_MAP_TOOL_ZOOMIN, OnMapToolZoomin)
ON_COMMAND(ID_MAP_TOOL_ZOOMOUT, OnMapToolZoomout)
ON_COMMAND(ID_MAP_TOOL_PAN, OnMapToolPan)
ON_COMMAND(ID_MAP_TOOL_SELECT, OnMapToolSelect)
ON_COMMAND(ID_MAP_TOOL_RECTANGLESELECT, OnMapToolRectangleselect)
ON_COMMAND(ID_MAP_TOOL_RADIUSSELECT, OnMapToolRadiusselect)
ON_COMMAND(ID_MAP_TOOL_ENTIRE, OnMapToolEntire)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_ZOOMIN, OnUpdateMapToolZoomin)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_ZOOMOUT, OnUpdateMapToolZoomout)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_PAN, OnUpdateMapToolPan)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_ARROW, OnUpdateMapToolArrow)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_SELECT, OnUpdateMapToolSelect)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_RECTANGLESELECT, OnUpdateMapToolRectangleselect)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_RADIUSSELECT, OnUpdateMapToolRadiusselect)
ON_COMMAND(ID_MAP_TOOL_CENTER, OnMapToolCenter)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_CENTER, OnUpdateMapToolCenter)
ON_COMMAND(ID_VIEW_PROJECTION, OnViewProjection)
ON_COMMAND(ID_VIEW_PROPERTIES, OnViewProperties)
ON_COMMAND(ID_MAP_TOOL_INFOTOOL, OnMapToolInfotool)
ON_COMMAND(ID_MAP_TOOL_POLYGONSELECT, OnMapToolPolygonselect)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_POLYGONSELECT, OnUpdateMapToolPolygonselect)
ON_COMMAND(ID_VIEW_LAYERCONTROL, OnViewLayercontrol)
ON_UPDATE_COMMAND_UI(ID_MAP_TOOL_INFOTOOL, OnUpdateMapToolInfotool)
ON_COMMAND(ID_VIEW_TOOLTEST, OnViewTooltest)
//}}AFX_MSG_MAP
// 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()
BEGIN_EVENTSINK_MAP(CGeoExpertView, CView)
ON_EVENT(CGeoExpertView, IDC_MAP, MAPX_DISPID_TOOLUSED, OnInfoToolUsed, VTS_I2 VTS_R8 VTS_R8 VTS_R8 VTS_R8 VTS_R8 VTS_BOOL VTS_BOOL VTS_PBOOL)
END_EVENTSINK_MAP()
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView construction/destruction
CGeoExpertView::CGeoExpertView()
{
// TODO: add construction code here
}
CGeoExpertView::~CGeoExpertView()
{
}
BOOL CGeoExpertView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView drawing
void CGeoExpertView::OnDraw(CDC* pDC)
{
CGeoExpertDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
}
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView printing
BOOL CGeoExpertView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CGeoExpertView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CGeoExpertView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView diagnostics
#ifdef _DEBUG
void CGeoExpertView::AssertValid() const
{
CView::AssertValid();
}
void CGeoExpertView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CGeoExpertDoc* CGeoExpertView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CGeoExpertDoc)));
return (CGeoExpertDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CGeoExpertView message handlers
int CGeoExpertView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
RECT windRect;
GetClientRect(&windRect);
if (!m_ctrlMapX.Create(NULL, WS_VISIBLE, windRect, this,IDC_MAP))
return -1;
// 创建信息工具
m_ctrlMapX.CreateCustomTool(INFO_TOOL, miToolTypePoint, miCrossCursor);
return 0;
}
void CGeoExpertView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
// TODO: Add your message handler code here
//if (cx != 0 && cy != 0)
if (m_ctrlMapX.m_hWnd != NULL)
m_ctrlMapX.MoveWindow(0,0,cx,cy,TRUE);
}
void CGeoExpertView::OnSetFocus(CWnd* pOldWnd)
{
CView::OnSetFocus(pOldWnd);
// TODO: Add your message handler code here
m_ctrlMapX.SetFocus();
}
static char BASED_CODE szTabFilter[] = "MapInfo Map Files (*.gst)|*.gst|All Files (*.*)|*.*||";
void CGeoExpertView::OnFileOpen()
{
// TODO: Add your command handler code here
CFileDialog dlgFile(TRUE, "*.gst", NULL, OFN_HIDEREADONLY , szTabFilter, this);
dlgFile.m_ofn.lpstrTitle = "Open MapInfo Map";
if (dlgFile.DoModal() == IDCANCEL)
return;
m_strFilePath = dlgFile.GetPathName();
try {
// Close the existing set of map layers and load the Canada map
TRACE0("Old Geoset: " + m_ctrlMapX.GetGeoSet());
m_ctrlMapX.SetGeoSet(m_strFilePath);
((CMainFrame*)AfxGetApp()->GetMainWnd())->m_wndMyDialogBar.SetDlgItemText( IDC_EDIT_GEOSET_NAME, m_ctrlMapX.GetTitleText() );
m_ctrlMapX.SetTitleText("");
TRACE0("New Geoset: " + m_ctrlMapX.GetGeoSet());
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
void CGeoExpertView::OnMapToolArrow()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miArrowTool);
}
void CGeoExpertView::OnMapToolZoomin()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miZoomInTool);
}
void CGeoExpertView::OnMapToolZoomout()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miZoomOutTool);
}
void CGeoExpertView::OnMapToolPan()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miPanTool);
}
void CGeoExpertView::OnMapToolSelect()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miSelectTool);
}
void CGeoExpertView::OnMapToolRectangleselect()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miRectSelectTool);
}
void CGeoExpertView::OnMapToolRadiusselect()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miRadiusSelectTool);
}
void CGeoExpertView::OnMapToolPolygonselect()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miPolygonSelectTool);
}
void CGeoExpertView::OnMapToolCenter()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(miCenterTool);
}
void CGeoExpertView::OnMapToolEntire()
{
// TODO: Add your command handler code here
/*
try {
m_ctrlMapX.SetZoom(m_ctrlMapX.GetGeoSetWidth());
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
*/
m_ctrlMapX.SetGeoSet(m_strFilePath);
}
void CGeoExpertView::OnUpdateMapToolArrow(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miArrowTool);
}
void CGeoExpertView::OnUpdateMapToolZoomin(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miZoomInTool);
}
void CGeoExpertView::OnUpdateMapToolZoomout(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miZoomOutTool);
}
void CGeoExpertView::OnUpdateMapToolPan(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miPanTool);
}
void CGeoExpertView::OnUpdateMapToolSelect(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miSelectTool);
}
void CGeoExpertView::OnUpdateMapToolRectangleselect(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miRectSelectTool);
}
void CGeoExpertView::OnUpdateMapToolRadiusselect(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miRadiusSelectTool);
}
void CGeoExpertView::OnUpdateMapToolPolygonselect(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miPolygonSelectTool);
}
void CGeoExpertView::OnUpdateMapToolCenter(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == miCenterTool);
}
void CGeoExpertView::OnViewProjection()
{
// TODO: Add your command handler code here
try {
VARIANT vHelpFile, vHelpID; // mark as optional since we don't have a helpfile
vHelpFile.vt = VT_ERROR;
vHelpFile.scode = DISP_E_PARAMNOTFOUND;
vHelpID.vt = VT_ERROR;
vHelpID.scode = DISP_E_PARAMNOTFOUND;
CMapXCoordSys csys = m_ctrlMapX.GetDisplayCoordSys();
csys.PickCoordSys(vHelpFile, vHelpID);
m_ctrlMapX.SetNumericCoordSys(csys);
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
void CGeoExpertView::OnViewProperties()
{
// TODO: Add your command handler code here
try {
m_ctrlMapX.PropertyPage();
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
void CGeoExpertView::OnMapToolInfotool()
{
// TODO: Add your command handler code here
m_ctrlMapX.SetCurrentTool(INFO_TOOL);
if (!m_bInfoDlg)
{
m_InfoDlg = new CInfoDlg;
m_InfoDlg->Create(IDD_INFO_DLG,NULL);
m_InfoDlg->ShowWindow(SW_SHOW);
m_InfoDlg->OnInitDlg();
m_bInfoDlg = TRUE;
}
}
void CGeoExpertView::OnViewLayercontrol()
{
// TODO: Add your command handler code here
try {
VARIANT vHelpFile, vHelpID; // mark as optional since we don't have a helpfile
vHelpFile.vt = VT_ERROR;
vHelpFile.scode = DISP_E_PARAMNOTFOUND;
vHelpID.vt = VT_ERROR;
vHelpID.scode = DISP_E_PARAMNOTFOUND;
CMapXLayers layers = m_ctrlMapX.GetLayers();
layers.LayersDlg(vHelpFile, vHelpID);
}
catch (COleDispatchException *e) {
e->ReportError();
e->Delete();
}
catch (COleException *e) {
e->ReportError();
e->Delete();
}
}
void CGeoExpertView::OnInfoToolUsed(short ToolNum, double X1, double Y1, double X2, double Y2, double Distance, BOOL Shift, BOOL Ctrl, BOOL *EnableDefault)
{
if (ToolNum == INFO_TOOL)
{
int iCount = 0;
iCount = m_ctrlMapX.GetLayers().GetCount();
CMapXPoint pt; //This point will be the center of our search
CMapXFeatures fs; //This will hold all of the features within our search
CMapXFeature f; //This will hold the customer feature we are currently
CMapXLayer layer;
pt.CreateDispatch(pt.GetClsid()); //Creates a dispatch for the point
pt.Set(X1,Y1); //Sets the coordinates into the point
double dLayerZoomMax,dLayerZoomMin;
double dMapZoom;
dMapZoom = m_ctrlMapX.GetZoom();
for (int i = 1; i <= iCount; i++)
{
layer = m_ctrlMapX.GetLayers().Item(i);
dLayerZoomMax = layer.GetZoomMax();
dLayerZoomMin = layer.GetZoomMin();
if ((dLayerZoomMax >= dMapZoom && dLayerZoomMin <= dMapZoom) || (dLayerZoomMax == 0 && dLayerZoomMin == 0))
{
fs = m_ctrlMapX.GetLayers().Item(i).SearchAtPoint(pt);
if(fs.GetCount() != 0)
{
CString buffer;
CMapXDataset ds;
f = fs.Item(1);
layer = f.GetLayer();
COleVariant layerVt;
layerVt.vt = VT_DISPATCH;
layerVt.pdispVal = layer.m_lpDispatch;
layerVt.pdispVal->AddRef();
ds = m_ctrlMapX.GetDatasets().Add(miDataSetLayer, layerVt);
m_InfoDlg->m_ctrlInfoList.DeleteAllItems();
COleVariant ValueVt;
int iFieldCount = ds.GetFields().GetCount();
for (int i = 0; i < iFieldCount; i++)
{
buffer = ds.GetFields().Item(i+1).GetName();
m_InfoDlg->m_ctrlInfoList.InsertItem(i,buffer);
COleVariant vVal;
for (int j = 0; j < iFieldCount; j++)
{
vVal = ds.GetValue(f.GetFeatureID(), j+1);
vVal.ChangeType(VT_BSTR);
buffer = vVal.bstrVal;
m_InfoDlg->m_ctrlInfoList.SetItemText(j,1,buffer);
} // end for (int j = 0; j < iFieldCount; j++)
} // end for (int i = 0; i < iFieldCount; i++)
break;
} // end if(fs.GetCount() != 0)
} // end if (dLayerZoomMax >= dMapZoom && dLayerZoomMin <= dMapZoom)
} // end for (int i = 1; i <= iCount; i++)
} // end if (ToolNum == INFO_TOOL)
}
void CGeoExpertView::OnUpdateMapToolInfotool(CCmdUI* pCmdUI)
{
// TODO: Add your command update UI handler code here
if (m_ctrlMapX.m_hWnd != NULL)
{
pCmdUI->Enable(TRUE);
}
else
{
pCmdUI->Enable(FALSE);
}
pCmdUI->SetCheck(m_ctrlMapX.GetCurrentTool() == INFO_TOOL);
}
void CGeoExpertView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
OnFileOpen();
}
//
void CGeoExpertView::OnToolTest()
{
/*
try
{
CMapXFeature f;
// create a new symbol or text feature
// first create the standalone feature object
f.CreateDispatch(f.GetClsid());
// attach standalone feature to map, as required in V3
f.Attach(m_ctrlMapX.GetDispatch());
///f.SetType(miFeatureTypeSymbol);
f.SetType(miFeatureTypeText);
f.SetCaption("Text");
// set the style and location of the feature
f.SetStyle(m_ctrlMapX.GetDefaultStyle());
f.GetPoint().Set(122.1419,37.4419);
// now add it to our layer
CMapXLayer layer = m_ctrlMapX.GetLayers().Item("Asia Capitals");
layer.AddFeature (f);
}
catch (COleDispatchException *e)
{
e->ReportError();
e->Delete();
}
catch (COleException *e)
{
e->ReportError();
e->Delete();
}
*/
CMapXLayers layers = m_ctrlMapX.GetLayers();
CMapXFeature ftr;
try
{
if (ftr.CreateDispatch(ftr.GetClsid()))
{
ftr = m_ctrlMapX.GetFeatureFactory().CreateSymbol();
ftr.SetStyle(m_ctrlMapX.GetDefaultStyle());
CMapXStyle style = ftr.GetStyle(); //获得图元的风格
//设置图元的风格,显示位图
style.SetSymbolType(miSymbolTypeBitmap);
//指定所要显示位图的存放位置
style.SetSymbolBitmapName("C:\\test.bmp");
//指定位图大小
style.SetSymbolBitmapSize(10); ////注意,不能大于48,否则出错
//设置为透明
style.SetSymbolBitmapTransparent(TRUE);
//指定需要显示图元的位置(经纬度位置)
ftr.GetPoint().Set(122.1419,37.4419);//学四东北角
layers.Item("Asia Capitals").AddFeature(ftr);
}
else
{
AfxThrowOleException(CO_E_CLASS_CREATE_FAILED);
}
}
catch (COleDispatchException *e)
{
e->ReportError();
e->Delete();
}
catch (COleException *e)
{
e->ReportError();
e->Delete();
}
}
void CGeoExpertView::OnViewTooltest()
{
// TODO: Add your command handler code here
OnToolTest();
}