BookAdminView.cpp
上传用户:weisheen
上传日期:2022-07-09
资源大小:19390k
文件大小:5k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // BookAdminView.cpp : implementation of the CBookAdminView class
  2. //
  3. #include "stdafx.h"
  4. #include "BookAdmin.h"
  5. #include "BookAdminDoc.h"
  6. #include "BookAdminView.h"
  7. #include "./DlgBookAdd.h"
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CBookAdminView
  15. IMPLEMENT_DYNCREATE(CBookAdminView, CListView)
  16. BEGIN_MESSAGE_MAP(CBookAdminView, CListView)
  17. //{{AFX_MSG_MAP(CBookAdminView)
  18. ON_COMMAND(ID_BUTTON32772, OnButton32772)
  19. ON_COMMAND(ID_BUTTON32773, OnButton32773)
  20. //}}AFX_MSG_MAP
  21. // Standard printing commands
  22. ON_COMMAND(ID_FILE_PRINT, CListView::OnFilePrint)
  23. ON_COMMAND(ID_FILE_PRINT_DIRECT, CListView::OnFilePrint)
  24. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CListView::OnFilePrintPreview)
  25. END_MESSAGE_MAP()
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CBookAdminView construction/destruction
  28. CBookAdminView::CBookAdminView()
  29. {
  30. // TODO: add construction code here
  31. }
  32. CBookAdminView::~CBookAdminView()
  33. {
  34. }
  35. BOOL CBookAdminView::PreCreateWindow(CREATESTRUCT& cs)
  36. {
  37. // TODO: Modify the Window class or styles here by modifying
  38. //  the CREATESTRUCT cs
  39. return CListView::PreCreateWindow(cs);
  40. }
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CBookAdminView drawing
  43. void CBookAdminView::OnDraw(CDC* pDC)
  44. {
  45. CBookAdminDoc* pDoc = GetDocument();
  46. ASSERT_VALID(pDoc);
  47. CListCtrl& refCtrl = GetListCtrl();
  48. refCtrl.InsertItem(0, "Item!");
  49. // TODO: add draw code for native data here
  50. }
  51. void CBookAdminView::OnInitialUpdate()
  52. {
  53. CListView::OnInitialUpdate();
  54. //创建列;
  55. this->GetListCtrl().InsertColumn(0,"BookName",LVCFMT_CENTER,200);
  56. this->GetListCtrl().InsertColumn(1,"BookAuther",LVCFMT_CENTER,200);
  57. //添加数据;
  58. POSITION ps;
  59. ps=this->GetDocument()->allBooks.GetHeadPosition();
  60. while(ps!=NULL)
  61. {
  62. BookEntity* be=this->GetDocument()->allBooks.GetNext(ps);
  63. this->GetListCtrl().InsertItem(0,be->BookName);
  64. this->GetListCtrl().SetItemText(0,1,be->BookAuthor);
  65. this->GetListCtrl().SetItemData(0,be->BookIsnb);
  66. }
  67. // TODO: You may populate your ListView with items by directly accessing
  68. //  its list control through a call to GetListCtrl().
  69. }
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CBookAdminView printing
  72. BOOL CBookAdminView::OnPreparePrinting(CPrintInfo* pInfo)
  73. {
  74. // default preparation
  75. return DoPreparePrinting(pInfo);
  76. }
  77. void CBookAdminView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  78. {
  79. // TODO: add extra initialization before printing
  80. }
  81. void CBookAdminView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83. // TODO: add cleanup after printing
  84. }
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CBookAdminView diagnostics
  87. #ifdef _DEBUG
  88. void CBookAdminView::AssertValid() const
  89. {
  90. CListView::AssertValid();
  91. }
  92. void CBookAdminView::Dump(CDumpContext& dc) const
  93. {
  94. CListView::Dump(dc);
  95. }
  96. CBookAdminDoc* CBookAdminView::GetDocument() // non-debug version is inline
  97. {
  98. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBookAdminDoc)));
  99. return (CBookAdminDoc*)m_pDocument;
  100. }
  101. #endif //_DEBUG
  102. /////////////////////////////////////////////////////////////////////////////
  103. // CBookAdminView message handlers
  104. void CBookAdminView::OnStyleChanged(int nStyleType, LPSTYLESTRUCT lpStyleStruct)
  105. {
  106. //TODO: add code to react to the user changing the view style of your window
  107. }
  108. void CBookAdminView::OnButton32772() 
  109. {
  110. // TODO: Add your command handler code here
  111. //找到被删除的记录ID;
  112. int index=this->GetListCtrl().GetNextItem(-1,LVNI_SELECTED|LVNI_ALL);
  113. if(index==-1)
  114. {
  115. MessageBox("请首先选中一条记录!");
  116. }
  117. else
  118. {
  119. UINT iOk=MessageBox("你真的要删除吗?","删除确认",MB_YESNO);
  120. //从当前的界面上删除;
  121. if(iOk==6)
  122. {
  123. //
  124. DWORD isbn=this->GetListCtrl().GetItemData(index);
  125. UINT iResult=0;
  126. this->GetDocument()->GetDbTools()->DeleteBookById((UINT)isbn,&iResult);
  127. this->GetListCtrl().DeleteItem(index);
  128. }
  129. }
  130. }
  131. //修改数据
  132. void CBookAdminView::OnButton32773() 
  133. {
  134. // TODO: Add your command handler code here
  135. //1. 获得被编辑的记录id;
  136. int index=this->GetListCtrl().GetNextItem(-1,LVNI_ALL|LVNI_SELECTED);
  137. if(index==-1)
  138. {
  139. MessageBox("请首先选中一条记录");
  140. return ;
  141. }
  142. //2. 显示被编辑的数据;
  143. UINT id=(UINT)(this->GetListCtrl().GetItemData(index));
  144. DTOBook* pCurrentData=NULL;
  145. this->GetDocument()->GetDbTools()->GetBookByIsbn(id,&pCurrentData);
  146. //3. 获得修改后的数据;
  147. DlgBookAdd dlg;
  148. dlg.m_bookisbn=id;
  149. dlg.m_bookname=CString(pCurrentData->BookName);
  150. dlg.m_bookauthor=CString(pCurrentData->BookAuthor);
  151. ::SysFreeString(pCurrentData->BookName);
  152. ::SysFreeString(pCurrentData->BookAuthor);
  153. dlg.DoModal();
  154. //4. 更新数据库;
  155. //5. 更新界面;
  156. }