📄 b06040403_a_1dlg.cpp
字号:
// B06040403_a_1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "B06040403_a_1.h"
#include "B06040403_a_1Dlg.h"
#include "Student.h"
#include "string.h"
#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()
/////////////////////////////////////////////////////////////////////////////
// CB06040403_a_1Dlg dialog
CB06040403_a_1Dlg::CB06040403_a_1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CB06040403_a_1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CB06040403_a_1Dlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CB06040403_a_1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CB06040403_a_1Dlg)
DDX_Control(pDX, IDC_LIST1, m_MyList);
DDX_Control(pDX, IDC_EDIT3, m_Grade);
DDX_Control(pDX, IDC_EDIT2, m_Name);
DDX_Control(pDX, IDC_EDIT1, m_Number);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CB06040403_a_1Dlg, CDialog)
//{{AFX_MSG_MAP(CB06040403_a_1Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnAdd)
ON_BN_CLICKED(IDC_BUTTON2, OnOpen)
ON_BN_CLICKED(IDC_BUTTON3, OnSave)
ON_BN_CLICKED(IDC_BUTTON4, OnDel)
ON_BN_CLICKED(IDC_BUTTON5, OnDelAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CB06040403_a_1Dlg message handlers
BOOL CB06040403_a_1Dlg::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_MyList.SetExtendedStyle(LVS_EX_FULLROWSELECT);
CRect rt;
m_MyList.GetClientRect(&rt);
m_MyList.InsertColumn(0,"学号",LVCFMT_LEFT,rt.right/3,0);
m_MyList.InsertColumn(1,"姓名",LVCFMT_LEFT,rt.right/3,0);
m_MyList.InsertColumn(2,"成绩",LVCFMT_LEFT,rt.right/3,0);
num=0;
return TRUE; // return TRUE unless you set the focus to a control
}
void CB06040403_a_1Dlg::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 CB06040403_a_1Dlg::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 CB06040403_a_1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CB06040403_a_1Dlg::OnAdd()
{
// TODO: Add your control notification handler code here
char Number[10],Name[10],Grade[4];
for(int i=0;i<10;i++)
{
Number[i]=NULL;
Name[i]=NULL;
Grade[i]=NULL;
} //初始化
m_Number.GetWindowText(Number,10);
m_Name.GetWindowText(Name,10);
m_Grade.GetWindowText(Grade,4); //以字符串形式获取学号、姓名、成绩
if(strlen(Number)==0 | strlen(Name)==0 | strlen(Grade)==0) //如果有一样没输全
{
MessageBox("信息没有填写完整!");
}
int flag=0,flag1=0,flag2=0;
for(int m=0;m<4;m++)
{
if(Grade[m]=='.')
{
flag=1;
break;
}
}
for(m=0;Grade[m]!='\0';m++)
{
if(Grade[m]<'0'||Grade[m]>'9')
{
flag2=1;
break;
}
}
for(i=0;i<num;i++)
{
if(strcmp(s[num].Number,Number)==0)
{
flag1=1;
break;
}
}
if(flag==1)
{
MessageBox("成绩不能为小数!请重新输入"); //设定输入分数为整数
}
else if(flag1==1)
{
MessageBox("学号不能重复!请重新输入");
}
else if(flag2==1)
{
MessageBox("成绩无效!请重新输入");
}
else
{
num++;
int t;
t=atoi(Grade); //把成绩转成整形
if(t<=100&&t>=0)
{
strcpy(s[num].Number,Number);
strcpy(s[num].Name,Name);
strcpy(s[num].Grade,Grade); //把数据存到对象中
for(i=0;i<(strlen(Grade));i++)
{
s[num].grade=s[num].grade+(Grade[i]-'0')*10;
}
for(i=0;i<num;i++){
char temp[4];
for(int j=0;j<4;j++)
{
temp[j]=NULL;
}
strcpy(temp,m_MyList.GetItemText(i,2));
int n;
n=atoi(temp);
if(n<=t)
{
m_MyList.InsertItem(i,Number);
m_MyList.SetItemText(i,1,Name);
m_MyList.SetItemText(i,2,Grade);
break;
}
}
}
else
MessageBox("分数输入错误!应为0~100,请重新输入!");
UpdateData(true);
}
}
void CB06040403_a_1Dlg::OnOpen()
{
// TODO: Add your control notification handler code here
//MessageBox("如果表中现在有数据,则必须先全部删除,否则出错!");
m_MyList.DeleteAllItems(); //先清空目前列表内容
CFileDialog filedlg(true);
filedlg.m_ofn.lpstrTitle="我的文件打开对话框";
filedlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
char *ch;
DWORD FileLenght;
if(IDOK==filedlg.DoModal())
{
CFile file(filedlg.GetFileName(),CFile::modeRead);
FileLenght=file.GetLength();
ch=new char[FileLenght+1];
ch[FileLenght]=0;
file.Read(ch,FileLenght);
file.Close();
char number[10],name[10],grade[4];
int Num=0;
for(int i=0;i<FileLenght;)
{
for(int j=0;j<10;j++)
{
number[j]=NULL;
name[j]=NULL;
}
for(j=0;j<4;j++)
{
grade[j]=NULL;
}
j=0;
while(*(ch+i)!='-')
{
number[j]=*(ch+i);
i++;
j++;
};
i++;
j=0;
while(*(ch+i)!='-')
{
name[j]=*(ch+i);
i++;
j++;
};
i++;
j=0;
while(*(ch+i)!='#')
{
grade[j]=*(ch+i);
i++;
j++;
};
i++;
m_MyList.InsertItem(0,number);
m_MyList.SetItemText(0,1,name);
m_MyList.SetItemText(0,2,grade);
Num++;
}
num=Num;
delete []ch;
}
}
void CB06040403_a_1Dlg::OnSave()
{
// TODO: Add your control notification handler code here
CFileDialog filedlg(false);
filedlg.m_ofn.lpstrTitle="我的文件保存对话框";
filedlg.m_ofn.lpstrFilter="Text Files(*.txt)\0*.txt\0All Files(*.*)\0*.*\0\0";
filedlg.m_ofn.lpstrDefExt="txt";
if(IDOK==filedlg.DoModal())
{
CFile file(filedlg.GetFileName(),CFile::modeCreate | CFile::modeWrite);
for(int i=num-1;i>=0;i--)
{
file.Write(m_MyList.GetItemText(i,0),strlen(m_MyList.GetItemText(i,0)));
file.Write("-",1);
file.Write(m_MyList.GetItemText(i,1),strlen(m_MyList.GetItemText(i,1)));
file.Write("-",1);
file.Write(m_MyList.GetItemText(i,2),strlen(m_MyList.GetItemText(i,2)));
file.Write("#",1);
}
file.Close();
}
}
void CB06040403_a_1Dlg::OnDel()
{
// TODO: Add your control notification handler code here
POSITION pos=m_MyList.GetFirstSelectedItemPosition();
if(pos==NULL)
MessageBox("你没有选中任何一项!");
else
{
while(pos)
{
int nItem=m_MyList.GetNextSelectedItem(pos);
m_MyList.DeleteItem(nItem);
}
}
}
void CB06040403_a_1Dlg::OnDelAll()
{
// TODO: Add your control notification handler code here
m_MyList.DeleteAllItems();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -