📄 studentdlg.cpp
字号:
// StudentDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Student.h"
#include "StudentDlg.h"
#include "StudentChangeDlg.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:
BOOL OnInitDialog();
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_pReadSet = NULL;
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// TODO: add construction code here
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_pReadSet = (CStudentDataSet *)OnGetRecordset();
CString strTmp;
strTmp.LoadString(IDS_DBNAME);
char path[ MAX_PATH ] = {'\0'};
GetCurrentDirectory( MAX_PATH, path);
m_SourseName.Format( "%s",path);
m_SourseName += strTmp;
try//打开数据库
{
m_dbStudent.Open(_T(m_SourseName));
}
catch (CDBException e1)
{
strTmp.LoadString(IDS_ERR_DB_OPEN);
AfxMessageBox(strTmp);
}
catch (CMemoryException e2)
{
strTmp.LoadString(IDS_ERR_MEMORY);
AfxMessageBox(strTmp);
}
CDaoTableDef m_pTableSQL(&m_dbStudent);
try//打开工作表
{
strTmp.LoadString(IDS_TABLE_NAME);
strTmp = '['+strTmp+']';
m_pTableSQL.Open(_T(strTmp));
}
catch (CDBException e1)
{
strTmp.LoadString(IDS_ERR_DB_OPEN);
AfxMessageBox(strTmp);
}
catch (CMemoryException e2)
{
strTmp.LoadString(IDS_ERR_MEMORY);
AfxMessageBox(strTmp);
}
}
void CStudentDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStudentDlg)
DDX_Control(pDX, IDC_BUTTON_DELETE, m_ButtonDelete);
DDX_Control(pDX, IDC_BUTTON_MODIFY, m_ButtonModify);
DDX_Control(pDX, IDC_STUDENT_LIST, m_lstStudent);
//}}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_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_DELETE, OnButtonDelete)
ON_BN_CLICKED(IDC_BUTTON_EXIT, OnButtonExit)
ON_BN_CLICKED(IDC_BUTTON_MODIFY, OnButtonModify)
ON_NOTIFY(NM_CLICK, IDC_STUDENT_LIST, OnClickStudentList)
ON_NOTIFY(NM_RCLICK, IDC_STUDENT_LIST, OnRclickStudentList)
ON_NOTIFY(LVN_KEYDOWN, IDC_STUDENT_LIST, OnKeydownStudentList)
ON_NOTIFY(NM_SETFOCUS, IDC_STUDENT_LIST, OnSetfocusStudentList)
ON_NOTIFY(NM_KILLFOCUS, IDC_STUDENT_LIST, OnKillfocusStudentList)
ON_BN_CLICKED(IDC_BUTTON_REFERSH, OnButtonRefersh)
//}}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
LV_COLUMN lc;
lc.mask = LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lc.fmt = LVCFMT_CENTER;
lc.cx = 75;
lc.iSubItem = 0;
lc.pszText = "学号";
m_lstStudent.InsertColumn(0,&lc);
lc.iSubItem = 1;
lc.pszText = "姓名";
m_lstStudent.InsertColumn(1,&lc);
lc.iSubItem = 2;
lc.pszText = "e_Mail地址";
m_lstStudent.InsertColumn(2,&lc);
lc.iSubItem = 3;
lc.pszText = "籍贯";
m_lstStudent.InsertColumn(3,&lc);
LVITEM Item;
int i;
CString str;
if(!m_pReadSet->IsEOF())m_pReadSet->MoveFirst();
for(i=0;!m_pReadSet->IsEOF();m_pReadSet->MoveNext(),i++){
Item.mask = LVIF_IMAGE|LVIF_TEXT;
Item.iItem = i;
Item.iSubItem = 0;
Item.iImage = i;
str = m_pReadSet->m_Stu.m_Stu_No;
Item.pszText = str.GetBuffer(str.GetLength());
m_lstStudent.InsertItem(&Item);
m_lstStudent.SetItemText(i,1,m_pReadSet->m_Stu.m_Stu_Name);
m_lstStudent.SetItemText(i,2,m_pReadSet->m_Stu.m_Stu_Email);
m_lstStudent.SetItemText(i,3,m_pReadSet->m_Stu.m_Stu_Home);
}
SetWindowLong(m_lstStudent.m_hWnd,GWL_STYLE,WS_VISIBLE|WS_CHILD|
WS_BORDER|LVS_REPORT|LVS_EDITLABELS);
m_lstStudent.SendMessage(LVS_SHOWSELALWAYS|LVS_SORTASCENDING);
//使删除和修改按钮无效
m_ButtonModify.EnableWindow(FALSE);
m_ButtonDelete.EnableWindow(FALSE);
CString strMainTitle;
strMainTitle.LoadString(IDS_MAIN_TITLE);
SetWindowText(strMainTitle);
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::OnButtonAdd()
{
// TODO: Add your control notification handler code here
CStudentChangeDlg dlg(m_lstStudent,-1);
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
if(!UpdateList(ADD_STU,&dlg.m_StuOnRefresh))
{
CString strErr;
strErr.LoadString(IDS_ERR_UPDARE);
MessageBox(_T(strErr),"ERROR",MB_OK|MB_ICONERROR);
return;
}
int ilen = m_lstStudent.GetItemCount();
if(m_lstStudent.InsertItem(ilen,dlg.m_StuOnRefresh.m_Stu_No)==-1)AfxMessageBox("ERROR");
m_lstStudent.SetItemText(ilen,1,dlg.m_StuOnRefresh.m_Stu_Name);
m_lstStudent.SetItemText(ilen,2,dlg.m_StuOnRefresh.m_Stu_Email);
m_lstStudent.SetItemText(ilen,3,dlg.m_StuOnRefresh.m_Stu_Home);
//if(!m_pSet->IsBOF())m_pSet->MoveFirst();
//m_pSet->MoveLast();
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
//*/
// Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
if(m_lstStudent.GetItemCount()<1){
m_ButtonModify.EnableWindow(FALSE);
m_ButtonDelete.EnableWindow(FALSE);
}
}
CDaoRecordset* CStudentDlg::OnGetRecordset()
{
if (m_pReadSet != NULL)
return m_pReadSet;
m_pReadSet = new CStudentDataSet(NULL);
m_pReadSet->Open();
return m_pReadSet;
}
void CStudentDlg::OnButtonDelete()
{
// TODO: Add your control notification handler code here
POSITION pos = m_lstStudent.GetFirstSelectedItemPosition();
int iCountSelected = m_lstStudent.GetSelectedCount();
if(iCountSelected<1)
{
CString strOnDeleteNoChoice,strDeleteTitle;
strOnDeleteNoChoice.LoadString(IDS_DELETE_NO_CHOICE);
strDeleteTitle.LoadString(IDS_MSGBOX_DELETE_TITLE);
MessageBox(strOnDeleteNoChoice,strDeleteTitle,MB_ICONINFORMATION|MB_OK);
return;
}
CList <int,int> m_ListSelcted;
int iItemSelected;
Student m_StuOnRefresh;
CString strOnDelete,strOnDeleteTitle;
strOnDelete.LoadString(IDS_ON_DELETE);
strOnDeleteTitle.LoadString(IDS_DELETE_TITLE);
int OKCANCLE = MessageBox(strOnDelete,strOnDeleteTitle,MB_ICONWARNING|MB_OKCANCEL);
if (OKCANCLE == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
for(int i=0;i<iCountSelected;i++){
iItemSelected = m_lstStudent.GetNextSelectedItem(pos);
m_ListSelcted.AddHead(iItemSelected);
}
for(;!m_ListSelcted.IsEmpty();m_ListSelcted.RemoveHead()){
iItemSelected =m_ListSelcted.GetHead();
char strTmp[41];
m_lstStudent.GetItemText(iItemSelected,0,strTmp,10);
m_StuOnRefresh.m_Stu_No = strTmp;
m_lstStudent.GetItemText(iItemSelected,1,strTmp,30);
m_StuOnRefresh.m_Stu_Name = strTmp;
m_lstStudent.GetItemText(iItemSelected,2,strTmp,40);
m_StuOnRefresh.m_Stu_Email = strTmp;
m_lstStudent.GetItemText(iItemSelected,3,strTmp,40);
m_StuOnRefresh.m_Stu_Home = strTmp;
if(!UpdateList(DELETE_STU,&m_StuOnRefresh))
{
CString strErr;
strErr.LoadString(IDS_ERR_UPDARE);
MessageBox(_T(strErr),"ERROR",MB_OK|MB_ICONERROR);
return;
}
m_lstStudent.DeleteItem(iItemSelected);
}
}
else if (OKCANCLE == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
}
if(m_lstStudent.GetItemCount()<1){
m_ButtonModify.EnableWindow(FALSE);
m_ButtonDelete.EnableWindow(FALSE);
}
}
void CStudentDlg::OnButtonExit()
{
CString strOnExit,strExitTitle;
strOnExit.LoadString(IDS_ON_EXIT);
strExitTitle.LoadString(IDS_EXIT_TITLE);
int YESNO = MessageBox(strOnExit,strExitTitle,MB_YESNO|MB_ICONQUESTION);
if (YESNO == IDYES)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
UpdateDatabase();//更新数据库
EndDialog(0);
}
else if (YESNO == IDNO)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
if(m_lstStudent.GetSelectedCount()<1){
m_ButtonModify.EnableWindow(FALSE);
m_ButtonDelete.EnableWindow(FALSE);
}
}
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -