📄 coursedlg.cpp
字号:
// CourseDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Course.h"
#include "CourseDlg.h"
#include "MyCourse.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()
/////////////////////////////////////////////////////////////////////////////
// CCourseDlg dialog
CCourseDlg::CCourseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCourseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCourseDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CCourseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCourseDlg)
DDX_Control(pDX, IDsave, m_save);
DDX_Control(pDX, NEXT, m_NEXT);
DDX_Control(pDX, IDC_LAST, m_LAST);
DDX_Control(pDX, IDC_BEFORE, m_BEFORE);
DDX_Control(pDX, IDC_FIRST, m_FIRST);
DDX_Control(pDX, IDdelete, m_delete);
DDX_Control(pDX, IDedit, m_edit);
DDX_Control(pDX, IDC_TIME, m_TIME);
DDX_Control(pDX, IDC_TEACHER, m_TEACHER);
DDX_Control(pDX, IDC_NAME, m_NAME);
DDX_Control(pDX, IDC_MARK, m_MARK);
DDX_Control(pDX, IDC_ID, m_ID);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCourseDlg, CDialog)
//{{AFX_MSG_MAP(CCourseDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDadd, Onadd)
ON_BN_CLICKED(IDedit, Onedit)
ON_BN_CLICKED(IDsave, Onsave)
ON_BN_CLICKED(IDdelete, Ondelete)
ON_BN_CLICKED(IDC_FIRST, OnFirst)
ON_BN_CLICKED(IDC_BEFORE, OnBefore)
ON_BN_CLICKED(NEXT, OnNEXT)
ON_BN_CLICKED(IDC_LAST, OnLast)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCourseDlg message handlers
BOOL CCourseDlg::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
//初始化,显示表中第一条记录
thecourse.Open();
displaydata();
m_FIRST.EnableWindow(false);
m_BEFORE.EnableWindow(FALSE);
m_save.EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
}
void CCourseDlg::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 CCourseDlg::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 CCourseDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CCourseDlg::Onadd()
{
// TODO: Add your control notification handler code here
//首先清空
CWnd* t;
t=CWnd::GetDlgItem(IDC_ID);
t->SetWindowText("");
t=CWnd::GetDlgItem(IDC_NAME);
t->SetWindowText("");
t=CWnd::GetDlgItem(IDC_TIME);
t->SetWindowText("");
t=CWnd::GetDlgItem(IDC_MARK);
t->SetWindowText("");
t=CWnd::GetDlgItem(IDC_TEACHER);
t->SetWindowText("");
//添加时不可编辑和删除
m_edit.EnableWindow(false);
m_delete.EnableWindow(false);
//添加时各edit框可写
setstate(false);
state=0;
m_save.EnableWindow(true);
}
void CCourseDlg::Onedit()
{
// TODO: Add your control notification handler code here
//编辑时不可删除
m_delete.EnableWindow(false);
//编辑时各edit框可写
setstate(false);
state=1;
CWnd* t;
CString s;
t=CWnd::GetDlgItem(IDC_ID);
t->GetWindowText(s);
old=atoi(s);
m_save.EnableWindow(true);
}
void CCourseDlg::Onsave()
{
//信息必须填写完整
CWnd* t;
CString s1,s2,s3,s4,s5;
int Is1,Is4;
t=CWnd::GetDlgItem(IDC_ID);
t->GetWindowText(s1);
t=CWnd::GetDlgItem(IDC_NAME);
t->GetWindowText(s2);
t=CWnd::GetDlgItem(IDC_TIME);
t->GetWindowText(s3);
t=CWnd::GetDlgItem(IDC_MARK);
t->GetWindowText(s4);
t=CWnd::GetDlgItem(IDC_TEACHER);
t->GetWindowText(s5);
if ((s1=="")||(s2=="")||(s3=="")||(s4=="")||(s5==""))
{
MessageBox("请将信息填写完整!","错误提示");
return;
};
if ((s1<"0")||(s1>"9"))
{
MessageBox("课程编号和学分必须为数字!","错误提示");
((CEdit*)t)->GetFocus();
return;
}
else
{
Is1=atoi(s1);
}
if ((s4<"0")||(s4>"9"))
{
MessageBox("课程编号和学分必须为数字!","错误提示");
((CEdit*)t)->GetFocus();
return;
}
else
{
Is4=atoi(s4);
}
try
{
//ID号不可重复
if (((state==1)&&!(old==Is1))||(state==0))
{
CDaoDatabase Db;
CDaoRecordset Set(&Db);
CString sql;
sql.Format("SELECT * FROM course where CourseId=%d",Is1);
Db.Open("course.mdb");
Set.Open(dbOpenDynaset,sql);
long count;
count=Set.GetRecordCount();
if (count>0)
{
MessageBox("此ID号已存在,请重试!","信息提示");
return;
}
}
//在库中添加一条空的记录
if (state==0)
thecourse.AddNew();
//使当前记录处于编辑状态
else
thecourse.Edit();
//将控件中所输入内容赋给变量
thecourse.m_CourseId=Is1;
thecourse.m_CourseName=s2;
thecourse.m_CourseTime=s3;
thecourse.m_Mark=Is4;
thecourse.m_Teacher=s5;
//更新数据库
thecourse.Update();
//重新获得数据
thecourse.Requery();
}
catch (CDaoException* e)
{
e->ReportError();
return;
}
MessageBox("保存成功!","信息提示");
//保存后使编辑和删除按钮可用
m_edit.EnableWindow();
m_delete.EnableWindow();
//保存后使各edit框只读
setstate(true);
m_save.EnableWindow(false);
}
void CCourseDlg::Ondelete()
{
if (MessageBox("确定要删除吗?","提示",MB_YESNO)==IDYES)
{
//删除
thecourse.Delete();
//重新获得记录集
thecourse.Requery();
MessageBox("删除成功!","提示信息");
displaydata();
}
}
void CCourseDlg::OnFirst()
{
thecourse.MoveFirst();
m_FIRST.EnableWindow(false);
m_BEFORE.EnableWindow(FALSE);
m_NEXT.EnableWindow(true);
m_LAST.EnableWindow(true);
displaydata();
}
void CCourseDlg::OnBefore()
{
thecourse.MovePrev();
m_NEXT.EnableWindow(true);
m_LAST.EnableWindow(true);
displaydata();
thecourse.MovePrev();
if (thecourse.IsBOF())
{
m_FIRST.EnableWindow(false);
m_BEFORE.EnableWindow(FALSE);
}
thecourse.MoveNext();
}
void CCourseDlg::OnNEXT()
{
thecourse.MoveNext();
m_FIRST.EnableWindow(true);
m_BEFORE.EnableWindow(true);
displaydata();
thecourse.MoveNext();
if (thecourse.IsEOF())
{
m_NEXT.EnableWindow(FALSE);
m_LAST.EnableWindow(FALSE);
}
thecourse.MovePrev();
}
void CCourseDlg::OnLast()
{
thecourse.MoveLast();
m_FIRST.EnableWindow(true);
m_BEFORE.EnableWindow(true);
m_NEXT.EnableWindow(FALSE);
m_LAST.EnableWindow(FALSE);
displaydata();
}
void CCourseDlg::displaydata()
{
char s[10];
itoa(thecourse.m_CourseId,s,10);
m_ID.SetWindowText(s);
m_NAME.SetWindowText(thecourse.m_CourseName);
m_TIME.SetWindowText(thecourse.m_CourseTime);
itoa(thecourse.m_Mark,s,10);
m_MARK.SetWindowText(s);
m_TEACHER.SetWindowText(thecourse.m_Teacher);
}
void CCourseDlg::setstate(bool breadstate)
{
((CEdit*)GetDlgItem(IDC_ID))->SetReadOnly(breadstate);
((CEdit*)GetDlgItem(IDC_NAME))->SetReadOnly(breadstate);
((CEdit*)GetDlgItem(IDC_TIME))->SetReadOnly(breadstate);
((CEdit*)GetDlgItem(IDC_MARK))->SetReadOnly(breadstate);
((CEdit*)GetDlgItem(IDC_TEACHER))->SetReadOnly(breadstate);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -