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

📄 namegisview.cpp

📁 电子地图 地理信息系统二次开发实例教程-C#和MapObjects实现程序源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnUpdateMapInfo(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_nCurTool == ID_MAP_INFO);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnNameQuery()
{
	CNameQuryDlg NameQuryDlg;
	NameQuryDlg.DoModal();
}
//----------------------------------------------------------------------------------------------
// 在下拉列表框中加入“地名类型”数据表中大类名称
// 参数CComboBox* pComboBox表示在该下拉列表框中加入大类名称
BOOL CNameGisView::LoadFilter(CComboBox* pComboBox)
{
	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();

	CDaoDatabase* tmpDB = new CDaoDatabase;
	try
	{
		tmpDB->Open(pMainWnd->m_environment.m_szDBName);
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return FALSE;
	}

	CDaoRecordset rs(tmpDB);
	try
	{
		pComboBox->ResetContent();
		pComboBox->AddString("(全部类型)");

		CString strQuery = "Select [大类] from [地名类型] where 大类 <> '北京纵览'";
		rs.Open(dbOpenDynaset,strQuery);
		while(!rs.IsEOF())
		{
			COleVariant var;
			var = rs.GetFieldValue("大类");
			pComboBox->AddString(CCrack::strVARIANT(var));

			rs.MoveNext();
		}

		pComboBox->SetCurSel(0);
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		tmpDB->Close();
		delete tmpDB;
		e->Delete();
		return FALSE;
	}

	if(tmpDB)
	{
		if(tmpDB->IsOpen())
		{
			tmpDB->Close();
		}

		delete tmpDB;
		tmpDB = NULL;
	}
}
//----------------------------------------------------------------------------------------------
// 在下拉列表框中加入某大类的所有中类名称
// 参数pComboBox表示在该下拉列表框中加入名称
// 参数szFilter表示大类
BOOL CNameGisView::LoadFilter2(CComboBox* pComboBox, CString szFilter, bool bLayerType)
{
	CString szFilter2 = "";
	if (szFilter != "(全部类型)")
	{
		szFilter2 = "where 大类 = '" + szFilter + "'";
	}

	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();

	CDaoDatabase* tmpDB = new CDaoDatabase;
	try
	{
		tmpDB->Open(pMainWnd->m_environment.m_szDBName);
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		delete tmpDB;
		e->Delete();
		return FALSE;
	}

	CDaoRecordset rs(tmpDB);
	try
	{
		pComboBox->ResetContent();
		pComboBox->AddString("(全部子类型)");

		CString strQuery = "Select [中类] from [地名中类型]";
		strQuery += szFilter2;
		rs.Open(dbOpenDynaset,strQuery);
		while(!rs.IsEOF())
		{
			COleVariant var;
			var = rs.GetFieldValue("中类");
			CString str = CCrack::strVARIANT(var);
			if(bLayerType && str == "乡镇、街道")
				rs.MoveNext();
			else
			{
				pComboBox->AddString(str);
				rs.MoveNext();
			}
		}

		pComboBox->SetCurSel(0);
	}
	catch (CDaoException* e)
	{
		DisplayDaoException(e);
		tmpDB->Close();
		delete tmpDB;
		e->Delete();
		return FALSE;
	}

	if(tmpDB)
	{
		if(tmpDB->IsOpen())
		{
			tmpDB->Close();
		}

		delete tmpDB;
		tmpDB = NULL;
	}
	return TRUE;
}
//----------------------------------------------------------------------------------------------
// 查找最近地物
void CNameGisView::OnNearestName()
{
	m_nCurTool = ID_MAP_NEARESTNAME;
	if(m_pNearestDlg == NULL)
	{
		// 申请资源
		m_pNearestDlg = new CNearestDlg();
		if(m_pNearestDlg)
		{
			// 创建对话框
			BOOL ret = m_pNearestDlg->Create(IDD_DIALOG_NEARESTNAME, this);
			if(!ret)
			{
				AfxMessageBox("创建对话框失败!");
				return;
			}

			// 显示为一非模态对话框
			m_pNearestDlg->ShowWindow(SW_SHOW);
		}
		else
			AfxMessageBox("创建对话框失败!");
	}
	else
	{
		// 显示为一非模态对话框
		m_pNearestDlg->ShowWindow(SW_SHOW);
	}
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnBusQuery()
{
	CBusQueryDlg busDlg;
	busDlg.DoModal();
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnNameIndex()
{
	CNameIndexDlg NameIndexDlg;
	NameIndexDlg.DoModal();
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnNearestPath()
{
	if(m_nCurTool == ID_MAP_NEARESTPATH)
	{
		m_nCurTool = NULL;
		m_Map.SetMousePointer(moArrow);
	}
	else
	{
		m_nCurTool = ID_MAP_NEARESTPATH;
		m_Map.SetMousePointer(moCross);
	}
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnUpdateNrstPath(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_nCurTool == ID_MAP_NEARESTPATH);
}
//----------------------------------------------------------------------------------------------
int CNameGisView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFormView::OnCreate(lpCreateStruct) == -1)
		return -1;

	m_nTimer = SetTimer(1, 100, 0);
	return 0;
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnTimer(UINT nIDEvent)
{
	if(m_Map.m_hWnd == NULL)
		return;

	// 如果用户在进行其他操作,则不执行快速地名显示
	if(m_Map.GetMousePointer() != moArrow)
		return;

	if (m_x == m_lastX && m_y == m_lastY)
	{
		CMoRecordset recs;
		recs = DoSearch();
		if (!recs)
		{
			// 如果没有对应的地名,则隐藏标签倥件
			m_NameLabel.ModifyStyle(WS_VISIBLE, 0,0);
		}
		else
		{
			if(recs.GetEof())
			{
				m_NameLabel.ModifyStyle(WS_VISIBLE, 0,0); // 隐藏标签倥件
				return;
			}
			CString szText = recs.GetFields().Item(
				   COleVariant("名称")).GetValueAsString();
			if (szText != "")
			{
				// 设置标签控件的显示内容与位置
				m_NameLabel.SetWindowText(szText);				
				CRect rect;
				m_NameLabel.GetWindowRect(&rect);
				CSize sizeText;
				CClientDC dc((CWnd*)&m_NameLabel);
				// 得到文本在当前显示模式下的大小
				sizeText = dc.GetTextExtent(szText); 
				// 设置标签控件的位置与大小
				m_NameLabel.SetWindowPos(&CWnd::wndTop, 
					                     m_x + 20,    // 标签控件左上点的X坐标
										 m_y + 11,    // 标签控件左上点的Y坐标
										 sizeText.cx, // 标签控件的长度
										 sizeText.cy, // 标签控件的高度
										 SWP_SHOWWINDOW);
				m_NameLabel.ModifyStyle(0, WS_VISIBLE, 0);
			}
		}
	}
	else
	{
		m_lastX = m_x;
		m_lastY = m_y;
	}

	CFormView::OnTimer(nIDEvent);
}
//----------------------------------------------------------------------------------------------
// 搜索某点处的地名
CMoRecordset CNameGisView::DoSearch()
{
	CMoRecordset rst;
	if(!m_Map.m_hWnd)
	{		
		return rst;
	}

	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
	CMoPoint pt;	
	pt = m_Map.ToMapPoint((float)m_x,(float)m_y);  
	double dScale = pMainWnd->m_environment.CalcScale(&m_Map);
	dScale = dScale/10000;
	dScale = dScale / 5000;

	for (int i=pMainWnd->m_environment.m_nLayerNum - 1; i >= 0 ; i --)
	{
		if (pMainWnd->m_environment.m_layerInfos[i].layer.GetVisible() 
			&& pMainWnd->m_environment.m_layerInfos[i].bCanSelected 
			&& pMainWnd->m_environment.m_layerInfos[i].layer.GetShapeType() == moShapeTypePoint)
			rst = pMainWnd->m_environment.m_layerInfos[i].layer.SearchByDistance(pt,dScale,"");

		if (rst)
		{
			rst.MoveFirst(); 
			if (!rst.GetEof())
				return rst;
		}
	}

	for (int i = 0; i <pMainWnd->m_environment.m_nLayerNum; i ++)
	{
		if (pMainWnd->m_environment.m_layerInfos[i].bVisible 
			&& pMainWnd->m_environment.m_layerInfos[i].bCanSelected 
			&& pMainWnd->m_environment.m_layerInfos[i].layer.GetShapeType() == moShapeTypeLine)
			rst = pMainWnd->m_environment.m_layerInfos[i].layer.SearchByDistance(pt,dScale,"");

		if (rst)
		{
			rst.MoveFirst(); 
			if (!rst.GetEof())
				return rst;
		}
	}

	for (int i = 0; i<pMainWnd->m_environment.m_nLayerNum; i ++)
	{
		if (pMainWnd->m_environment.m_layerInfos[i].bVisible 
			&& pMainWnd->m_environment.m_layerInfos[i].bCanSelected 
			&& pMainWnd->m_environment.m_layerInfos[i].layer.GetShapeType() == moShapeTypePolygon)
			rst = pMainWnd->m_environment.m_layerInfos[i].layer.SearchByDistance(pt,dScale,"");

		if (rst)
		{
			rst.MoveFirst(); 
			if (!rst.GetEof())
				return rst;
		}
	}

	return rst;
}
//----------------------------------------------------------------------------------------------
HBRUSH CNameGisView::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
	HBRUSH hbr = CFormView::OnCtlColor(pDC, pWnd, nCtlColor);

	// 判断是否为目标控件
	if (pWnd->GetDlgCtrlID() == IDC_MAPTIP_STATIC)
	{      
		// 将文本颜色设置为红色
		pDC->SetTextColor(RGB(255, 0, 0));
		// 将背景颜色设置为黄色
		pDC->SetBkColor(RGB(255,255,0));
	}
	
	return hbr;
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnLineMeasure()
{
	if(m_nCurTool == ID_MAP_LINEMEASURE)
	{
		m_nCurTool = NULL;
		m_Map.SetMousePointer(moArrow);
	}
	else
	{
		m_nCurTool = ID_MAP_LINEMEASURE;
		m_Map.SetMousePointer(moCross);
	}
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnUpdateLnMsu(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_nCurTool == ID_MAP_LINEMEASURE);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnAreaMeasure()
{
	if(m_nCurTool == ID_MAP_AREAMEASURE)
	{
		m_nCurTool = NULL;
		m_Map.SetMousePointer(moArrow);
	}
	else
	{
		m_nCurTool = ID_MAP_AREAMEASURE;
		m_Map.SetMousePointer(moCross);
	}
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnUpdateAreaMsu(CCmdUI* pCmdUI)
{
	pCmdUI->SetCheck(m_nCurTool == ID_MAP_AREAMEASURE);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnMapIndex()
{
	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
	CMapControlView* pCtrlView = (CMapControlView*)(pMainWnd->m_wndSplitter2.GetPane(0,0));
	if(pCtrlView->m_OperaTab.GetCurSel() == 0)
		return;

	pCtrlView->m_OperaTab.SetCurSel(0);
	pCtrlView->m_MapIndexTreeView.ModifyStyle(0, WS_VISIBLE, 0);
	pCtrlView->m_LayerTreeView.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_ResultListBox.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_ContentBtn.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_PositingBtn.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->Invalidate(TRUE);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnLayerControl()
{
	CMainFrame* pMainWnd = (CMainFrame*)AfxGetMainWnd();
	CMapControlView* pCtrlView = (CMapControlView*)(pMainWnd->m_wndSplitter2.GetPane(0,0));
	if(pCtrlView->m_OperaTab.GetCurSel() == 1)
		return;

	pCtrlView->m_OperaTab.SetCurSel(1);
	pCtrlView->m_MapIndexTreeView.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_LayerTreeView.ModifyStyle(0, WS_VISIBLE, 0);
	pCtrlView->m_ResultListBox.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_ContentBtn.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->m_PositingBtn.ModifyStyle(WS_VISIBLE, 0, 0);
	pCtrlView->Invalidate(TRUE);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnPrintMap()
{
	m_Map.PrintMap("地图", "北京市地理信息公众查询系统", TRUE);
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnOutputMap()
{
	CString szFilters = "位图(*.bmp)|*.bmp|增强的图元格式文件(*.emf)|*.emf|";
	szFilters += "Jpeg文件(*.jpg)|*.jpg;*.jpeg||";
	CFileDialog fileDlg(FALSE, "map", "*.bmp", OFN_FILEMUSTEXIST| OFN_HIDEREADONLY,
	                    szFilters, this);
	if( fileDlg.DoModal ()== IDOK)
	{
		CString ext = fileDlg.GetFileExt();
		if(ext == "bmp")
		{
			m_Map.ExportMap(moExportBMP, fileDlg.GetPathName(), 1);
		}
		else if(ext == "emf")
		{
			m_Map.ExportMap(moExportEMF, fileDlg.GetPathName(), 1);
		}
		else
		{
			m_Map.ExportMapToJpeg(fileDlg.GetPathName(), 100, TRUE, 1, 
				                  moLineSymbolsNotScaled);
		}
	}
}
//----------------------------------------------------------------------------------------------
void CNameGisView::OnShowHelp()
{
	CString www = GetModulePath() + "\\帮助\\start.htm";
	ShellExecute(m_hWnd, "open", www,0,0,SW_SHOW);
}
//----------------------------------------------------------------------------------------------

⌨️ 快捷键说明

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