代码改变世界

一个简单的MFC中TreeView使用

2011-04-19 11:05  愤怒的青蛙  阅读(3417)  评论(0编辑  收藏  举报

今天用MapX写图层管理是,使用TreeView来显示和管理图层,样式模仿ArcGIS的样式,可以通过勾选相应的图层来决定相应的图层是否显示,可是在CTreeCtrl使用上不熟悉,郁闷了好长时间,用从各种地方找了很多资料,有的是对的,有的是部分对的(这个最恶心),有的是全错的。。。不扯了,直接上代码

void CMy2008302590145View::OnLoadlayer()
{
	// TODO: Add your command handler code here
	//	添加图层
	//	方法一:使用MapX自带的默认函数添加图层
	//	m_MapX.GetLayers().LayersDlg();
	//	m_MapX.SetCurrentTool(miPanTool);
	//	方法二:使用自定义对话框添加
	CFileDialog *pFileDlg;
	pFileDlg = new CFileDialog(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT, _T("图层文件 (*.TAB)|*.TAB|All Files (*.*)|*.*||"));
	if (pFileDlg->DoModal() == IDOK)
	{
		CMapXLayers layers = m_MapX.GetLayers();
		POSITION pos = pFileDlg->GetStartPosition();
		TVINSERTSTRUCT tvRoot;
		tvRoot.hParent = NULL;
		tvRoot.hInsertAfter = NULL;
		tvRoot.item.mask = TVIF_TEXT;
		char* layername;
		layername = new char[256];
		layerIndex++;
		sprintf(layername, "图层%d", layerIndex);
		tvRoot.item.pszText = _bstr_t(layername);
		HTREEITEM hTreeRoot = m_MapLayerTreeCtrl.InsertItem(&tvRoot);
		m_MapLayerTreeCtrl.SetCheck(hTreeRoot, TRUE);
		while (pos)
		{
			itemIndex++;
			CString strFilePath = pFileDlg->GetNextPathName(pos);
			HTREEITEM tempItem = m_MapLayerTreeCtrl.InsertItem(TVIF_TEXT, strFilePath, 0, 0, 0, 0, 0, hTreeRoot, NULL);
			m_MapLayerTreeCtrl.SetItemData(tempItem,itemIndex);
			m_MapLayerTreeCtrl.SetCheck(tempItem,TRUE);
			layers.Add(strFilePath, itemIndex);
		}
	}
}
void CMy2008302590145View::OnZoomin()
{
	// TODO: Add your command handler code here
	m_MapX.SetCurrentTool(miZoomInTool);
}
void CMy2008302590145View::OnZoomout()
{
	// TODO: Add your command handler code here
	m_MapX.SetCurrentTool(miZoomOutTool);
}
void CMy2008302590145View::OnPan()
{
	// TODO: Add your command handler code here
	m_MapX.SetCurrentTool(miPanTool);
}
void CMy2008302590145View::OnNMClickTreeLayers(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	*pResult = 0;
	NM_TREEVIEW *pHdr = (NM_TREEVIEW*)pNMHDR;
	*pResult = 0;
	CPoint point;
	UINT uFlag;
	GetCursorPos(&point);
	
	//这个很关键,下午我就是这里错了,导致一直结果很诡异。。。
	m_MapLayerTreeCtrl.ScreenToClient(&point);
	
	HTREEITEM treeItem = m_MapLayerTreeCtrl.HitTest(point, &uFlag);
	if (treeItem && (uFlag & TVHT_ONITEMSTATEICON))
	{
		BOOL bCheck = m_MapLayerTreeCtrl.GetCheck(treeItem);
		if (m_MapLayerTreeCtrl.GetChildItem(treeItem) == NULL)
		{
			int itemIndex = m_MapLayerTreeCtrl.GetItemData(treeItem);
			m_MapX.GetLayers().Item(itemIndex).SetVisible(!bCheck);
		}
		SetItemCheckState(treeItem, !bCheck);
	}
}
void CMy2008302590145View::SetItemCheckState(HTREEITEM treeItem, bool bCheck)
{
	SetChildCheck(treeItem, bCheck);
	SetParentCheck(treeItem, bCheck);
}
void CMy2008302590145View::SetChildCheck(HTREEITEM treeItem, bool bCheck)
{
	HTREEITEM childItem = m_MapLayerTreeCtrl.GetChildItem(treeItem);
	while(childItem)
	{
		m_MapLayerTreeCtrl.SetCheck(childItem,bCheck);
		if (m_MapLayerTreeCtrl.GetChildItem(childItem) == NULL)
		{
			int itemIndex = m_MapLayerTreeCtrl.GetItemData(childItem);
			m_MapX.GetLayers().Item(itemIndex).SetVisible(bCheck);
		}
		SetChildCheck(childItem, bCheck);
		childItem = m_MapLayerTreeCtrl.GetNextItem(childItem, TVGN_NEXT);
	}
}
void CMy2008302590145View::SetParentCheck(HTREEITEM treeItem, bool bCheck)
{
	HTREEITEM parentItem = m_MapLayerTreeCtrl.GetParentItem(treeItem);
	if (parentItem == NULL)
	{
		return;
	}
	if (bCheck)
	{
		m_MapLayerTreeCtrl.SetCheck(parentItem,bCheck);
	}
	else
	{
		HTREEITEM bro = m_MapLayerTreeCtrl.GetNextItem(treeItem, TVGN_NEXT);
		BOOL bFlag = FALSE;
		while(bro)
		{
			if (m_MapLayerTreeCtrl.GetCheck(bro))
			{
				bFlag = TRUE;
				break;
			}
			bro = m_MapLayerTreeCtrl.GetNextItem(bro, TVGN_NEXT);
		}
		if (!bFlag)
		{
			bro = m_MapLayerTreeCtrl.GetNextItem(treeItem, TVGN_PREVIOUS);
			while(bro)
			{
				if (m_MapLayerTreeCtrl.GetCheck(bro))
				{
					bFlag = TRUE;
					break;
				}
				bro = m_MapLayerTreeCtrl.GetNextItem(bro, TVGN_PREVIOUS);
			}
		}
		if (!bFlag)
		{
			m_MapLayerTreeCtrl.SetCheck(parentItem, FALSE);
		}
		SetParentCheck(parentItem, m_MapLayerTreeCtrl.GetCheck(parentItem));
	}
}

代码比较简单,所以就没有写注释