📄 studentdlg.cpp
字号:
// StudentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDlg.h"
#include <direct.h>
#include "StudentSet.h"
#include "ScoreSet.h"
#include "AddressSet.h"
void CreateDirectory(CString strDirectory);
bool ReadAngWrite(CString m_strSourceFile, CString m_strTargetFile);
bool CheckFile(CString strFileName);
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentDlg dialog
CStudentDlg::CStudentDlg(CWnd* pParent /*=NULL*/)
: CDialog(CStudentDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CStudentDlg)
m_strSourceFile = _T("");
m_strTargetFile = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStudentDlg)
DDX_Text(pDX, IDC_EDIT_SOURCE, m_strSourceFile);
DDX_Text(pDX, IDC_EDIT_TARGET, m_strTargetFile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CStudentDlg, CDialog)
//{{AFX_MSG_MAP(CStudentDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_SOURCE, OnButtonSource)
ON_BN_CLICKED(IDC_BUTTON_TARGET, OnButtonTarget)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CStudentDlg message handlers
BOOL CStudentDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
m_strSourceFile = "E:\\ygx\\student\\student.mdb";
m_strTargetFile = "E:\\ygx\\student\\Score.txt";
UpdateData(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CStudentDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CStudentDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CStudentDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CStudentDlg::OnOK()
{
// TODO: Add extra validation here
UpdateData(true);
if(!CheckFile(m_strTargetFile))
return;
if(!ReadAngWrite(m_strSourceFile, m_strTargetFile))
return;
CDialog::OnOK();
}
bool CheckFile(CString strFileName)
{
int nDirectoryLen = strFileName.ReverseFind('\\');
CString strDirectory = strFileName.Left(nDirectoryLen);
CFileFind find;
int n = strFileName.ReverseFind('.');
if(n == -1)
strFileName += ".txt";
if(find.FindFile(strFileName))
{
int i=AfxMessageBox(strFileName + " already exists. Overwrite it?", MB_YESNO|MB_ICONINFORMATION);
if (i == IDYES)
return true;
else
return false;
}
else
{
if(!find.FindFile(strDirectory + "\\*.*"))
{
int i=AfxMessageBox("The destination directory does not exist, do you want to create it?",MB_YESNO|MB_ICONINFORMATION);
if(i == IDYES)
{
CreateDirectory(strDirectory);
return true;
}
else
return false; // Open binary file
}
return true;
}
}
void CreateDirectory(CString strDirectory)
{
CString strParent = strDirectory.Left(strDirectory.ReverseFind('\\'));
CFileFind find;
if(!find.FindFile(strParent + "\\*.*"))
CreateDirectory(strParent);
_mkdir(strDirectory);
}
bool ReadAngWrite(CString m_strSourceFile, CString m_strTargetFile)
{
CStdioFile TextFile;
CDaoDatabase db;
TRY
{
db.Open(m_strSourceFile);
}
CATCH(CDaoException, e)
{
AfxMessageBox("The Access database " + m_strSourceFile + " can not be opened, please check the file name and path!");
return false;
}
END_CATCH
int n = m_strTargetFile.ReverseFind('.');
if(n == -1)
m_strTargetFile += ".txt";
TextFile.Open(m_strTargetFile, CFile::modeWrite | CFile::modeCreate); // Open text file
TextFile.WriteString("No.\tName\tAge\tScore1\tScore2\tScore3\r\n");
CStudentSet* pStudentSet = new CStudentSet(&db);
CScoreSet* pScoreSet = new CScoreSet(&db);
CAddressSet* pAddressSet = new CAddressSet(&db); // For Address table
pStudentSet->m_strSort = "[SNo]";
pStudentSet->Open();
pScoreSet->Open();
pAddressSet->Open();
pStudentSet->MoveFirst();
while(!pStudentSet->IsEOF())
{
TextFile.WriteString(pStudentSet->m_SNo);
TextFile.WriteString("\t");
TextFile.WriteString(pStudentSet->m_SName);
TextFile.WriteString("\t");
CString strTemp;
strTemp.Format("%2.0f", pStudentSet->m_SAge);
TextFile.WriteString(strTemp);
TextFile.WriteString("\t");
CString strSNo = pStudentSet->m_SNo;
CString strSQL;
strSQL = "Sno = '" + strSNo +"'";
int isFind = pScoreSet->FindFirst(strSQL);
if(isFind != 0)
{
strTemp.Format("%.1f",pScoreSet->m_SScore1);
TextFile.WriteString(strTemp);
TextFile.WriteString("\t");
strTemp.Format("%.1f",pScoreSet->m_SScore2);
TextFile.WriteString(strTemp);
TextFile.WriteString("\t");
strTemp.Format("%.1f",pScoreSet->m_SScore3);
TextFile.WriteString(strTemp);
TextFile.WriteString("\t");
}
else
{
CString strMess = "The student with number '";
strMess += strSNo;
strMess += "' were not found int the score table! A special value 999 was put that place.";
AfxMessageBox(strMess);
TextFile.WriteString("999\t999\t999");
}
pStudentSet->MoveNext();
TextFile.WriteString("\r\n");
}
TextFile.Close();
pStudentSet->Close();
pScoreSet->Close();
pAddressSet->Close();
db.Close();
return true;
}
void CStudentDlg::OnButtonSource()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(true,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
dlg.m_ofn.lpstrInitialDir = m_strSourceFile;
dlg.m_ofn.lpstrTitle = "Select a Database";
dlg.m_ofn.lpstrFilter = "Microsoft Access(*.mdb)\0*.mdb\0\0";
int len = m_strSourceFile.GetLength() - m_strSourceFile.ReverseFind('\\');
strcpy(dlg.m_ofn.lpstrFile, m_strSourceFile.Right(len - 1));
if(dlg.DoModal() == IDOK)
{
m_strSourceFile = dlg.GetPathName();
UpdateData(false);
}
}
void CStudentDlg::OnButtonTarget()
{
// TODO: Add your control notification handler code here
CFileDialog dlg(false,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT);
dlg.m_ofn.lpstrInitialDir = m_strTargetFile;
dlg.m_ofn.lpstrTitle = "SelectFlatTitle";
dlg.m_ofn.lpstrFilter = "*.txt\0*.txt\0\0";
int len = m_strTargetFile.GetLength() - m_strTargetFile.ReverseFind('\\');
strcpy(dlg.m_ofn.lpstrFile, m_strTargetFile.Right(len - 1));
if(dlg.DoModal() == IDOK)
{
m_strTargetFile = dlg.GetPathName();
UpdateData(false);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -