studentView.cpp
上传用户:hzzhuce
上传日期:2013-04-01
资源大小:1289k
文件大小:8k
源码类别:

SQL Server

开发平台:

Visual C++

  1. // studentView.cpp : implementation of the CStudentView class
  2. //
  3. #include "stdafx.h"
  4. #include "student.h"
  5. #include"Select.h"
  6. #include "studentSet.h"
  7. #include "studentDoc.h"
  8. #include "studentView.h"
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CStudentView
  16. IMPLEMENT_DYNCREATE(CStudentView, CRecordView)
  17. BEGIN_MESSAGE_MAP(CStudentView, CRecordView)
  18. //{{AFX_MSG_MAP(CStudentView)
  19. ON_BN_CLICKED(IDC_SHOW_BUTTON, OnShowButton)
  20. ON_BN_CLICKED(IDC_ADD_BUTTON2, OnAddButton2)
  21. ON_BN_CLICKED(IDC_DELETE_BUTTON, OnDeleteButton)
  22. ON_BN_CLICKED(IDC_MODIFY_BUTTON, OnModifyButton)
  23. ON_BN_CLICKED(IDC_QUERY_BUTTON, OnQueryButton)
  24. ON_BN_CLICKED(IDC_SORT_BUTTON, OnSortButton)
  25. //}}AFX_MSG_MAP
  26. // Standard printing commands
  27. ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
  28. ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
  29. ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
  30. END_MESSAGE_MAP()
  31. /////////////////////////////////////////////////////////////////////////////
  32. // CStudentView construction/destruction
  33. CStudentView::CStudentView()
  34. : CRecordView(CStudentView::IDD)
  35. {
  36. //{{AFX_DATA_INIT(CStudentView)
  37. // NOTE: the ClassWizard will add member initialization here
  38. m_pSet = NULL;
  39. //}}AFX_DATA_INIT
  40. // TODO: add construction code here
  41.    
  42. }
  43. CStudentView::~CStudentView()
  44. {
  45. }
  46. void CStudentView::DoDataExchange(CDataExchange* pDX)
  47. {
  48. CRecordView::DoDataExchange(pDX);
  49. //{{AFX_DATA_MAP(CStudentView)
  50. DDX_Control(pDX, IDC_RECORD_LIST, m_Record_List);
  51. //}}AFX_DATA_MAP
  52. }
  53. BOOL CStudentView::PreCreateWindow(CREATESTRUCT& cs)
  54. {
  55. // TODO: Modify the Window class or styles here by modifying
  56. //  the CREATESTRUCT cs
  57. return CRecordView::PreCreateWindow(cs);
  58. }
  59. void CStudentView::OnInitialUpdate()
  60. {
  61. m_pSet = &GetDocument()->m_studentSet;
  62. CRecordView::OnInitialUpdate();
  63. GetParentFrame()->RecalcLayout();
  64. ResizeParentToFit();
  65. m_Record_List.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
  66. LV_COLUMN h;
  67. h.mask=LVCF_FMT|LVCF_TEXT|LVCF_WIDTH;
  68. h.fmt=LVCFMT_CENTER;
  69. h.cx=90;
  70. h.pszText="学号";
  71. m_Record_List.InsertColumn(0,&h);
  72.  
  73. h.pszText="姓名";
  74. m_Record_List.InsertColumn(1,&h);
  75. h.pszText="数学";
  76. m_Record_List.InsertColumn(2,&h);
  77. h.pszText="语文";
  78. m_Record_List.InsertColumn(3,&h);
  79. h.pszText="英语";
  80. m_Record_List.InsertColumn(4,&h);
  81. OnShowButton();
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // CStudentView printing
  85. BOOL CStudentView::OnPreparePrinting(CPrintInfo* pInfo)
  86. {
  87. // default preparation
  88. return DoPreparePrinting(pInfo);
  89. }
  90. void CStudentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  91. {
  92. // TODO: add extra initialization before printing
  93. }
  94. void CStudentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  95. {
  96. // TODO: add cleanup after printing
  97. }
  98. /////////////////////////////////////////////////////////////////////////////
  99. // CStudentView diagnostics
  100. #ifdef _DEBUG
  101. void CStudentView::AssertValid() const
  102. {
  103. CRecordView::AssertValid();
  104. }
  105. void CStudentView::Dump(CDumpContext& dc) const
  106. {
  107. CRecordView::Dump(dc);
  108. }
  109. CStudentDoc* CStudentView::GetDocument() // non-debug version is inline
  110. {
  111. ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStudentDoc)));
  112. return (CStudentDoc*)m_pDocument;
  113. }
  114. #endif //_DEBUG
  115. /////////////////////////////////////////////////////////////////////////////
  116. // CStudentView database support
  117. CRecordset* CStudentView::OnGetRecordset()
  118. {
  119. return m_pSet;
  120. }
  121. /////////////////////////////////////////////////////////////////////////////
  122. // CStudentView message handlers
  123. void CStudentView::OnShowButton() 
  124. {
  125. // TODO: Add your control notification handler code here
  126.     m_Record_List.DeleteAllItems();
  127. CString s;
  128. i=0;
  129. m_pSet->MoveFirst();
  130. while(!m_pSet->IsEOF())
  131. {
  132. s.Format("%d",m_pSet->m_column1);
  133. m_Record_List.InsertItem(i,s);
  134. m_Record_List.SetItemText(i,1,m_pSet->m_column2);
  135.         s.Format("%d",m_pSet->m_column3);
  136. m_Record_List.SetItemText(i,2,s);
  137. s.Format("%d",m_pSet->m_column4);
  138. m_Record_List.SetItemText(i,3,s);
  139. s.Format("%d",m_pSet->m_column5);
  140. m_Record_List.SetItemText(i,4,s);
  141. m_pSet->MoveNext();
  142. i++;
  143. }
  144. }
  145. void CStudentView::OnAddButton2() 
  146. {
  147. // TODO: Add your control notification handler code here
  148.     CAdddlg dlg;
  149. CString s;
  150. if(dlg.DoModal()==IDOK)
  151. {
  152. m_pSet->AddNew();
  153. m_pSet->m_column1=atol(dlg.m_num);
  154. m_pSet->m_column2=dlg.m_name;
  155. m_pSet->m_column3=atol(dlg.m_math);
  156. m_pSet->m_column4=atol(dlg.m_chi);
  157. m_pSet->m_column5=atol(dlg.m_eng);
  158. s.Format("%d",m_pSet->m_column1);
  159. m_Record_List.InsertItem(i,s);
  160. m_Record_List.SetItemText(i,1,m_pSet->m_column2);
  161. s.Format("%d",m_pSet->m_column3);
  162. m_Record_List.SetItemText(i,2,s);
  163. s.Format("%d",m_pSet->m_column4);
  164. m_Record_List.SetItemText(i,3,s);
  165. s.Format("%d",m_pSet->m_column5);
  166. m_Record_List.SetItemText(i,4,s);
  167. m_pSet->Update();
  168. i++;
  169. }
  170. }
  171. void CStudentView::OnDeleteButton() 
  172. {
  173. // TODO: Add your control notification handler code here
  174. CString s;
  175. m_pSet->MoveFirst();
  176. int index=m_Record_List.GetNextItem(-1,LVNI_SELECTED);
  177. if(index!=-1)
  178. {
  179. while(!m_pSet->IsEOF())
  180. {
  181. s.Format("%ld",m_pSet->m_column1);
  182. if(s==m_Record_List.GetItemText(index,0))
  183. {
  184. m_pSet->Delete();
  185. i--;
  186. }
  187. m_pSet->MoveNext();
  188. }
  189. MessageBox("记录已经删除!");
  190. m_Record_List.DeleteItem(index);
  191. }
  192. else
  193. MessageBox("请选中删除记录!");
  194. }
  195. void CStudentView::OnModifyButton() 
  196. {
  197. // TODO: Add your control notification handler code here
  198.     CString s;
  199. int j=0;
  200. CAdddlg dlg;
  201. m_pSet->MoveFirst();
  202. int index=m_Record_List.GetNextItem(-1,LVNI_SELECTED);
  203. if(index!=-1)
  204. {
  205. dlg.m_num=m_Record_List.GetItemText(index,0);
  206. dlg.m_name=m_Record_List.GetItemText(index,1);
  207. dlg.m_math=m_Record_List.GetItemText(index,2);
  208. dlg.m_chi=m_Record_List.GetItemText(index,3);
  209. dlg.m_eng=m_Record_List.GetItemText(index,4);
  210. if(dlg.DoModal()==IDOK)
  211. {
  212. while(!m_pSet->IsEOF())
  213. {
  214. if(m_pSet->m_column1==atol(dlg.m_num))
  215. {
  216. m_pSet->Edit();
  217. m_pSet->m_column1=atoi(dlg.m_num);
  218. m_pSet->m_column2=dlg.m_name;
  219. m_pSet->m_column3=atoi(dlg.m_math);
  220. m_pSet->m_column4=atoi(dlg.m_chi);
  221. m_pSet->m_column5=atoi(dlg.m_eng);
  222. m_pSet->Update();
  223. MessageBox("记录已经修改!");
  224. m_Record_List.DeleteItem(j);
  225. s.Format("%d",m_pSet->m_column1);
  226. m_Record_List.InsertItem(j,s);
  227. m_Record_List.SetItemText(j,1,m_pSet->m_column2);
  228. s.Format("%d",m_pSet->m_column3);
  229. m_Record_List.SetItemText(j,2,s);
  230. s.Format("%d",m_pSet->m_column4);
  231. m_Record_List.SetItemText(j,3,s);
  232. s.Format("%d",m_pSet->m_column5);
  233. m_Record_List.SetItemText(j,4,s);
  234. break;
  235. }
  236. else
  237. {
  238. j++;
  239. m_pSet->MoveNext();
  240. }
  241. }
  242. }
  243. }
  244. else
  245. MessageBox("请选中修改记录!");
  246. }
  247. void CStudentView::OnQueryButton() 
  248. {
  249. // TODO: Add your control notification handler code here
  250.     CString s;
  251.     CSelect dlg;
  252.     bool b=0;
  253. int j=0;
  254. m_pSet->MoveFirst();
  255. if(dlg.DoModal()==IDOK)
  256. {
  257. m_Record_List.DeleteAllItems();
  258. int j=0;
  259. while(!m_pSet->IsEOF())
  260. {
  261. if(m_pSet->m_column2==dlg.m_name)
  262. {
  263. s.Format("%d",m_pSet->m_column1);
  264.         m_Record_List.InsertItem(j,s);
  265.         m_Record_List.SetItemText(j,1,m_pSet->m_column2);
  266.      s.Format("%d",m_pSet->m_column3);
  267. m_Record_List.SetItemText(j,2,s);
  268. s.Format("%d",m_pSet->m_column4);
  269. m_Record_List.SetItemText(j,3,s);
  270. s.Format("%d",m_pSet->m_column5);
  271. m_Record_List.SetItemText(j,4,s);
  272. j++;
  273. }
  274. m_pSet->MoveNext();
  275. }
  276. if(!j)MessageBox("你所要的记录不存在!");
  277. }
  278. }
  279. void CStudentView::OnSortButton() 
  280. {
  281. // TODO: Add your control notification handler code here
  282. CSort dlg;
  283. if(dlg.DoModal()==IDOK)
  284. {
  285. m_Record_List.DeleteAllItems();
  286. m_pSet->m_strSort=dlg.m_Getstring;
  287. m_pSet->Requery();//重新建立记录集时使用 
  288. OnShowButton();
  289. }
  290. }
  291. void CStudentView::OnDraw(CDC* pDC) 
  292. {
  293. // TODO: Add your specialized code here and/or call the base class
  294. CStudentDoc*pDoc=GetDocument();
  295. ASSERT_VALID(pDoc);
  296. CDC MemDC;
  297. MemDC.CreateCompatibleDC(NULL);
  298. MemDC.SelectObject(pDoc->m_Bitmap);
  299. pDC->BitBlt(0,0,pDoc->m_nWidth,pDoc->m_nHeight,&MemDC,0,0,SRCCOPY);
  300. }