lyfzBranchInfoCfgDlg.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. // lyfzBranchInfoCfgDlg.cpp : 实现文件
  2. //
  3. #include "stdafx.h"
  4. #include "lyfzBranchInfoCfg.h"
  5. #include "lyfzBranchInfoCfgDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #endif
  9. // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
  10. class CAboutDlg : public CDialog
  11. {
  12. public:
  13. CAboutDlg();
  14. // 对话框数据
  15. enum { IDD = IDD_ABOUTBOX };
  16. protected:
  17. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
  18. // 实现
  19. protected:
  20. DECLARE_MESSAGE_MAP()
  21. };
  22. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  23. {
  24. }
  25. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  26. {
  27. CDialog::DoDataExchange(pDX);
  28. }
  29. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  30. END_MESSAGE_MAP()
  31. // ClyfzBranchInfoCfgDlg 对话框
  32. ClyfzBranchInfoCfgDlg::ClyfzBranchInfoCfgDlg(CWnd* pParent /*=NULL*/)
  33. : CDialog(ClyfzBranchInfoCfgDlg::IDD, pParent)
  34. , m_strFilePath(_T(""))
  35. , m_nCurrSel(-1)
  36. {
  37. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  38. }
  39. void ClyfzBranchInfoCfgDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41. CDialog::DoDataExchange(pDX);
  42. DDX_Control(pDX, IDC_BranchInfo_LIST, m_BranchInfoList);
  43. }
  44. BEGIN_MESSAGE_MAP(ClyfzBranchInfoCfgDlg, CDialog)
  45. ON_WM_SYSCOMMAND()
  46. ON_WM_PAINT()
  47. ON_WM_QUERYDRAGICON()
  48. ON_BN_CLICKED(IDC_Edit_BUTTON, &ClyfzBranchInfoCfgDlg::OnBnClickedEditButton)
  49. ON_BN_CLICKED(IDC_Save_BUTTON, &ClyfzBranchInfoCfgDlg::OnBnClickedSaveButton)
  50. ON_NOTIFY(NM_CLICK, IDC_BranchInfo_LIST, &ClyfzBranchInfoCfgDlg::OnNMClickBranchinfoList)
  51. ON_NOTIFY(LVN_ITEMCHANGED, IDC_BranchInfo_LIST, &ClyfzBranchInfoCfgDlg::OnLvnItemchangedBranchinfoList)
  52. ON_BN_CLICKED(IDC_ADD_BUTTON, &ClyfzBranchInfoCfgDlg::OnBnClickedAddButton)
  53. ON_BN_CLICKED(IDC_DEL_BUTTON, &ClyfzBranchInfoCfgDlg::OnBnClickedDelButton)
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. // ClyfzBranchInfoCfgDlg 消息处理程序
  57. BOOL ClyfzBranchInfoCfgDlg::OnInitDialog()
  58. {
  59. CDialog::OnInitDialog();
  60. // 将“关于...”菜单项添加到系统菜单中。
  61. // IDM_ABOUTBOX 必须在系统命令范围内。
  62. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  63. ASSERT(IDM_ABOUTBOX < 0xF000);
  64. CMenu* pSysMenu = GetSystemMenu(FALSE);
  65. if (pSysMenu != NULL)
  66. {
  67. BOOL bNameValid;
  68. CString strAboutMenu;
  69. bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
  70. ASSERT(bNameValid);
  71. if (!strAboutMenu.IsEmpty())
  72. {
  73. pSysMenu->AppendMenu(MF_SEPARATOR);
  74. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  75. }
  76. }
  77. // 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
  78. // 执行此操作
  79. SetIcon(m_hIcon, TRUE); // 设置大图标
  80. SetIcon(m_hIcon, FALSE); // 设置小图标
  81. // TODO: 在此添加额外的初始化代码
  82. m_strFilePath = _T("branchinfocfg.dat");
  83. ReadInfo(m_strFilePath, m_InfoArr);
  84. InitListCtrl(m_InfoArr);
  85. return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
  86. }
  87. void ClyfzBranchInfoCfgDlg::OnSysCommand(UINT nID, LPARAM lParam)
  88. {
  89. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  90. {
  91. CAboutDlg dlgAbout;
  92. dlgAbout.DoModal();
  93. }
  94. else
  95. {
  96. CDialog::OnSysCommand(nID, lParam);
  97. }
  98. }
  99. // 如果向对话框添加最小化按钮,则需要下面的代码
  100. // 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
  101. // 这将由框架自动完成。
  102. void ClyfzBranchInfoCfgDlg::OnPaint()
  103. {
  104. if (IsIconic())
  105. {
  106. CPaintDC dc(this); // 用于绘制的设备上下文
  107. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  108. // 使图标在工作区矩形中居中
  109. int cxIcon = GetSystemMetrics(SM_CXICON);
  110. int cyIcon = GetSystemMetrics(SM_CYICON);
  111. CRect rect;
  112. GetClientRect(&rect);
  113. int x = (rect.Width() - cxIcon + 1) / 2;
  114. int y = (rect.Height() - cyIcon + 1) / 2;
  115. // 绘制图标
  116. dc.DrawIcon(x, y, m_hIcon);
  117. }
  118. else
  119. {
  120. CDialog::OnPaint();
  121. }
  122. }
  123. //当用户拖动最小化窗口时系统调用此函数取得光标
  124. //显示。
  125. HCURSOR ClyfzBranchInfoCfgDlg::OnQueryDragIcon()
  126. {
  127. return static_cast<HCURSOR>(m_hIcon);
  128. }
  129. void ClyfzBranchInfoCfgDlg::InitListCtrl(CArray<CStringArray, CStringArray>& infoArr)
  130. {
  131. int nIndex = 0;
  132. m_BranchInfoList.InsertColumn(nIndex, _T("分店域名"));
  133. m_BranchInfoList.SetColumnWidth(nIndex++, 140);
  134. m_BranchInfoList.InsertColumn(nIndex, _T("店名"));
  135. m_BranchInfoList.SetColumnWidth(nIndex++, 80);
  136. m_BranchInfoList.InsertColumn(nIndex, _T("IP地址"));
  137. m_BranchInfoList.SetColumnWidth(nIndex++, 100);
  138. m_BranchInfoList.InsertColumn(nIndex, _T("是否总店"));
  139. m_BranchInfoList.SetColumnWidth(nIndex++, 60);
  140. m_BranchInfoList.InsertColumn(nIndex, _T("企业名"));
  141. m_BranchInfoList.SetColumnWidth(nIndex++, 80);
  142. m_BranchInfoList.InsertColumn(nIndex, _T("连接权限"));
  143. m_BranchInfoList.SetColumnWidth(nIndex++, 140);
  144. m_BranchInfoList.InsertColumn(nIndex, _T("可访问界面"));
  145. m_BranchInfoList.SetColumnWidth(nIndex++, 120);
  146. m_BranchInfoList.SetExtendedStyle(m_BranchInfoList.GetExtendedStyle() | LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
  147. for(int i=0; i<infoArr.GetSize(); i++)
  148. {
  149. int nidx = 1;
  150. int nRow = m_BranchInfoList.InsertItem(i, infoArr.ElementAt(i).ElementAt(0));
  151. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(1));
  152. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(2));
  153. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(3));
  154. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(4));
  155. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(5));
  156. m_BranchInfoList.SetItemText(nRow, nidx++, infoArr.ElementAt(i).ElementAt(6));
  157. }
  158. }
  159. void ClyfzBranchInfoCfgDlg::ReadInfo(LPCTSTR lpFile, CArray<CStringArray, CStringArray>& infoArr)
  160. {
  161. if(lpFile == NULL)
  162. return;
  163. CFile fp;
  164. if( PathFileExists(lpFile) == 0)
  165. return;
  166. fp.Open(lpFile, CFile::modeRead);
  167. DWORD leng = fp.GetLength();
  168. if (leng < sizeof(DWORD) * 2)
  169. {
  170. fp.Close();
  171. return;
  172. }
  173. DWORD flag = 0;
  174. //fp.Seek(-sizeof(DWORD), CFile::end);
  175. fp.Seek(-(int)sizeof(DWORD), CFile::end);
  176. fp.Read(&flag, sizeof(DWORD));
  177. if (flag != 889900)
  178. {
  179. fp.Close();
  180. return;
  181. }
  182. fp.SeekToBegin();
  183. DWORD arraysize;
  184. fp.Read(&arraysize, sizeof(DWORD));
  185. if (arraysize > 20)
  186. {
  187. fp.Close();
  188. return;
  189. }
  190. BYTE *pData = new BYTE[leng - sizeof(DWORD)];
  191. memset(pData, 0, leng - sizeof(DWORD));
  192. fp.Read(pData, leng - sizeof(DWORD));
  193. fp.Close();
  194. /*test
  195. for(int i=0; i<(leng - sizeof(DWORD)); i++)
  196. {
  197. if(pData[i] == '\n')
  198. MessageBox(_T("存在换行符"));
  199. }
  200. */
  201. CMemFile memfile;
  202. memfile.Attach(pData, leng - sizeof(DWORD));
  203. CArchive ar(&memfile, CArchive::load);
  204. infoArr.SetSize(arraysize, 1);
  205. for (int ii = 0; ii < infoArr.GetSize(); ii++)
  206. infoArr.ElementAt(ii).Serialize(ar);
  207. for(int i=0; i<infoArr.GetSize(); i++)
  208. {
  209. for(int j=0; j<infoArr.ElementAt(i).GetSize(); j++)
  210. {
  211. ::OutputDebugString(infoArr.ElementAt(i).ElementAt(j));
  212. ::OutputDebugString("\n");
  213. }
  214. }
  215. ar.Close();
  216. memfile.Detach();
  217. delete [] pData;
  218. }
  219. //编辑
  220. void ClyfzBranchInfoCfgDlg::OnBnClickedEditButton()
  221. {
  222. // TODO: 在此添加控件通知处理程序代码
  223. if(m_nCurrSel == -1)
  224. return;
  225. CString str = _T("");
  226. if( m_BranchInfoList.GetItemState(m_nCurrSel, LVIS_SELECTED) == LVIS_SELECTED)
  227. {
  228. int nidx = 1;
  229. ((CEdit*)GetDlgItem(IDC_Domain_EDIT))->GetWindowText(str);
  230. int nRow = m_BranchInfoList.InsertItem(m_nCurrSel, str);
  231. ((CEdit*)GetDlgItem(IDC_SHOPNAME_EDIT))->GetWindowText(str);
  232. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  233. ((CEdit*)GetDlgItem(IDC_IPADDR_EDIT))->GetWindowText(str);
  234. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  235. if(((CButton*)GetDlgItem(IDC_IsHQ_CHECK))->GetCheck() == 1)
  236. m_BranchInfoList.SetItemText(nRow, nidx++, _T("总店"));
  237. else
  238. m_BranchInfoList.SetItemText(nRow, nidx++, _T(""));
  239. ((CEdit*)GetDlgItem(IDC_Enterprise_EDIT))->GetWindowText(str);
  240. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  241. ((CEdit*)GetDlgItem(IDC_ConectRight_EDIT))->GetWindowText(str);
  242. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  243. ((CEdit*)GetDlgItem(IDC_Accessible_interfa_EDIT))->GetWindowText(str);
  244. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  245. }
  246. m_nCurrSel = -1;
  247. }
  248. //保存
  249. void ClyfzBranchInfoCfgDlg::OnBnClickedSaveButton()
  250. {
  251. // TODO: 在此添加控件通知处理程序代码
  252. int nSize = m_BranchInfoList.GetItemCount();
  253. if(nSize == 0)
  254. {
  255. MessageBox(_T("没有添加数据,不用保存"));
  256. return;
  257. }
  258. CArray<CStringArray, CStringArray> infoArr;
  259. infoArr.SetSize(nSize, 1);
  260. infoArr.ElementAt(0).Add(_T("20150801095127452308.ly.com"));
  261. infoArr.ElementAt(0).Add(_T("儿童虚拟-1"));
  262. infoArr.ElementAt(0).Add(_T("192.168.220.128"));
  263. infoArr.ElementAt(0).Add(_T("总店"));
  264. infoArr.ElementAt(0).Add(_T("利亚方舟2"));
  265. infoArr.ElementAt(0).Add(_T("儿童虚拟-1"));
  266. infoArr.ElementAt(0).Add(_T("儿童本地-2"));
  267. infoArr.ElementAt(1).Add(_T("20151007104526521757.ly.com"));
  268. infoArr.ElementAt(1).Add(_T("儿童虚拟-2"));
  269. infoArr.ElementAt(1).Add(_T("192.168.1.115"));
  270. infoArr.ElementAt(1).Add(_T("总店"));
  271. infoArr.ElementAt(1).Add(_T("利亚方舟2"));
  272. infoArr.ElementAt(1).Add(_T("儿童虚拟-1"));
  273. infoArr.ElementAt(1).Add(_T("儿童本地-2"));
  274. // 创建内存文件;
  275. CMemFile MembranchInfo;
  276. CArchive ar(&MembranchInfo,CArchive::store);
  277. for ( int i = 0; i < nSize; i++)
  278. {
  279. // 将数组序列化到对象中;
  280. infoArr.ElementAt(i).Serialize(ar);
  281. }
  282. ar.Close();
  283. DWORD fLength = MembranchInfo.GetLength();
  284. PBYTE pData = MembranchInfo.Detach();
  285. CFile fb;
  286. if (fb.Open(m_strFilePath, CFile::modeCreate|CFile::modeWrite))
  287. {
  288. fb.Write(&nSize,sizeof(DWORD)); // 写入数组个数;
  289. fb.Write(pData,fLength); // 写入数组内容;
  290. DWORD dwFlag = 889900;
  291. fb.Write(&dwFlag,sizeof(DWORD)); // 写入结束标记;
  292. fb.Close();
  293. }
  294. MessageBox(_T("保存成功,需要重启服务端才能生效!"));
  295. }
  296. void ClyfzBranchInfoCfgDlg::OnNMClickBranchinfoList(NMHDR *pNMHDR, LRESULT *pResult)
  297. {
  298. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  299. // TODO: 在此添加控件通知处理程序代码
  300. m_nCurrSel = pNMItemActivate->iItem;
  301. if(m_nCurrSel != -1)
  302. {
  303. m_BranchInfoList.SetItemState(m_nCurrSel, LVIS_FOCUSED | LVIS_SELECTED,LVIS_FOCUSED | LVIS_SELECTED);
  304. ((CEdit*)GetDlgItem(IDC_Domain_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 0));
  305. ((CEdit*)GetDlgItem(IDC_SHOPNAME_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 1));
  306. ((CEdit*)GetDlgItem(IDC_IPADDR_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 2));
  307. CString strIsHQ = m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 3);
  308. if(strIsHQ == _T("总店"))
  309. ((CButton*)GetDlgItem(IDC_IsHQ_CHECK))->SetCheck(1);
  310. else
  311. ((CButton*)GetDlgItem(IDC_IsHQ_CHECK))->SetCheck(0);
  312. ((CEdit*)GetDlgItem(IDC_Enterprise_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 4));
  313. ((CEdit*)GetDlgItem(IDC_ConectRight_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 5));
  314. ((CEdit*)GetDlgItem(IDC_Accessible_interfa_EDIT))->SetWindowText(m_BranchInfoList.GetItemText(pNMItemActivate->iItem, 6));
  315. }
  316. *pResult = 0;
  317. }
  318. void ClyfzBranchInfoCfgDlg::OnLvnItemchangedBranchinfoList(NMHDR *pNMHDR, LRESULT *pResult)
  319. {
  320. LPNMLISTVIEW pNMLV = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
  321. // TODO: 在此添加控件通知处理程序代码
  322. *pResult = 0;
  323. }
  324. //添加
  325. void ClyfzBranchInfoCfgDlg::OnBnClickedAddButton()
  326. {
  327. // TODO: 在此添加控件通知处理程序代码
  328. int i = m_BranchInfoList.GetItemCount();
  329. int nidx = 1;
  330. CString str = _T("");
  331. ((CEdit*)GetDlgItem(IDC_Domain_EDIT))->GetWindowText(str);
  332. int nRow = m_BranchInfoList.InsertItem(i, str);
  333. ((CEdit*)GetDlgItem(IDC_SHOPNAME_EDIT))->GetWindowText(str);
  334. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  335. ((CEdit*)GetDlgItem(IDC_IPADDR_EDIT))->GetWindowText(str);
  336. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  337. if(((CButton*)GetDlgItem(IDC_IsHQ_CHECK))->GetCheck() == 1)
  338. m_BranchInfoList.SetItemText(nRow, nidx++, _T("总店"));
  339. else
  340. m_BranchInfoList.SetItemText(nRow, nidx++, _T(""));
  341. ((CEdit*)GetDlgItem(IDC_Enterprise_EDIT))->GetWindowText(str);
  342. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  343. ((CEdit*)GetDlgItem(IDC_ConectRight_EDIT))->GetWindowText(str);
  344. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  345. ((CEdit*)GetDlgItem(IDC_Accessible_interfa_EDIT))->GetWindowText(str);
  346. m_BranchInfoList.SetItemText(nRow, nidx++, str);
  347. }
  348. //删除
  349. void ClyfzBranchInfoCfgDlg::OnBnClickedDelButton()
  350. {
  351. // TODO: 在此添加控件通知处理程序代码
  352. if(m_nCurrSel != -1)
  353. {
  354. m_BranchInfoList.DeleteItem(m_nCurrSel);
  355. ((CEdit*)GetDlgItem(IDC_Domain_EDIT))->SetWindowText(_T(""));
  356. ((CEdit*)GetDlgItem(IDC_SHOPNAME_EDIT))->SetWindowText(_T(""));
  357. ((CEdit*)GetDlgItem(IDC_IPADDR_EDIT))->SetWindowText(_T(""));
  358. ((CButton*)GetDlgItem(IDC_IsHQ_CHECK))->SetCheck(0);
  359. ((CEdit*)GetDlgItem(IDC_Enterprise_EDIT))->SetWindowText(_T(""));
  360. ((CEdit*)GetDlgItem(IDC_ConectRight_EDIT))->SetWindowText(_T(""));
  361. ((CEdit*)GetDlgItem(IDC_Accessible_interfa_EDIT))->SetWindowText(_T(""));
  362. }
  363. }