一段代码和四/五子棋以及成员信息表(学生信息管理系统)的云盘链接

  1. // ListCtrlDlg.cpp : implementation file
  2. //
  3. #include 'stdafx.h'
  4. #include 'ListCtrl.h'
  5. #include 'ListCtrlDlg.h'
  6. #include 'CApplication.h'
  7. #include 'CRange.h'
  8. #include 'CWorkbooks.h'
  9. #include 'CWorksheets.h'
  10. #include 'CWorkbook.h'
  11. #include 'CWorksheet.h'
  12. #include 'afxdialogex.h'
  13. #include <afxdb.h>
  14. #include <Afxdlgs.h>
  15. #include 'comutil.h'
  16. //#include 'ListCtrl.rc'
  17. #pragma comment(lib, 'comsupp.lib')
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /
  24. // CAboutDlg dialog used for App About
  25. class CAboutDlg : public CDialog
  26. {
  27. public:
  28. CAboutDlg();
  29. // Dialog Data
  30. //{{AFX_DATA(CAboutDlg)
  31. enum { IDD = IDD_ABOUTBOX };
  32. //}}AFX_DATA
  33. // ClassWizard generated virtual function overrides
  34. //{{AFX_VIRTUAL(CAboutDlg)
  35. protected:
  36. virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
  37. //}}AFX_VIRTUAL
  38. // Implementation
  39. protected:
  40. //{{AFX_MSG(CAboutDlg)
  41. //}}AFX_MSG
  42. DECLARE_MESSAGE_MAP()
  43. };
  44. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  45. {
  46. //{{AFX_DATA_INIT(CAboutDlg)
  47. //}}AFX_DATA_INIT
  48. }
  49. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  50. {
  51. CDialog::DoDataExchange(pDX);
  52. //{{AFX_DATA_MAP(CAboutDlg)
  53. //}}AFX_DATA_MAP
  54. }
  55. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  56. //{{AFX_MSG_MAP(CAboutDlg)
  57. // No message handlers
  58. //}}AFX_MSG_MAP
  59. END_MESSAGE_MAP()
  60. /
  61. // CListCtrlDlg dialog
  62. CListCtrlDlg::CListCtrlDlg(CWnd* pParent /*=NULL*/)
  63. : CDialog(CListCtrlDlg::IDD)
  64. {
  65. //{{AFX_DATA_INIT(CListCtrlDlg)
  66. // NOTE: the ClassWizard will add member initialization here
  67. //}}AFX_DATA_INIT
  68. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  69. m_iItemCount = 0;
  70. m_Index.first = 0;
  71. m_Index.second = 0;
  72. m_vecSerach.clear();
  73. pDlg = NULL;
  74. iSerachIndex = 0;
  75. mapAllData.RemoveAll();
  76. mapCheckData.RemoveAll();
  77. pColName = NULL;
  78. }
  79. void CListCtrlDlg::DoDataExchange(CDataExchange* pDX)
  80. {
  81. CDialog::DoDataExchange(pDX);
  82. //{{AFX_DATA_MAP(CListCtrlDlg)
  83. DDX_Control(pDX, IDC_LIST1, m_List);
  84. //}}AFX_DATA_MAP
  85. // DDX_Text(pDX, IDC_EDIT_CHARU, m_charu);
  86. DDX_Control(pDX, IDC_EDIT_CHARU, m_charu);
  87. }
  88. BEGIN_MESSAGE_MAP(CListCtrlDlg, CDialog)
  89. //{{AFX_MSG_MAP(CListCtrlDlg)
  90. ON_WM_SYSCOMMAND()
  91. ON_WM_PAINT()
  92. ON_WM_QUERYDRAGICON()
  93. ON_COMMAND(ID_LST_DELETE, OnLstDelete)
  94. ON_COMMAND(ID_LST_INSERT, OnLstInsert)
  95. ON_COMMAND(ID_LST_MINISIZE, OnLstMinisizeScreen)
  96. ON_COMMAND(ID_LST_MAXISIZE,OnLstMaxisizeScreen)
  97. ON_COMMAND(ID_LST_Display, DisplayAuthor)
  98. ON_NOTIFY(NM_RCLICK, IDC_LIST, OnRclickList)
  99. //}}AFX_MSG_MAP
  100. ON_MESSAGE(WM_VALIDATE, OnEndLabelEditVariableCriteria)
  101. ON_MESSAGE(WM_SET_ITEMS, PopulateComboList)
  102. ON_BN_CLICKED(IDC_BUTTON_SAVE, &CListCtrlDlg::OnButtonSave)
  103. ON_BN_CLICKED(IDC_BUTTON_LINGSAVE, &CListCtrlDlg::OnButtonLingsave)
  104. ON_BN_CLICKED(IDC_BUTTON_EXIT, &CListCtrlDlg::OnButtonExit)
  105. ON_BN_CLICKED(IDC_BUTTON_INDEX, &CListCtrlDlg::OnButtonIndex)
  106. ON_BN_CLICKED(IDC_BUTTON_ADD, &CListCtrlDlg::OnButtonAdd)
  107. ON_COMMAND(ID_XIU, &CListCtrlDlg::OnButtonXiu)
  108. ON_BN_CLICKED(IDC_BUTTON_DELETE, &CListCtrlDlg::OnButtonDelete)
  109. ON_BN_CLICKED(IDC_BUTTON_OPEN, &CListCtrlDlg::OnButtonOpen)
  110. ON_NOTIFY(NM_DBLCLK, IDC_LIST, &CListCtrlDlg::OnDblclkList)
  111. ON_WM_SIZE()
  112. ON_BN_CLICKED(IDC_BUTTON_FILTER, &CListCtrlDlg::OnBnClickedButtonFilter)
  113. ON_MESSAGE(WM_USER_UP_SERACH, &CListCtrlDlg::OnUserUpSerach)
  114. ON_MESSAGE(WM_USER_DOWN_SERACH, &CListCtrlDlg::OnUserDownSerach)
  115. ON_BN_CLICKED(IDC_BUTTON_IMPORT, &CListCtrlDlg::OnBnClickedButtonImport)
  116. END_MESSAGE_MAP()
  117. /
  118. // CListCtrlDlg message handlers
  119. BOOL CListCtrlDlg::OnInitDialog()
  120. {
  121. CDialog::OnInitDialog();
  122. // Add 'About...' menu item to system menu.
  123. // IDM_ABOUTBOX must be in the system command range.
  124. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  125. ASSERT(IDM_ABOUTBOX < 0xF000);
  126. CMenu* pSysMenu = GetSystemMenu(FALSE);
  127. if (pSysMenu != NULL)
  128. {
  129. CString strAboutMenu;
  130. strAboutMenu.LoadString(IDS_ABOUTBOX);
  131. if (!strAboutMenu.IsEmpty())
  132. {
  133. pSysMenu->AppendMenu(MF_SEPARATOR);
  134. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  135. }
  136. }
  137. m_hIcon1 = AfxGetApp()->LoadIcon(IDI_ICON1);
  138. SetIcon(m_hIcon1, TRUE);
  139. SetIcon(m_hIcon1, FALSE);
  140. // Set the icon for this dialog. The framework does this automatically
  141. // when the application's main window is not a dialog
  142. //SetIcon(m_hIcon, TRUE);// Set big icon
  143. //SetIcon(m_hIcon, FALSE);// Set small icon
  144. // TODO: Add extra initialization here
  145. m_charu.ShowWindow(SW_HIDE);
  146. // Make the necessary initializations
  147. length = rect.Width();
  148. m_List.GetClientRect(&rect);
  149. m = 0;
  150. //初始化列表框
  151. m_List.ModifyStyle(0, LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS);
  152. m_List.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP);
  153. //显示字体的颜色
  154. m_List.SetTextColor(RGB(0, 0, 0));
  155. m_List.InsertColumn(m, '序号', LVCFMT_CENTER, 70);
  156. m_List.InsertColumn(m + 1, '姓名', LVCFMT_CENTER, 125);
  157. m_List.InsertColumn(m + 2, '学号', LVCFMT_CENTER, 145);
  158. m_List.InsertColumn(m + 3, '班级', LVCFMT_CENTER, 335);
  159. m_List.SetReadOnlyColumns(0);
  160. //m_List.SetComboColumns(1);
  161. CString strValidChars = '';
  162. m_List.SetValidEditCtrlCharacters(strValidChars);
  163. m_List.EnableVScroll();
  164. int iCount = m + 4; //列总数
  165. //pColName = new CString[iCount];
  166. pColName = new CString[20];
  167. pColName[0]= '序号';
  168. pColName[1]= '姓名';
  169. pColName[2]= '学号';
  170. pColName[3]= '班级';
  171. // 执行此操作
  172. SetIcon(m_hIcon1, TRUE);// 设置大图标
  173. SetIcon(m_hIcon1, FALSE);// 设置小图标
  174. CRect rc;
  175. GetClientRect(&rc);//获取对话框客户区大小
  176. GetDlgItem(IDC_BUTTON_OPEN)->ShowWindow(SW_HIDE);
  177. return TRUE; // return TRUE unless you set the focus to a control
  178. }
  179. void CListCtrlDlg::OnSysCommand(UINT nID, LPARAM lParam)
  180. {
  181. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  182. {
  183. CAboutDlg dlgAbout;
  184. dlgAbout.DoModal();
  185. }
  186. else
  187. {
  188. CDialog::OnSysCommand(nID, lParam);
  189. }
  190. }
  191. // If you add a minimize button to your dialog, you will need the code below
  192. // to draw the icon. For MFC applications using the document/view model,
  193. // this is automatically done for you by the framework.
  194. void CListCtrlDlg::OnPaint()
  195. {
  196. // 不为绘图消息调用 CDialogEx::OnPaint()
  197. if (IsIconic())
  198. {
  199. CPaintDC dc(this); // 用于绘制的设备上下文
  200. SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
  201. // 使图标在工作区矩形中居中
  202. int cxIcon = GetSystemMetrics(SM_CXICON);
  203. int cyIcon = GetSystemMetrics(SM_CYICON);
  204. CRect rect;
  205. GetClientRect(&rect);
  206. int x = (rect.Width() - cxIcon + 1) / 2;
  207. int y = (rect.Height() - cyIcon + 1) / 2;
  208. // 绘制图标
  209. dc.DrawIcon(x, y, m_hIcon1);
  210. }
  211. else
  212. {
  213. CDialog::OnPaint();
  214. CDialog::UpdateWindow(); //UpdateWindow一下
  215. }
  216. }
  217. // The system calls this to obtain the cursor to display while the user drags
  218. // the minimized window.
  219. HCURSOR CListCtrlDlg::OnQueryDragIcon()
  220. {
  221. return (HCURSOR) m_hIcon1;
  222. }
  223. //void CListCtrlDlg::ReadVersion(CString& strVersion, CString& strCopyriht)
  224. //{
  225. //char szAppFullPath[_MAX_PATH] = { 0 };
  226. //::GetModuleFileName(NULL, szAppFullPath, _MAX_PATH);
  227. //
  228. //
  229. //DWORD dwLen = GetFileVersionInfoSize(szAppFullPath, NULL);
  230. //char *pszAppVersion = new char[dwLen + 1];
  231. //if (NULL != pszAppVersion)
  232. //{
  233. //ZeroMemory(pszAppVersion, dwLen + 1);
  234. //::GetFileVersionInfo(szAppFullPath, 0, dwLen, pszAppVersion);
  235. //
  236. //UINT nLen = 0;
  237. //char *pInfo = NULL;
  238. //CString strVersionValues;
  239. //struct LANGANDCODEPAGE {
  240. //WORD wLanguage;
  241. //WORD wCodePage;
  242. //} *lpTranslate;
  243. //
  244. //VerQueryValue(pszAppVersion, '\\VarFileInfo\\Translation', (LPVOID*)&lpTranslate, &nLen);
  245. //strVersionValues.Format(_T('\\StringFileInfo\\%04x%04x\\FileVersion'),
  246. //lpTranslate->wLanguage, lpTranslate->wCodePage);
  247. //VerQueryValue(pszAppVersion, strVersionValues.GetBuffer(strVersionValues.GetLength()),
  248. //(LPVOID*)&pInfo, &nLen);
  249. //strVersion = pInfo;
  250. //
  251. //strVersionValues.Format(_T('\\StringFileInfo\\%04x%04x\\LegalCopyright'),
  252. //lpTranslate->wLanguage, lpTranslate->wCodePage);
  253. //VerQueryValue(pszAppVersion, strVersionValues.GetBuffer(strVersionValues.GetLength()),
  254. //(LPVOID*)&pInfo, &nLen);
  255. //m_strCopyriht = pInfo;
  256. //
  257. //delete[] pszAppVersion;
  258. //pszAppVersion = NULL;
  259. //lpTranslate = NULL;
  260. //pInfo = NULL;
  261. //}
  262. //}
  263. void CListCtrlDlg::OnLstDelete()
  264. {
  265. // TODO: Add your command handler code here
  266. // Get the current selected item
  267. POSITION SelectionPos = m_List.GetFirstSelectedItemPosition();
  268. int iCurSel = -1;
  269. // Delete the selected item from the list control
  270. while (NULL != SelectionPos)
  271. {
  272. SelectionPos = m_List.GetFirstSelectedItemPosition();
  273. iCurSel = m_List.GetNextSelectedItem(SelectionPos);
  274. m_List.DeleteItem(iCurSel);
  275. }
  276. }
  277. void CListCtrlDlg::OnLstInsert()
  278. {
  279. //TODO: Add your command handler code here
  280. //Insert a new row below the selected row or at the end
  281. CString strResource;
  282. strResource.Format('%d', ++m_iItemCount);
  283. int iCount = m_List.GetItemCount();
  284. int iRow = -1;
  285. POSITION SelectedItemPos = m_List.GetFirstSelectedItemPosition();
  286. int iIndex = -1;
  287. int iLastSelectedIndex = -1;
  288. while (SelectedItemPos)
  289. {
  290. iIndex = m_List.GetNextSelectedItem(SelectedItemPos);
  291. if ((LVIS_SELECTED == m_List.GetItemState(iIndex, LVIS_SELECTED)) &&
  292. (LVIS_FOCUSED == m_List.GetItemState(iIndex, LVIS_FOCUSED)))
  293. {
  294. iRow = iIndex;
  295. }
  296. m_List.SetItemState(iIndex, 0, LVIS_SELECTED|LVIS_FOCUSED);
  297. }
  298. if (-1 == iRow)
  299. {
  300. m_List.InsertItem(LVIF_TEXT | LVIF_STATE, iCount,
  301. strResource, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED, 0, 0);
  302. }
  303. else
  304. {
  305. m_List.InsertItem(LVIF_TEXT | LVIF_STATE, iRow,
  306. strResource, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED, 0, 0);
  307. }
  308. UpdateData(FALSE);
  309. SortFirstColumn();
  310. }
  311. void CListCtrlDlg::OnButtonXiu()
  312. {
  313. // TODO: 在此添加命令处理程序代码
  314. int nResult = ::AfxMessageBox('是否修改', MB_YESNO);
  315. if (nResult == IDNO)
  316. {
  317. return;
  318. }
  319. int iCount = m_List.GetItemCount();
  320. int iRow = -1;
  321. CString strResource;
  322. strResource.Format('%d', ++m_iItemCount);
  323. POSITION SelectedItemPos = m_List.GetFirstSelectedItemPosition();
  324. int iIndex = -1;
  325. int iLastSelectedIndex = -1;
  326. while (SelectedItemPos)
  327. {
  328. iIndex = m_List.GetNextSelectedItem(SelectedItemPos);
  329. if ((LVIS_SELECTED == m_List.GetItemState(iIndex, LVIS_SELECTED)) &&
  330. (LVIS_FOCUSED == m_List.GetItemState(iIndex, LVIS_FOCUSED)))
  331. {
  332. iRow = iIndex;
  333. }
  334. m_List.SetItemState(iIndex - 1, 0, LVIS_SELECTED | LVIS_FOCUSED);
  335. }
  336. m_List.InsertItem(LVIF_TEXT | LVIF_STATE, iCount,
  337. strResource, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED, 0, 0);
  338. }
  339. void CListCtrlDlg::OnLstMinisizeScreen()
  340. {
  341. ShowWindow(SW_MINIMIZE);
  342. }
  343. void CListCtrlDlg::OnLstMaxisizeScreen()
  344. {
  345. ShowWindow(SW_MAXIMIZE);
  346. }
  347. void CListCtrlDlg::OnRclickList(NMHDR* pNMHDR, LRESULT* pResult)
  348. {
  349. // TODO: Add your control notification handler code here
  350. // Create the pop up menu
  351. CMenu obMenu;
  352. obMenu.LoadMenu(IDR_LIST_POPUP);
  353. CMenu* pPopupMenu = obMenu.GetSubMenu(0);
  354. ASSERT(pPopupMenu);
  355. // Get the cursor position
  356. CPoint obCursorPoint = (0, 0);
  357. GetCursorPos(&obCursorPoint);
  358. if (0 >= m_List.GetSelectedCount())
  359. {
  360. pPopupMenu->EnableMenuItem(ID_LST_DELETE, MF_BYCOMMAND | MF_GRAYED | MF_DISABLED);
  361. }
  362. else
  363. {
  364. pPopupMenu->EnableMenuItem(ID_LST_DELETE, MF_BYCOMMAND | MF_ENABLED);
  365. }
  366. // Track the popup menu
  367. pPopupMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_RIGHTBUTTON, obCursorPoint.x,
  368. obCursorPoint.y, this);
  369. *pResult = 0;
  370. }
  371. LRESULT CListCtrlDlg::OnEndLabelEditVariableCriteria(WPARAM wParam, LPARAM lParam)
  372. {
  373. LV_DISPINFO* pDispInfo = (LV_DISPINFO*)lParam;
  374. // TODO: Add your control notification handler code here
  375. if (wParam == IDC_LIST)
  376. {
  377. // Update the item text with the new text
  378. CString strUpdatedTxt = pDispInfo->item.pszText;
  379. // Validate the format of the new string for the edit columns
  380. // If invalid then
  381. // Set pResult to 1
  382. switch(pDispInfo->item.iSubItem)
  383. {
  384. case 0:
  385. case 1:
  386. break;
  387. default:
  388. {
  389. if (!strUpdatedTxt.IsEmpty())
  390. {
  391. // Get the left most non numeral characters
  392. // Get the remaining numerals
  393. // If the left most part does not contain the mandatory conditions
  394. // Or the rest contains the mandatory conditions
  395. // Display an error and set focus back onto the control
  396. int iFirstNumeralOccurrance = strUpdatedTxt.FindOneOf('');
  397. CString strNonNumerals = strUpdatedTxt.Left(iFirstNumeralOccurrance);
  398. CString strNumerals = strUpdatedTxt.Mid(iFirstNumeralOccurrance);
  399. strNonNumerals.TrimLeft();
  400. strNonNumerals.TrimRight();
  401. strNumerals.TrimLeft();
  402. strNumerals.TrimRight();
  403. int iDecimalIndex = strNumerals.Find('.');
  404. if ((-1 != iDecimalIndex) && (iDecimalIndex != (strNumerals.GetLength() - 1)))
  405. {
  406. iDecimalIndex = strNumerals.Find('.', iDecimalIndex + 1);
  407. }
  408. // Check that the condition is either
  409. // Not empty or '=' or '!=' or '<' or '>' or '<=' or '>='
  410. // The Numerals do not contain
  411. // Space or more than 1 '.' or any of the conditions
  412. if (
  413. (!strNonNumerals.IsEmpty()) ||
  414. (-1 != strNumerals.Find(' ')) ||
  415. (-1 != iDecimalIndex)
  416. )
  417. {
  418. AfxMessageBox('插入格式不对,数据将恢复!');
  419. m_List.SetItemText(pDispInfo->item.iItem, pDispInfo->item.iSubItem, '');
  420. }
  421. return 0;
  422. }
  423. }
  424. break;
  425. }
  426. }
  427. return 1;
  428. }
  429. LRESULT CListCtrlDlg::PopulateComboList(WPARAM wParam, LPARAM lParam)
  430. {
  431. // Get the Combobox window pointer
  432. CComboBox* pInPlaceCombo = static_cast<CComboBox*> (GetFocus());
  433. // Get the inplace combbox top left
  434. CRect obWindowRect;
  435. pInPlaceCombo->GetWindowRect(&obWindowRect);
  436. CPoint obInPlaceComboTopLeft(obWindowRect.TopLeft());
  437. // Get the active list
  438. // Get the control window rect
  439. // If the inplace combobox top left is in the rect then
  440. // The control is the active control
  441. m_List.GetWindowRect(&obWindowRect);
  442. int iColIndex = wParam;
  443. CStringList* pComboList = reinterpret_cast<CStringList*>(lParam);
  444. pComboList->RemoveAll();
  445. if (obWindowRect.PtInRect(obInPlaceComboTopLeft))
  446. {
  447. if (1 == iColIndex)
  448. {
  449. pComboList->AddTail('男');
  450. pComboList->AddTail('女');
  451. }
  452. }
  453. return true;
  454. //return 0;
  455. }
  456. void CListCtrlDlg::OnButtonSave()
  457. {
  458. // TODO: 在此添加控件通知处理程序代码
  459. //首先判断列表框中是否有记录
  460. if (m_List.GetItemCount() <= 0)
  461. {
  462. AfxMessageBox(_T('列表中没有记录需要保存!'));
  463. return;
  464. }
  465. else
  466. {
  467. if (AfxMessageBox('确定要保存吗?', MB_OKCANCEL) == IDOK)
  468. {
  469. CString stText;
  470. stText = '学生信息';
  471. CString cStrFile;//%d年%d月%d日%d时%d分%d秒
  472. CTime m_time = CTime::GetCurrentTime();
  473. cStrFile.Format('C:\\' + stText + '-' + '%d年%d月%d日%d时%d分%d秒.xls', m_time.GetYear(), m_time.GetMonth(), m_time.GetDay(), m_time.GetHour(), m_time.GetMinute(),
  474. m_time.GetSecond());
  475. if (::PathFileExists(cStrFile))
  476. DeleteFile(cStrFile);
  477. COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  478. CApplication app; //Excel程序
  479. CWorkbooks books; //工作簿集合
  480. CWorkbook book; //工作表
  481. CWorksheets sheets; //工作簿集合
  482. CWorksheet sheet; //工作表集合
  483. CRange range; //使用区域
  484. book.PrintPreview(_variant_t(false));
  485. if (!app.CreateDispatch(_T('Excel.Application')))//创建IDispatch接口对象
  486. {
  487. AfxMessageBox(_T('保存失败!'));
  488. }
  489. else
  490. {
  491. AfxMessageBox(_T('保存成功!'));
  492. }
  493. books = app.get_Workbooks();
  494. book = books.Add(covOptional);
  495. sheets = book.get_Worksheets();
  496. sheet = sheets.get_Item(COleVariant((short)1)); //得到第一个工作表
  497. CHeaderCtrl *pmyHeaderCtrl = m_List.GetHeaderCtrl(); //获取表头
  498. int m_cols = pmyHeaderCtrl->GetItemCount(); //获取列数
  499. int m_rows = m_List.GetItemCount(); //获取行数
  500. TCHAR lpBuffer[256];
  501. HDITEM hdi; //This structure contains information about an item in a header control. This structure has been updated to support header item images and order values.
  502. hdi.mask = HDI_TEXT;
  503. hdi.pszText = lpBuffer;
  504. hdi.cchTextMax = 256;
  505. CString colname;
  506. CString strTemp;
  507. int iRow, iCol;
  508. for (iCol = 0; iCol < m_cols; iCol++)//将列表的标题头写入EXCEL
  509. {
  510. GetCellName(1, iCol + 1, colname); //(colname就是对应表格的A1,B1,C1,D1)
  511. range = sheet.get_Range(COleVariant(colname), COleVariant(colname));
  512. pmyHeaderCtrl->GetItem(iCol, &hdi); //获取表头每列的信息
  513. range.put_Value2(COleVariant(hdi.pszText)); //设置每列的内容
  514. int nWidth = m_List.GetColumnWidth(iCol) / 6;
  515. //得到第iCol+1列
  516. range.AttachDispatch(range.get_Item(_variant_t((long)(iCol + 1)), vtMissing).pdispVal, true);
  517. //设置列宽
  518. range.put_ColumnWidth(_variant_t((long)nWidth));
  519. }
  520. range = sheet.get_Range(COleVariant(_T('A1 ')), COleVariant(colname));
  521. range.put_RowHeight(_variant_t((long)50));//设置行的高度
  522. range.put_VerticalAlignment(COleVariant((short)-4108));//xlVAlignCenter = -4108
  523. COleSafeArray saRet; //COleSafeArray类是用于处理任意类型和维数的数组的类
  524. DWORD numElements[] = { m_rows, m_cols }; //行列写入数组
  525. saRet.Create(VT_BSTR, 2, numElements); //创建所需的数组
  526. range = sheet.get_Range(COleVariant(_T('A2 ')), covOptional); //从A2开始
  527. range = range.get_Resize(COleVariant((short)m_rows), COleVariant((short)m_cols)); //表的区域
  528. long index[2];
  529. for (iRow = 1; iRow <= m_rows; iRow++)//将列表内容写入EXCEL
  530. {
  531. for (iCol = 1; iCol <= m_cols; iCol++)
  532. {
  533. index[0] = iRow - 1;
  534. index[1] = iCol - 1;
  535. CString szTemp;
  536. szTemp = m_List.GetItemText(iRow - 1, iCol - 1); //取得m_list控件中的内容
  537. BSTR bstr = szTemp.AllocSysString(); //The AllocSysString method alloscates a new BSTR string that is Automation compatible
  538. saRet.PutElement(index, bstr); //把m_list控件中的内容放入saRet
  539. SysFreeString(bstr);
  540. }
  541. }
  542. range.put_Value2(COleVariant(saRet)); //将得到的数据的saRet数组值放入表格
  543. book.SaveCopyAs(COleVariant(cStrFile)); //保存到cStrFile文件
  544. book.put_Saved(true);
  545. books.Close();
  546. //释放对象
  547. range.ReleaseDispatch();
  548. sheet.ReleaseDispatch();
  549. sheets.ReleaseDispatch();
  550. book.ReleaseDispatch();
  551. books.ReleaseDispatch();
  552. //调用顺序不能变
  553. app.Quit();
  554. app.ReleaseDispatch();
  555. }
  556. else
  557. {
  558. return;
  559. }
  560. }
  561. }
  562. void CListCtrlDlg::OnButtonLingsave()
  563. {
  564. // TODO: 在此添加控件通知处理程序代码
  565. //首先判断列表框中是否有记录
  566. if (m_List.GetItemCount() <= 0)
  567. {
  568. AfxMessageBox(_T('列表中没有记录需要保存!'));
  569. return;
  570. }
  571. else
  572. {
  573. if (AfxMessageBox('确定要保存吗?', MB_OKCANCEL) == IDOK)
  574. {
  575. CFileDialog FileDialog(FALSE, 'xls', NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, _T('Microsoft Excel(*.xls)|*.xls|所有文件(*.*)'), this);
  576. if (FileDialog.DoModal() != IDOK)
  577. {
  578. return;
  579. }
  580. CString cStrFile = FileDialog.GetPathName(); //选择保存路径名称
  581. if (::PathFileExists(cStrFile))
  582. DeleteFile(cStrFile);
  583. COleVariant covTrue((short)TRUE), covFalse((short)FALSE), covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
  584. CApplication app; //Excel程序
  585. CWorkbooks books; //工作簿集合
  586. CWorkbook book; //工作表
  587. CWorksheets sheets; //工作簿集合
  588. CWorksheet sheet; //工作表集合
  589. CRange range; //使用区域
  590. book.PrintPreview(_variant_t(false));
  591. if (!app.CreateDispatch(_T('Excel.Application')))//创建IDispatch接口对象
  592. {
  593. AfxMessageBox(_T('保存失败!'));
  594. }
  595. else
  596. {
  597. AfxMessageBox(_T('保存成功!'));
  598. }
  599. books = app.get_Workbooks();
  600. book = books.Add(covOptional);
  601. sheets = book.get_Worksheets();
  602. sheet = sheets.get_Item(COleVariant((short)1)); //得到第一个工作表
  603. CHeaderCtrl *pmyHeaderCtrl = m_List.GetHeaderCtrl(); //获取表头
  604. int m_cols = pmyHeaderCtrl->GetItemCount(); //获取列数
  605. int m_rows = m_List.GetItemCount(); //获取行数
  606. TCHAR lpBuffer[256];
  607. HDITEM hdi; //This structure contains information about an item in a header control. This structure has been updated to support header item images and order values.
  608. hdi.mask = HDI_TEXT;
  609. hdi.pszText = lpBuffer;
  610. hdi.cchTextMax = 256;
  611. CString colname;
  612. CString strTemp;
  613. int iRow, iCol;
  614. for (iCol = 0; iCol < m_cols; iCol++)//将列表的标题头写入EXCEL
  615. {
  616. GetCellName(1, iCol + 1, colname); //(colname就是对应表格的A1,B1,C1,D1)
  617. range = sheet.get_Range(COleVariant(colname), COleVariant(colname));
  618. pmyHeaderCtrl->GetItem(iCol, &hdi); //获取表头每列的信息
  619. range.put_Value2(COleVariant(hdi.pszText)); //设置每列的内容
  620. int nWidth = m_List.GetColumnWidth(iCol) / 6;
  621. //得到第iCol+1列
  622. range.AttachDispatch(range.get_Item(_variant_t((long)(iCol + 1)), vtMissing).pdispVal, true);
  623. //设置列宽
  624. range.put_ColumnWidth(_variant_t((long)nWidth));
  625. }
  626. range = sheet.get_Range(COleVariant(_T('A1 ')), COleVariant(colname));
  627. range.put_RowHeight(_variant_t((long)50));//设置行的高度
  628. range.put_VerticalAlignment(COleVariant((short)-4108));//xlVAlignCenter = -4108
  629. COleSafeArray saRet; //COleSafeArray类是用于处理任意类型和维数的数组的类
  630. DWORD numElements[] = { m_rows, m_cols }; //行列写入数组
  631. saRet.Create(VT_BSTR, 2, numElements); //创建所需的数组
  632. range = sheet.get_Range(COleVariant(_T('A2 ')), covOptional); //从A2开始
  633. range = range.get_Resize(COleVariant((short)m_rows), COleVariant((short)m_cols)); //表的区域
  634. long index[2];
  635. for (iRow = 1; iRow <= m_rows; iRow++)//将列表内容写入EXCEL
  636. {
  637. for (iCol = 1; iCol <= m_cols; iCol++)
  638. {
  639. index[0] = iRow - 1;
  640. index[1] = iCol - 1;
  641. CString szTemp;
  642. szTemp = m_List.GetItemText(iRow - 1, iCol - 1); //取得m_list控件中的内容
  643. BSTR bstr = szTemp.AllocSysString(); //The AllocSysString method alloscates a new BSTR string that is Automation compatible
  644. saRet.PutElement(index, bstr); //把m_list控件中的内容放入saRet
  645. SysFreeString(bstr);
  646. }
  647. }
  648. range.put_Value2(COleVariant(saRet)); //将得到的数据的saRet数组值放入表格
  649. book.SaveCopyAs(COleVariant(cStrFile)); //保存到cStrFile文件
  650. book.put_Saved(true);
  651. books.Close();
  652. //释放对象
  653. range.ReleaseDispatch();
  654. sheet.ReleaseDispatch();
  655. sheets.ReleaseDispatch();
  656. book.ReleaseDispatch();
  657. books.ReleaseDispatch();
  658. //调用顺序不能变
  659. app.Quit();
  660. app.ReleaseDispatch();
  661. }
  662. else
  663. {
  664. return;
  665. }
  666. }
  667. }
  668. void CListCtrlDlg::OnButtonExit()
  669. {
  670. // TODO: 在此添加控件通知处理程序代码
  671. //
  672. if (pDlg != NULL)
  673. {
  674. pDlg->OnDestroy();
  675. pDlg = NULL;
  676. }
  677. if(pColName != NULL)
  678. delete[] pColName;
  679. CListCtrlDlg::OnOK();
  680. }
  681. void CListCtrlDlg::OnButtonIndex()
  682. {
  683. // TODO: 在此添加控件通知处理程序代码
  684. int iCount = 0; //检索个数
  685. iSerachIndex = 0;
  686. dlg.DoModal();
  687. m_vecSerach.clear();
  688. if(dlg.m_keywords.IsEmpty())
  689. {
  690. return;
  691. }
  692. for (int i = 0; i < m_List.GetItemCount(); i++ )
  693. {
  694. for (int j = 0; j <m_List.GetHeaderCtrl()->GetItemCount(); j++)
  695. {
  696. CString s = m_List.GetItemText(i, j); //i是行,j是列 m_list是个list control 对象
  697. if (s == dlg.m_keywords)
  698. {
  699. std::pair<int,int>keyIndex;
  700. keyIndex.first = i;
  701. keyIndex.second = j;
  702. m_vecSerach.push_back(keyIndex);
  703. iCount++;
  704. }
  705. }
  706. }
  707. if(pDlg == NULL)
  708. {
  709. pDlg = new CSerachDlg();
  710. pDlg->Create(IDD_DIALOG5,this);
  711. }
  712. pDlg->SetSerachInfo(iCount,dlg.m_keywords);
  713. iSerachIndex= iCount;
  714. pDlg->ShowWindow(SW_SHOW);
  715. ShowFirstSerach(); //显示检索到的第一项
  716. }
  717. void CListCtrlDlg::OnButtonAdd()
  718. {
  719. // TODO: 在此添加控件通知处理程序代码
  720. dlg2.DoModal();
  721. if (dlg2.m_lieming.IsEmpty())
  722. {
  723. AfxMessageBox(_T('输入内容不能为空!'));
  724. return;
  725. }
  726. m_List.InsertColumn(m + 6 , dlg2.m_lieming, LVCFMT_LEFT, 50);
  727. m++;
  728. UpdateData(FALSE);
  729. int nCount = m_List.GetHeaderCtrl()->GetItemCount();
  730. pColName[nCount-1] = dlg2.m_lieming;
  731. //m_List.SetComboColumns(5);
  732. }
  733. void CListCtrlDlg::OnButtonDelete()
  734. {
  735. // TODO: 在此添加控件通知处理程序代码
  736. dlg3.DoModal();
  737. int nColumnCount = m_List.GetHeaderCtrl()->GetItemCount();
  738. // Delete all of the columns.
  739. for (int i = 0; i < nColumnCount; i++)
  740. {
  741. HDITEM hdi;
  742. TCHAR lpBuffer[256];
  743. bool fFound = false;
  744. hdi.mask = HDI_TEXT;
  745. hdi.pszText = lpBuffer;
  746. hdi.cchTextMax = 256;
  747. m_List.GetHeaderCtrl()->GetItem(i, &hdi);//i列的名
  748. if (dlg3.m_delete == hdi.pszText)
  749. {
  750. m_List.DeleteColumn(i);
  751. }
  752. }
  753. }
  754. void CListCtrlDlg::GetCellName(int nRow, int nCol, CString &strName)
  755. {
  756. int nSeed = nCol;
  757. CString strRow;
  758. char cCell = 'A' + nCol - 1;
  759. strName.Format(_T('%c'), cCell);
  760. strRow.Format(_T('%d '), nRow);
  761. strName += strRow;
  762. }
  763. void CListCtrlDlg::OnButtonOpen()
  764. {
  765. // TODO: 在此添加控件通知处理程序代码
  766. CString strKey = _T('');
  767. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  768. //删除所有行(除第一行)
  769. for(int i=m_List.GetItemCount(); i>=0;i--)
  770. {
  771. m_List.DeleteItem(i);
  772. }
  773. m_List.SetRedraw(TRUE); //恢复刷新
  774. m_List.Invalidate();
  775. m_List.UpdateWindow();
  776. int nItems = m_List.GetItemCount(); //获取现在的行数
  777. POSITION ps = mapAllData.GetStartPosition();
  778. while(ps)
  779. {
  780. CString* strData = NULL;
  781. mapAllData.GetNextAssoc(ps, strKey, strData);
  782. CString strTemp = strData[0];
  783. m_List.InsertItem(nItems,strTemp); //插入行
  784. for(int i=1;i<nCol;i++)
  785. {
  786. strTemp = strData[i];
  787. m_List.SetItemText(nItems, i, strTemp); //设置数据
  788. }
  789. }
  790. GetDlgItem(IDC_BUTTON_OPEN)->ShowWindow(SW_HIDE);
  791. UpdateData(FALSE);
  792. }
  793. void CListCtrlDlg::OnDblclkList(NMHDR *pNMHDR, LRESULT *pResult)
  794. {
  795. LPNMITEMACTIVATE pNMItemActivate = reinterpret_cast<LPNMITEMACTIVATE>(pNMHDR);
  796. // TODO: 在此添加控件通知处理程序代码
  797. CRect rc;
  798. CString strTemp;
  799. NM_LISTVIEW *pNMListView = (NM_LISTVIEW *)pNMHDR;
  800. m_Row = pNMListView->iItem;
  801. m_Col = pNMListView->iSubItem;
  802. if (pNMListView->iItem == -1) //选择空白处,添加一行,并设置焦点为最后一行,第二列
  803. {
  804. m_Row = m_List.GetItemCount();
  805. strTemp.Format(_T('%d'), m_Row + 1);
  806. m_List.InsertItem(m_Row, strTemp);
  807. m_List.SetItemState(m_Row, LVIS_SELECTED | LVIS_FOCUSED, LVIS_SELECTED | LVIS_FOCUSED);
  808. m_List.EnsureVisible(m_Row, FALSE);
  809. m_iItemCount++;
  810. //m_Col = 1;
  811. }
  812. if (m_Col != 0) // 选择子项
  813. {
  814. m_List.GetSubItemRect(m_Row, m_Col, LVIR_LABEL, rc);
  815. m_charu.SetParent(&m_List);
  816. m_charu.MoveWindow(rc);
  817. m_charu.SetWindowText(m_List.GetItemText(m_Row, m_Col));
  818. m_charu.ShowWindow(SW_HIDE);
  819. m_charu.SetFocus();//设置Edit焦点
  820. m_charu.ShowCaret();//显示光标
  821. m_charu.SetSel(0, -1);//全选
  822. }
  823. *pResult = 0;
  824. }
  825. void CListCtrlDlg::OnSize(UINT nType, int cx, int cy)
  826. {
  827. CDialog::OnSize(nType, cx, cy);
  828. // TODO: 在此处添加消息处理程序代码
  829. if (nType != SIZE_MINIMIZED)
  830. {
  831. int a[11] = { IDC_LIST, IDC_EDIT_CHARU, IDC_BUTTON_ADD, IDC_BUTTON_DELETE, IDC_BUTTON_EXIT, IDC_BUTTON_INDEX,IDC_BUTTON_FILTER,IDC_BUTTON_IMPORT,
  832. IDC_BUTTON_LINGSAVE, IDC_BUTTON_OPEN, IDC_BUTTON_SAVE
  833. };
  834. int i;
  835. for (i = 0; i < 11; i++)
  836. ReSizes(a[i], m_rect, cx, cy);
  837. GetClientRect(&m_rect);//将变化后的对话框大小设为旧大小
  838. }
  839. }
  840. //======================================
  841. //设置对话框尺寸函数
  842. //======================================
  843. void CListCtrlDlg::ReSizes(int idc, CRect client, int x, int y)
  844. {
  845. CWnd *pWin;
  846. pWin = GetDlgItem(idc);//获取控件句柄
  847. if (pWin)//判断是否为空,因为对话框创建时会调用此函数,而当时控件还未创建
  848. {
  849. CRect Editrect;//获取控件变化前大小
  850. pWin->GetWindowRect(&Editrect);
  851. ScreenToClient(&Editrect);//将控件大小转换为在对话框中的区域坐标
  852. //x/client.Width()为对话框在横向的变化比例
  853. Editrect.left = Editrect.left*x / client.Width();//调整控件大小
  854. Editrect.right = Editrect.right*x / client.Width();
  855. Editrect.top = Editrect.top*y / client.Height();
  856. Editrect.bottom = Editrect.bottom*y / client.Height();
  857. pWin->MoveWindow(Editrect);//调整控件大小
  858. }
  859. }
  860. void CListCtrlDlg::OnBnClickedButtonFilter()
  861. {
  862. // TODO: 在此添加控件通知处理程序代码
  863. CFilterDlg FilterDlg;
  864. FilterDlg.DoModal();
  865. if(FilterDlg.mapFilter.IsEmpty())
  866. return;
  867. int nSize = FilterDlg.mapFilter.GetCount();
  868. int *pIndex = new int[nSize]; //用于记录符合条件列名的序号
  869. ZeroMemory(pIndex,nSize);
  870. CString strkey = '';
  871. CString strValue = '';
  872. std::vector<CString>vecKey; //条件名
  873. std::vector<CString>vecValue; //条件内容
  874. POSITION ps = FilterDlg.mapFilter.GetStartPosition();
  875. while(ps)
  876. {
  877. FilterDlg.mapFilter.GetNextAssoc(ps, strkey, strValue);
  878. vecKey.push_back(strkey);
  879. vecValue.push_back(strValue);
  880. }
  881. if(!CheackConditions(vecKey,pIndex))
  882. {
  883. AfxMessageBox(_T('条件名称错误,查询条件功能失败!'));
  884. return;
  885. }
  886. else
  887. {
  888. SaveData(); //保持当前列表控件所有数据
  889. QueryInfo(vecValue,pIndex); //条件查询数据
  890. GetDlgItem(IDC_BUTTON_OPEN)->ShowWindow(SW_SHOW);
  891. }
  892. delete FilterDlg;
  893. }
  894. //保存所有数据
  895. void CListCtrlDlg::SaveData()
  896. {
  897. mapAllData.RemoveAll();
  898. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  899. CString* pColName = NULL;
  900. CString strKey = '';
  901. CString* strValue = NULL;
  902. for (int i = 0; i < m_List.GetItemCount(); i++ )
  903. {
  904. //存放列名
  905. pColName = NULL;
  906. pColName = new CString[nCol];
  907. for (int j = 0; j <m_List.GetHeaderCtrl()->GetItemCount(); j++)
  908. {
  909. CString s = m_List.GetItemText(i, j); //i是行,j是列 m_list是个list control 对象
  910. pColName[j] = s;
  911. }
  912. strKey = pColName[0];
  913. strValue = pColName;
  914. mapAllData.SetAt(strKey,strValue);
  915. }
  916. }
  917. //显示检索第一个
  918. void CListCtrlDlg::ShowFirstSerach()
  919. {
  920. if(m_vecSerach.empty())
  921. return ;
  922. iSerachIndex = 1;
  923. int iKey = iSerachIndex - 1;
  924. m_Index.first = m_vecSerach[iKey].first;
  925. m_Index.second = m_vecSerach[iKey].second;
  926. CRect rect;
  927. BOOL bReturn = m_List.GetSubItemRect(m_Index.first,m_Index.second,LVIR_BOUNDS,rect);
  928. if(!bReturn)
  929. {
  930. AfxMessageBox(_T('调用CListCtrl::GetSubItemRect函数错误!'));
  931. return;
  932. }
  933. else
  934. {
  935. CRect r;
  936. m_List.GetWindowRect(&r);
  937. int x = rect.TopLeft().x + 25;
  938. int y = rect.TopLeft().y + 55;
  939. if(m_Index.second == 0)
  940. {
  941. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  942. CreateSolidCaret(rect.Width()/nCol, rect.Height());
  943. }
  944. else
  945. {
  946. CreateSolidCaret(rect.Width(), rect.Height());
  947. }
  948. ::SetCaretPos(x,y); //将光标移动指定位置
  949. ShowCaret();
  950. UpdateData(FALSE);
  951. }
  952. }
  953. //移动向上光标
  954. afx_msg LRESULT CListCtrlDlg::OnUserUpSerach(WPARAM wParam, LPARAM lParam)
  955. {
  956. if(m_vecSerach.empty())
  957. return 0;
  958. iSerachIndex--;
  959. if(iSerachIndex == 0)
  960. {
  961. iSerachIndex++;
  962. AfxMessageBox('已是第一项');
  963. return 0;
  964. }
  965. int iKey = iSerachIndex - 1;
  966. m_Index.first = m_vecSerach[iKey].first;
  967. m_Index.second = m_vecSerach[iKey].second;
  968. CRect rect;
  969. BOOL bReturn = m_List.GetSubItemRect(m_Index.first,m_Index.second,LVIR_BOUNDS,rect);
  970. if(!bReturn)
  971. {
  972. AfxMessageBox(_T('调用CListCtrl::GetSubItemRect函数错误!'));
  973. return 0;
  974. }
  975. else
  976. {
  977. CRect r;
  978. m_List.GetWindowRect(&r);
  979. int x = rect.TopLeft().x + 25;
  980. int y = rect.TopLeft().y + 55;
  981. if(m_Index.second == 0)
  982. {
  983. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  984. CreateSolidCaret(rect.Width()/nCol, rect.Height());
  985. }
  986. else
  987. {
  988. CreateSolidCaret(rect.Width(), rect.Height());
  989. }
  990. ::SetCaretPos(x,y); //将光标移动指定位置
  991. ShowCaret();
  992. UpdateData(FALSE);
  993. }
  994. return 0;
  995. }
  996. //移动向下光标
  997. afx_msg LRESULT CListCtrlDlg::OnUserDownSerach(WPARAM wParam, LPARAM lParam)
  998. {
  999. if(m_vecSerach.empty())
  1000. return 0;
  1001. iSerachIndex++;
  1002. if(iSerachIndex > m_vecSerach.size())
  1003. {
  1004. iSerachIndex = m_vecSerach.size();
  1005. AfxMessageBox('已是最后一项');
  1006. return 0;
  1007. }
  1008. int iKey = iSerachIndex - 1;
  1009. m_Index.first = m_vecSerach[iKey].first;
  1010. m_Index.second = m_vecSerach[iKey].second;
  1011. CRect rect;
  1012. BOOL bReturn = m_List.GetSubItemRect(m_Index.first,m_Index.second,LVIR_BOUNDS,rect);
  1013. if(!bReturn)
  1014. {
  1015. AfxMessageBox(_T('调用CListCtrl::GetSubItemRect函数错误!'));
  1016. return 0;
  1017. }
  1018. else
  1019. {
  1020. CRect r;
  1021. m_List.GetWindowRect(&r);
  1022. int x = rect.TopLeft().x + 25;
  1023. int y = rect.TopLeft().y + 55;
  1024. if(m_Index.second == 0)
  1025. {
  1026. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  1027. CreateSolidCaret(rect.Width()/nCol, rect.Height());
  1028. }
  1029. else
  1030. {
  1031. CreateSolidCaret(rect.Width(), rect.Height());
  1032. }
  1033. ::SetCaretPos(x,y); //将光标移动指定位置
  1034. ShowCaret();
  1035. UpdateData(FALSE);
  1036. }
  1037. return 0;
  1038. }
  1039. void CListCtrlDlg::OnBnClickedButtonImport()
  1040. {
  1041. // TODO: 在此添加控件通知处理程序代码
  1042. CFileDialog dlg( TRUE, //TRUE或FALSE。TRUE为打开文件;FALSE为保存文件
  1043. 'xls', //为缺省的扩展名
  1044. 'FileList', //为显示在文件名组合框的编辑框的文件名,一般可选NULL
  1045. OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT,//为对话框风格,一般为OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,即隐藏只读选项和覆盖已有文件前提示。
  1046. 'Excel 文件(*.xls)|*.xls||'//为下拉列表枢中显示文件类型
  1047. );
  1048. dlg.m_ofn.lpstrTitle = '导入数据';
  1049. if (dlg.DoModal() != IDOK)
  1050. return;
  1051. CString strFilePath;
  1052. //获得文件路径名
  1053. strFilePath = dlg.GetPathName();
  1054. //判断文件是否已经存在,存在则打开文件
  1055. DWORD dwRe = GetFileAttributes(strFilePath);
  1056. if ( dwRe != (DWORD)-1 )
  1057. {
  1058. //ShellExecute(NULL, NULL, strFilePath, NULL, NULL, SW_RESTORE);
  1059. }
  1060. else return;
  1061. CDatabase db;//数据库库需要包含头文件 #include <afxdb.h>
  1062. CString sDriver = 'MICROSOFT EXCEL DRIVER (*.XLS)'; // Excel驱动
  1063. CString sSql,arr[4];
  1064. sSql.Format('DRIVER={%s};DSN='';FIRSTROWHASNAMES=1;READONLY=FALSE;CREATE_DB=\'%s\';DBQ=%s',sDriver, strFilePath, strFilePath);
  1065. if(!db.OpenEx(sSql,CDatabase::noOdbcDialog))//连接数据源DJB.xls
  1066. {
  1067. MessageBox('打开EXCEL文件失败!','错误');
  1068. return;
  1069. }
  1070. //打开EXCEL表
  1071. CRecordset pset(&db);
  1072. m_List.DeleteAllItems();
  1073. AfxMessageBox('OK');
  1074. sSql.Format('SELECT 序号,姓名,学号,班级 FROM DSO_DX');
  1075. pset.Open(CRecordset::forwardOnly,sSql,CRecordset::readOnly);
  1076. while(!pset.IsEOF())
  1077. {
  1078. pset.GetFieldValue('序号',arr[0]);//前面字段必须与表中的相同,否则出错。
  1079. pset.GetFieldValue('姓名',arr[1]);
  1080. pset.GetFieldValue('学号',arr[2]);
  1081. pset.GetFieldValue('班级',arr[2]);
  1082. int count = m_List.GetItemCount();//插入到ListCtrl中
  1083. m_List.InsertItem(count,arr[0]);
  1084. m_List.SetItemText(count,1,arr[1]);
  1085. m_List.SetItemText(count,2,arr[2]);
  1086. m_List.SetItemText(count,3,arr[3]);
  1087. pset.MoveNext();
  1088. }
  1089. db.Close();
  1090. MessageBox('Excel数据成功导入系统!','导入成功');
  1091. }
  1092. //条件查询数据
  1093. void CListCtrlDlg::QueryInfo(std::vector<CString>&vecDataTemp,int nColIndex[])
  1094. {
  1095. int nSize = sizeof(nColIndex)/4;
  1096. int nCol = m_List.GetHeaderCtrl()->GetItemCount();
  1097. CString strKey = '';
  1098. POSITION pos = mapAllData.GetStartPosition();
  1099. while(pos)
  1100. {
  1101. CString* strData = NULL;
  1102. strData = new CString[nCol];
  1103. mapAllData.GetNextAssoc(pos, strKey, strData);
  1104. bool bFlag = true;
  1105. for(int i = 0;i<nSize;i++)
  1106. {
  1107. int nKey = nColIndex[i];
  1108. if(strData[nKey] != vecDataTemp[i])
  1109. {
  1110. bFlag = false;
  1111. break;
  1112. }
  1113. }
  1114. if(bFlag)
  1115. {
  1116. mapCheckData.SetAt(strKey,strData);
  1117. }
  1118. }
  1119. //删除所有行(除第一行)
  1120. for(int i=m_List.GetItemCount(); i>=0;i--)
  1121. {
  1122. m_List.DeleteItem(i);
  1123. }
  1124. m_List.SetRedraw(TRUE); //恢复刷新
  1125. m_List.Invalidate();
  1126. m_List.UpdateWindow();
  1127. int nItems = m_List.GetItemCount(); //获取现在的行数
  1128. POSITION ps = mapCheckData.GetStartPosition();
  1129. while(ps)
  1130. {
  1131. CString* strData = NULL;
  1132. mapCheckData.GetNextAssoc(ps, strKey, strData);
  1133. CString strTemp = strData[0];
  1134. m_List.InsertItem(nItems,strTemp); //插入行
  1135. for(int i=1;i<nCol;i++)
  1136. {
  1137. strTemp = strData[i];
  1138. m_List.SetItemText(nItems, i, strTemp); //设置数据
  1139. }
  1140. }
  1141. UpdateData(FALSE);
  1142. }
  1143. // 判断查询条件内容是否符合列名
  1144. bool CListCtrlDlg::CheackConditions(std::vector<CString>&vecTemp,int nColIndex[])
  1145. {
  1146. //listctrl控件的列名
  1147. if(pColName == NULL)
  1148. return false;
  1149. //获取listctrl列的总数
  1150. UINT iCount = m_List.GetHeaderCtrl()->GetItemCount();
  1151. bool bFlag = false;
  1152. int nIdex = 0;
  1153. for(UINT i=0;i<vecTemp.size();i++)
  1154. {
  1155. for(UINT j = 0;j <iCount;j++)
  1156. {
  1157. if(vecTemp[i] == pColName[j])
  1158. {
  1159. nColIndex[nIdex++] = j;
  1160. bFlag= true;
  1161. }
  1162. }
  1163. if(!bFlag)
  1164. {
  1165. //手动释放内存
  1166. delete[]nColIndex;
  1167. nColIndex = NULL;
  1168. return false;
  1169. }
  1170. }
  1171. return true;
  1172. }
  1173. //列表框第一列排序
  1174. void CListCtrlDlg::SortFirstColumn()//神特么的还有bug!!!
  1175. {
  1176. int nItemNum = m_List.GetItemCount();
  1177. for (int i = 0; i <= nItemNum ; i++)
  1178. {
  1179. CString strIndex = _T('');
  1180. strIndex.Format(_T('%d'),i+1);
  1181. m_List.SetItemText(i,0, strIndex);
  1182. }
  1183. }
  1184. void CListCtrlDlg::DisplayAuthor()
  1185. {
  1186. AfxMessageBox('邮箱:u25th_engineer@163.com\nQQ :2046195761');
  1187. //AfxMessageBox('QQ:592551037');
  1188. }

四/五子棋(无TCP):

链接: https://pan.baidu.com/s/1PkMaOaiCyCNm38o9j46F4w 密码: 7rks

四/五子棋(有TCP):

链接: https://pan.baidu.com/s/13DPdXSAlhRxlU6GM9kLbrA 密码: tmfn

成员信息表(学生信息管理系统):

链接: https://pan.baidu.com/s/1qBi2WiFPmFWPjRtpB-2Kzg 密码: xt9i

总的链接:

链接: https://pan.baidu.com/s/1FARAhY2Tx48NuFVZEQaRWA 密码: 7958

(0)

相关推荐