RdWrtIniDlg.cpp
上传用户:lqt88888
上传日期:2009-12-14
资源大小:905k
文件大小:5k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // RdWrtIniDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "RdWrtIni.h"
  5. #include "RdWrtIniDlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CAboutDlg dialog used for App About
  13. class CAboutDlg : public CDialog
  14. {
  15. public:
  16. CAboutDlg();
  17. // Dialog Data
  18. //{{AFX_DATA(CAboutDlg)
  19. enum { IDD = IDD_ABOUTBOX };
  20. //}}AFX_DATA
  21. // ClassWizard generated virtual function overrides
  22. //{{AFX_VIRTUAL(CAboutDlg)
  23. protected:
  24. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  25. //}}AFX_VIRTUAL
  26. // Implementation
  27. protected:
  28. //{{AFX_MSG(CAboutDlg)
  29. //}}AFX_MSG
  30. DECLARE_MESSAGE_MAP()
  31. };
  32. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  33. {
  34. //{{AFX_DATA_INIT(CAboutDlg)
  35. //}}AFX_DATA_INIT
  36. }
  37. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  38. {
  39. CDialog::DoDataExchange(pDX);
  40. //{{AFX_DATA_MAP(CAboutDlg)
  41. //}}AFX_DATA_MAP
  42. }
  43. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  44. //{{AFX_MSG_MAP(CAboutDlg)
  45. // No message handlers
  46. //}}AFX_MSG_MAP
  47. END_MESSAGE_MAP()
  48. /////////////////////////////////////////////////////////////////////////////
  49. // CRdWrtIniDlg dialog
  50. CRdWrtIniDlg::CRdWrtIniDlg(CWnd* pParent /*=NULL*/)
  51. : CDialog(CRdWrtIniDlg::IDD, pParent)
  52. {
  53. //{{AFX_DATA_INIT(CRdWrtIniDlg)
  54. // NOTE: the ClassWizard will add member initialization here
  55. //}}AFX_DATA_INIT
  56. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  57. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  58. }
  59. void CRdWrtIniDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61. CDialog::DoDataExchange(pDX);
  62. //{{AFX_DATA_MAP(CRdWrtIniDlg)
  63. DDX_Control(pDX, IDC_LIST_RIGHT, m_lstRight);
  64. DDX_Control(pDX, IDC_LIST_LEFT, m_lstLeft);
  65. //}}AFX_DATA_MAP
  66. }
  67. BEGIN_MESSAGE_MAP(CRdWrtIniDlg, CDialog)
  68. //{{AFX_MSG_MAP(CRdWrtIniDlg)
  69. ON_WM_SYSCOMMAND()
  70. ON_WM_PAINT()
  71. ON_WM_QUERYDRAGICON()
  72. ON_BN_CLICKED(IDC_BUTTON_READ, OnRead)
  73. ON_BN_CLICKED(IDC_BUTTON_WRITE, OnWrite)
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CRdWrtIniDlg message handlers
  78. BOOL CRdWrtIniDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. //初始化左边列表框
  102. for(int i=0;i<6;i++)
  103. {
  104. CString str;
  105. str.Format("学生%d",i);
  106. m_lstLeft.AddString(str);
  107. }
  108. return TRUE;  // return TRUE  unless you set the focus to a control
  109. }
  110. void CRdWrtIniDlg::OnSysCommand(UINT nID, LPARAM lParam)
  111. {
  112. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  113. {
  114. CAboutDlg dlgAbout;
  115. dlgAbout.DoModal();
  116. }
  117. else
  118. {
  119. CDialog::OnSysCommand(nID, lParam);
  120. }
  121. }
  122. // If you add a minimize button to your dialog, you will need the code below
  123. //  to draw the icon.  For MFC applications using the document/view model,
  124. //  this is automatically done for you by the framework.
  125. void CRdWrtIniDlg::OnPaint() 
  126. {
  127. if (IsIconic())
  128. {
  129. CPaintDC dc(this); // device context for painting
  130. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  131. // Center icon in client rectangle
  132. int cxIcon = GetSystemMetrics(SM_CXICON);
  133. int cyIcon = GetSystemMetrics(SM_CYICON);
  134. CRect rect;
  135. GetClientRect(&rect);
  136. int x = (rect.Width() - cxIcon + 1) / 2;
  137. int y = (rect.Height() - cyIcon + 1) / 2;
  138. // Draw the icon
  139. dc.DrawIcon(x, y, m_hIcon);
  140. }
  141. else
  142. {
  143. CDialog::OnPaint();
  144. }
  145. }
  146. // The system calls this to obtain the cursor to display while the user drags
  147. //  the minimized window.
  148. HCURSOR CRdWrtIniDlg::OnQueryDragIcon()
  149. {
  150. return (HCURSOR) m_hIcon;
  151. }
  152. void CRdWrtIniDlg::OnRead() 
  153. {
  154. //从ini文件中获得数量
  155. int nCount =::GetPrivateProfileInt("StudentCount","Count",0,".\Student.ini");
  156. for(int i=0;i<nCount;i++)
  157. {
  158. //从ini文件中获得学生,并加到右边列表中
  159. CString strKeyName,strStudentName;
  160. strKeyName.Format("Name%d",i);
  161. ::GetPrivateProfileString("StudentName",strKeyName,NULL,strStudentName.GetBuffer(128),128,".\Student.ini");
  162. m_lstRight.AddString(strStudentName);
  163. }
  164. }
  165. void CRdWrtIniDlg::OnWrite() 
  166. {
  167. //获得学生数量
  168. int nCount = m_lstLeft.GetCount();
  169. for(int i=0;i<nCount;i++)
  170. {
  171. //将学生写入ini文件
  172. CString strKeyName,strStudentName;
  173. m_lstLeft.GetText(i,strStudentName);
  174. strKeyName.Format("Name%d",i);
  175. ::WritePrivateProfileString("StudentName",strKeyName,strStudentName,".\Student.ini");
  176. }
  177. //将学生数量写入ini文件
  178. CString strCount;
  179. strCount.Format("%d",nCount);
  180. ::WritePrivateProfileString("StudentCount","Count",strCount,".\Student.ini");
  181. }