MyFileLayer.cpp
上传用户:netltd
上传日期:2013-02-12
资源大小:7234k
文件大小:10k
源码类别:

绘图程序

开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "MyFileLayer.h"
  3. IMPLEMENT_SERIAL(CFileLayer, CObject, 1)
  4. CFileLayer::CFileLayer(CString name, BOOL show, BOOL locked)
  5. {
  6. m_name = name;
  7. m_show = show;
  8. m_locked = locked;
  9. }
  10. CFileLayer::operator =(const CFileLayer& Layer)
  11. {
  12. m_name = Layer.m_name;
  13. m_show = Layer.m_show;
  14. m_locked = Layer.m_locked;
  15. }
  16. void CFileLayer::SetShow(BOOL flag)
  17. {
  18. m_show = flag;
  19. }
  20. void CFileLayer::SetLocked(BOOL flag)
  21. {
  22. m_locked = flag;
  23. }
  24. void CFileLayer::SetName(CString name)
  25. {
  26. m_name = name;
  27. }
  28. CString CFileLayer::GetName()
  29. {
  30. return m_name;
  31. }
  32. BOOL CFileLayer::IsShow()
  33. {
  34. return m_show;
  35. }
  36. BOOL CFileLayer::IsLocked()
  37. {
  38. return m_locked;
  39. }
  40. void CFileLayer::Serialize(CArchive& ar)
  41. {
  42. if(ar.IsStoring())
  43. {
  44. ar << m_name << m_show << m_locked;
  45. }
  46. else
  47. {
  48. ar >> m_name >> m_show >> m_locked;
  49. }
  50. }
  51. ///////////////////////////////////////////////////////////////////////////////////////
  52. CManageFileLayer::CManageFileLayer()
  53. {
  54. CurrentLayerName = "";
  55. }
  56. CManageFileLayer::~CManageFileLayer()
  57. {
  58. for(int i = 0; i < m_LayerArray.GetSize(); i++)
  59. delete (CFileLayer*)m_LayerArray.GetAt(i);
  60.     m_LayerArray.RemoveAll();
  61. }
  62. UINT CManageFileLayer::GetLayerNum()
  63. {
  64. return m_LayerArray.GetSize();
  65. }
  66. void CManageFileLayer::Serialize(CArchive& ar)
  67. {
  68. if(ar.IsStoring())
  69. {
  70. ar << CurrentLayerName;
  71. }
  72. else
  73. {
  74. ar >> CurrentLayerName;
  75. }
  76. m_LayerArray.Serialize(ar);
  77. }
  78. void CManageFileLayer::AddLayer(CString name, BOOL show, BOOL locked)
  79. {
  80. CFileLayer* pLayer;
  81. pLayer = new CFileLayer(name, show, locked);
  82. m_LayerArray.Add(pLayer);
  83. }
  84. void CManageFileLayer::DelLayer(CString name)
  85. {
  86. for(int i = 0; i < m_LayerArray.GetSize(); i++)
  87. {
  88. CFileLayer* pLayer = (CFileLayer*)m_LayerArray.GetAt(i);
  89. if(pLayer->GetName() == name)
  90. {
  91. delete pLayer;
  92. m_LayerArray.RemoveAt(i);
  93. return;
  94. }
  95. }
  96. }
  97. CFileLayer*  CManageFileLayer::GetLayer(int index)
  98. {
  99. ASSERT(index >= 0 && index < m_LayerArray.GetSize());
  100.     return (CFileLayer*)m_LayerArray.GetAt(index);
  101. }
  102. CFileLayer*  CManageFileLayer::GetLayer(CString name)
  103. {
  104. for(int i = 0; i < m_LayerArray.GetSize(); i++)
  105. {
  106. CFileLayer* pLayer = (CFileLayer*)m_LayerArray.GetAt(i);
  107. if(pLayer->GetName() == name)
  108. {
  109. return pLayer;
  110. }
  111. }
  112. return NULL;
  113. }
  114. void CManageFileLayer::SetCurrentLayerName(CString name)
  115. {
  116. CurrentLayerName = name;
  117. }
  118. CString CManageFileLayer::GetCurrentLayerName()
  119. {
  120. return CurrentLayerName;
  121. }
  122. /////////////////////////////////////////////////////////////////////////////
  123. // CFileLayerDlg dialog
  124. CFileLayerDlg::CFileLayerDlg(CWnd* pParent /*=NULL*/)
  125. : CDialog(CFileLayerDlg::IDD, pParent)
  126. {
  127. //{{AFX_DATA_INIT(CFileLayerDlg)
  128. // NOTE: the ClassWizard will add member initialization here
  129. //}}AFX_DATA_INIT
  130. }
  131. void CFileLayerDlg::DoDataExchange(CDataExchange* pDX)
  132. {
  133. CDialog::DoDataExchange(pDX);
  134. //{{AFX_DATA_MAP(CFileLayerDlg)
  135. DDX_Control(pDX, IDC_NAME, m_name);
  136. DDX_Control(pDX, IDC_LAYERLIST, m_LayerList);
  137. //}}AFX_DATA_MAP
  138. }
  139. BEGIN_MESSAGE_MAP(CFileLayerDlg, CDialog)
  140. //{{AFX_MSG_MAP(CFileLayerDlg)
  141. ON_BN_CLICKED(IDC_NEW, OnNew)
  142. ON_BN_CLICKED(IDC_DELETE, OnDelete)
  143. ON_NOTIFY(NM_DBLCLK, IDC_LAYERLIST, OnDblclkLayerlist)
  144. ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LAYERLIST, OnEndlabeleditLayerlist)
  145. //}}AFX_MSG_MAP
  146. END_MESSAGE_MAP()
  147. /////////////////////////////////////////////////////////////////////////////
  148. // CFileLayerDlg message handlers
  149. BOOL CFileLayerDlg::OnInitDialog() 
  150. {
  151. CDialog::OnInitDialog();
  152. NewFlag = FALSE;
  153. m_name.SetWindowText(FileName);
  154. CImageList  m_ImageList;
  155. m_ImageList.Create(16, 16, ILC_MASK, 2, 2);
  156. m_LayerList.SetExtendedStyle(LVS_EX_SUBITEMIMAGES | LVS_EX_FULLROWSELECT);
  157. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_LAYER));
  158. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_LOCKFLAG));
  159. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_UNLOCKFLAG));
  160. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_SHOWFLAG));
  161. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_HIDEFLAG));
  162. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_CURRENTFLAG));
  163. m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_NOCURRENTFLAG));
  164. m_LayerList.SetImageList(&m_ImageList, LVSIL_SMALL);
  165. m_ImageList.Detach();
  166. int width;
  167. CRect rect;
  168. m_LayerList.GetClientRect(&rect);
  169. width = rect.Width() / 4;
  170. m_LayerList.InsertColumn(0, "名称", LVCFMT_LEFT, width);
  171. m_LayerList.InsertColumn(1, "显示", LVCFMT_LEFT, width);
  172. m_LayerList.InsertColumn(2, "锁定", LVCFMT_LEFT, width);
  173. m_LayerList.InsertColumn(3, "当前层", LVCFMT_LEFT, width);
  174. for(int i = 0; i < (int)pManageFileLayer->GetLayerNum(); i++)
  175. {
  176. m_LayerList.InsertItem(i, pManageFileLayer->GetLayer(i)->GetName());
  177. if(pManageFileLayer->GetLayer(i)->IsShow())
  178. m_LayerList.SetItem(i, 1, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
  179. else
  180. m_LayerList.SetItem(i, 1, LVIF_TEXT | LVIF_IMAGE, "否", ICON_HIDEFLAG, 0, 0, LVIF_PARAM);
  181. if(pManageFileLayer->GetLayer(i)->IsLocked())
  182. m_LayerList.SetItem(i, 2, LVIF_TEXT | LVIF_IMAGE, "是", ICON_LOCKFLAG, 0, 0, LVIF_PARAM);
  183. else
  184. m_LayerList.SetItem(i, 2, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
  185. if(pManageFileLayer->GetLayer(i)->GetName() == pManageFileLayer->GetCurrentLayerName())
  186. m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "是", ICON_CURRENTFLAG, 0, 0, LVIF_PARAM);
  187. else
  188. m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
  189. }
  190. return TRUE;  
  191. }
  192. void CFileLayerDlg::Create(CString name, CManageFileLayer* pManageLayer)
  193. {
  194. FileName = name;
  195. pManageFileLayer = pManageLayer;
  196. DoModal();
  197. }
  198. void CFileLayerDlg::OnNew() 
  199. {
  200. m_LayerList.SetActiveWindow();
  201. NewFlag = TRUE;
  202. m_LayerList.InsertItem(m_LayerList.GetItemCount(), "基层", 0);
  203. m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 1, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
  204. m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 2, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
  205.     m_LayerList.SetItem(m_LayerList.GetItemCount() - 1, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
  206. //设置焦点在m_LayerList
  207. m_LayerList.SetFocus();
  208. //编辑标题
  209. CEdit* pEdit = m_LayerList.EditLabel(m_LayerList.GetItemCount() - 1);
  210. }
  211. void CFileLayerDlg::OnDelete() 
  212. {
  213. m_LayerList.GetFirstSelectedItemPosition();
  214. POSITION pos = m_LayerList.GetFirstSelectedItemPosition();
  215. if (pos == NULL)
  216. return;
  217. else
  218. {
  219. while (pos)
  220. {
  221. int index = m_LayerList.GetNextSelectedItem(pos);
  222. CString name = m_LayerList.GetItemText(index, 0);
  223. m_LayerList.DeleteItem(index);
  224. pManageFileLayer->DelLayer(name);
  225. //如果删除了当前层,设置当前层为空
  226. if(name == pManageFileLayer->GetCurrentLayerName())
  227. pManageFileLayer->SetCurrentLayerName("");
  228.  }
  229. }
  230. }
  231. void CFileLayerDlg::OnDblclkLayerlist(NMHDR* pNMHDR, LRESULT* pResult) 
  232. {
  233. NM_LISTVIEW*  pListView = (NM_LISTVIEW*)pNMHDR;
  234. CString layername;
  235. CFileLayer* pLayer;
  236. if(pListView->iSubItem == 0)
  237. //如果双击第一项,则编辑图层的名字
  238. m_LayerList.EditLabel(pListView->iItem);
  239. else
  240. {
  241. layername = m_LayerList.GetItemText(pListView->iItem, 0);
  242. pLayer = pManageFileLayer->GetLayer(layername);
  243. ASSERT(pLayer != NULL);
  244. switch(pListView->iSubItem)
  245. {
  246. case 1:
  247. if(pLayer->IsShow())
  248. {
  249. m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "否", ICON_HIDEFLAG, 0, 0, LVIF_PARAM);
  250. pLayer->SetShow(FALSE);
  251. }
  252. else
  253. {
  254. m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "是", ICON_SHOWFLAG, 0, 0, LVIF_PARAM);
  255. pLayer->SetShow(TRUE);
  256. }
  257. break;
  258. case 2:
  259. if(pLayer->IsLocked())
  260. {
  261. m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "否", ICON_UNLOCKFLAG, 0, 0, LVIF_PARAM);
  262.                 pLayer->SetLocked(FALSE);
  263. }
  264. else
  265. {
  266. m_LayerList.SetItem(pListView->iItem, pListView->iSubItem, LVIF_TEXT | LVIF_IMAGE, "是", ICON_LOCKFLAG, 0, 0, LVIF_PARAM);
  267.                 pLayer->SetLocked(TRUE);
  268. }
  269. break;
  270. case 3:
  271. pManageFileLayer->SetCurrentLayerName(layername);
  272. for(int i =0; i < m_LayerList.GetItemCount(); i++)
  273. {
  274. if(m_LayerList.GetItemText(i, 0) == layername)
  275. m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "是", ICON_CURRENTFLAG, 0, 0, LVIF_PARAM);
  276. else
  277. m_LayerList.SetItem(i, 3, LVIF_TEXT | LVIF_IMAGE, "否", ICON_NOCURRENTFLAG, 0, 0, LVIF_PARAM);
  278. }
  279.             break;  
  280. }
  281. }
  282. *pResult = 0;
  283. }
  284. void CFileLayerDlg::OnEndlabeleditLayerlist(NMHDR* pNMHDR, LRESULT* pResult) 
  285. {
  286. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)pNMHDR;
  287.     CString oldname = m_LayerList.GetItemText(pDispInfo->item.iItem, 0);;
  288. CFileLayer* pLayer;
  289. CString newname = pDispInfo->item.pszText;
  290. if(newname.IsEmpty())
  291. newname = oldname;
  292. for(int i = 0; i < m_LayerList.GetItemCount() - 1; i++)
  293. {
  294.     if(i == pDispInfo->item.iItem)
  295.    continue;
  296. CString tempname = m_LayerList.GetItemText(i, 0);
  297. if(tempname == newname)
  298. {
  299. if(m_LayerList.MessageBox("图层名不能重复!", "错误", MB_OK
  300.    | MB_ICONERROR) == 1)
  301. {
  302.    m_LayerList.SetFocus();
  303.    CEdit* pEdit = m_LayerList.EditLabel(pDispInfo->item.iItem);
  304.    break;
  305. }
  306. }
  307. }
  308.     if(NewFlag == FALSE)
  309. {
  310. //获取图层指针
  311. pLayer = pManageFileLayer->GetLayer(oldname);
  312. m_LayerList.SetItemText(pDispInfo->item.iItem, 0, newname);
  313. pLayer->SetName(newname);
  314. }
  315.     //如果是新建图层时编辑标题 
  316. else
  317. {
  318. m_LayerList.SetItemText(pDispInfo->item.iItem, 0, newname);
  319. pManageFileLayer->AddLayer(newname, TRUE, FALSE);
  320. NewFlag = FALSE;
  321. }
  322. *pResult = 0;
  323. }