MyFileLayer.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:10k
- #include "stdafx.h"
- #include "MyFileLayer.h"
- IMPLEMENT_SERIAL(CFileLayer, CObject, 1)
- CFileLayer::CFileLayer(CString name, BOOL show, BOOL locked)
- {
- m_name = name;
- m_show = show;
- m_locked = locked;
- }
- CFileLayer::operator =(const CFileLayer& Layer)
- {
- m_name = Layer.m_name;
- m_show = Layer.m_show;
- m_locked = Layer.m_locked;
- }
- void CFileLayer::SetShow(BOOL flag)
- {
- m_show = flag;
- }
- void CFileLayer::SetLocked(BOOL flag)
- {
- m_locked = flag;
- }
- void CFileLayer::SetName(CString name)
- {
- m_name = name;
- }
- CString CFileLayer::GetName()
- {
- return m_name;
- }
- BOOL CFileLayer::IsShow()
- {
- return m_show;
- }
- BOOL CFileLayer::IsLocked()
- {
- return m_locked;
- }
- void CFileLayer::Serialize(CArchive& ar)
- {
- if(ar.IsStoring())
- {
- ar << m_name << m_show << m_locked;
- }
- else
- {
- ar >> m_name >> m_show >> m_locked;
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////
- CManageFileLayer::CManageFileLayer()
- {
- CurrentLayerName = "";
- }
- CManageFileLayer::~CManageFileLayer()
- {
- for(int i = 0; i < m_LayerArray.GetSize(); i++)
- delete (CFileLayer*)m_LayerArray.GetAt(i);
- m_LayerArray.RemoveAll();
- }
- UINT CManageFileLayer::GetLayerNum()
- {
- return m_LayerArray.GetSize();
- }
- void CManageFileLayer::Serialize(CArchive& ar)
- {
- if(ar.IsStoring())
- {
- ar << CurrentLayerName;
- }
- else
- {
- ar >> CurrentLayerName;
- }
- m_LayerArray.Serialize(ar);
- }
- void CManageFileLayer::AddLayer(CString name, BOOL show, BOOL locked)
- {
- CFileLayer* pLayer;
- pLayer = new CFileLayer(name, show, locked);
- m_LayerArray.Add(pLayer);
- }
- void CManageFileLayer::DelLayer(CString name)
- {
- for(int i = 0; i < m_LayerArray.GetSize(); i++)
- {
- CFileLayer* pLayer = (CFileLayer*)m_LayerArray.GetAt(i);
- if(pLayer->GetName() == name)
- {
- delete pLayer;
- m_LayerArray.RemoveAt(i);
- return;
- }
- }
- }
- CFileLayer* CManageFileLayer::GetLayer(int index)
- {
- ASSERT(index >= 0 && index < m_LayerArray.GetSize());
- return (CFileLayer*)m_LayerArray.GetAt(index);
- }
- CFileLayer* CManageFileLayer::GetLayer(CString name)
- {
- for(int i = 0; i < m_LayerArray.GetSize(); i++)
- {
- CFileLayer* pLayer = (CFileLayer*)m_LayerArray.GetAt(i);
- if(pLayer->GetName() == name)
- {
- return pLayer;
- }
- }
- return NULL;
- }
- void CManageFileLayer::SetCurrentLayerName(CString name)
- {
- CurrentLayerName = name;
- }
- CString CManageFileLayer::GetCurrentLayerName()
- {
- return CurrentLayerName;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CFileLayerDlg dialog
- CFileLayerDlg::CFileLayerDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CFileLayerDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CFileLayerDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- void CFileLayerDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CFileLayerDlg)
- DDX_Control(pDX, IDC_NAME, m_name);
- DDX_Control(pDX, IDC_LAYERLIST, m_LayerList);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CFileLayerDlg, CDialog)
- //{{AFX_MSG_MAP(CFileLayerDlg)
- ON_BN_CLICKED(IDC_NEW, OnNew)
- ON_BN_CLICKED(IDC_DELETE, OnDelete)
- ON_NOTIFY(NM_DBLCLK, IDC_LAYERLIST, OnDblclkLayerlist)
- ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LAYERLIST, OnEndlabeleditLayerlist)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CFileLayerDlg message handlers
- BOOL CFileLayerDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- NewFlag = FALSE;
- m_name.SetWindowText(FileName);
- CImageList m_ImageList;
-
- m_ImageList.Create(16, 16, ILC_MASK, 2, 2);
- m_LayerList.SetExtendedStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT);
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_LAYER));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_LOCKFLAG));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_UNLOCKFLAG));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_SHOWFLAG));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_HIDEFLAG));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_CURRENTFLAG));
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_NOCURRENTFLAG));
-
- m_LayerList.SetImageList(&m_ImageList, LVSIL_SMALL);
- m_ImageList.Detach();
- int width;
- CRect rect;
- m_LayerList.GetClientRect(&rect);
- width = rect.Width() / 4;
- m_LayerList.InsertColumn(0, "名称", LVCFMT_LEFT, width);
- m_LayerList.InsertColumn(1, "显示", LVCFMT_LEFT, width);
- m_LayerList.InsertColumn(2, "锁定", LVCFMT_LEFT, width);
- m_LayerList.InsertColumn(3, "当前层", LVCFMT_LEFT, width);
- for(int i = 0; i < (int)pManageFileLayer->GetLayerNum(); i++)
- {
- m_LayerList.InsertItem(i, pManageFileLayer->GetLayer(i)->GetName());
-
- if(pManageFileLayer->GetLayer(i)->IsShow())
- m_LayerList.SetItem(i, 1, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
- else
- m_LayerList.SetItem(i, 1, LVIF_TEXT | LVIF_IMAGE, "否", ICON_HIDEFLAG, 0, 0, LVIF_PARAM);
- if(pManageFileLayer->GetLayer(i)->IsLocked())
- m_LayerList.SetItem(i, 2, LVIF_TEXT | LVIF_IMAGE, "是", ICON_LOCKFLAG, 0, 0, LVIF_PARAM);
- else
- m_LayerList.SetItem(i, 2, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
- if(pManageFileLayer->GetLayer(i)->GetName() == pManageFileLayer->GetCurrentLayerName())
- m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "是", ICON_CURRENTFLAG, 0, 0, LVIF_PARAM);
- else
- m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
- }
- return TRUE;
- }
- void CFileLayerDlg::Create(CString name, CManageFileLayer* pManageLayer)
- {
- FileName = name;
- pManageFileLayer = pManageLayer;
- DoModal();
- }
- void CFileLayerDlg::OnNew()
- {
- m_LayerList.SetActiveWindow();
- NewFlag = TRUE;
- m_LayerList.InsertItem(m_LayerList.GetItemCount(), "基层", 0);
- m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 1, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
- m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 2, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
- m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
- //设置焦点在m_LayerList
- m_LayerList.SetFocus();
- //编辑标题
- CEdit* pEdit = m_LayerList.EditLabel(m_LayerList.GetItemCount() - 1);
- }
- void CFileLayerDlg::OnDelete()
- {
- m_LayerList.GetFirstSelectedItemPosition();
- POSITION pos = m_LayerList.GetFirstSelectedItemPosition();
- if (pos == NULL)
- return;
- else
- {
- while (pos)
- {
- int index = m_LayerList.GetNextSelectedItem(pos);
- CString name = m_LayerList.GetItemText(index, 0);
- m_LayerList.DeleteItem(index);
- pManageFileLayer->DelLayer(name);
- //如果删除了当前层,设置当前层为空
- if(name == pManageFileLayer->GetCurrentLayerName())
- pManageFileLayer->SetCurrentLayerName("");
- }
- }
- }
- void CFileLayerDlg::OnDblclkLayerlist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pListView = (NM_LISTVIEW*)pNMHDR;
- CString layername;
- CFileLayer* pLayer;
-
- if(pListView->iSubItem == 0)
- //如果双击第一项,则编辑图层的名字
- m_LayerList.EditLabel(pListView->iItem);
- else
- {
- layername = m_LayerList.GetItemText(pListView->iItem, 0);
- pLayer = pManageFileLayer->GetLayer(layername);
- ASSERT(pLayer != NULL);
- switch(pListView->iSubItem)
- {
- case 1:
- if(pLayer->IsShow())
- {
- m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "否", ICON_HIDEFLAG, 0, 0, LVIF_PARAM);
- pLayer->SetShow(FALSE);
- }
- else
- {
- m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
- pLayer->SetShow(TRUE);
- }
- break;
- case 2:
- if(pLayer->IsLocked())
- {
- m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
- pLayer->SetLocked(FALSE);
- }
- else
- {
- m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "是", ICON_LOCKFLAG, 0, 0, LVIF_PARAM);
- pLayer->SetLocked(TRUE);
- }
- break;
- case 3:
- pManageFileLayer->SetCurrentLayerName(layername);
- for(int i =0; i < m_LayerList.GetItemCount(); i++)
- {
- if(m_LayerList.GetItemText(i, 0) == layername)
- m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "是", ICON_CURRENTFLAG, 0, 0, LVIF_PARAM);
- else
- m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
- }
- break;
- }
- }
- *pResult = 0;
- }
- void CFileLayerDlg::OnEndlabeleditLayerlist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
- CString oldname = m_LayerList.GetItemText(pDispInfo->item.iItem, 0);;
- CFileLayer* pLayer;
- CString newname = pDispInfo->item.pszText;
- if(newname.IsEmpty())
- newname = oldname;
- for(int i = 0; i < m_LayerList.GetItemCount() - 1; i++)
- {
- if(i == pDispInfo->item.iItem)
- continue;
- CString tempname = m_LayerList.GetItemText(i, 0);
- if(tempname == newname)
- {
- if(m_LayerList.MessageBox("图层名不能重复!", "错误", MB_OK
- | MB_ICONERROR) == 1)
- {
- m_LayerList.SetFocus();
- CEdit* pEdit = m_LayerList.EditLabel(pDispInfo->item.iItem);
- break;
- }
- }
- }
- if(NewFlag == FALSE)
- {
- //获取图层指针
- pLayer = pManageFileLayer->GetLayer(oldname);
- m_LayerList.SetItemText(pDispInfo->item.iItem, 0, newname);
- pLayer->SetName(newname);
- }
- //如果是新建图层时编辑标题
- else
- {
- m_LayerList.SetItemText(pDispInfo->item.iItem, 0, newname);
- pManageFileLayer->AddLayer(newname, TRUE, FALSE);
- NewFlag = FALSE;
- }
- *pResult = 0;
- }