
// Ch7Demo1Dlg.cpp : 实现文件
//

#include "stdafx.h"
#include "Ch7Demo1.h"
#include "Ch7Demo1Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CCh7Demo1Dlg 对话框




CCh7Demo1Dlg::CCh7Demo1Dlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(CCh7Demo1Dlg::IDD, pParent)
	, m_strSNo_Q(_T(""))
	, m_strSNAME_Q(_T(""))
	, m_strSNo(_T(""))
	, m_strSName(_T(""))
	, m_strSSex(_T(""))
	, m_strSMajor(_T(""))
	, m_strSBDate(_T(""))
	, m_strSSDate(_T(""))
	, m_bAdd(false)
	, m_bEdit(false)
	, m_bEmpty(false)
	, m_bFirst(false)
	, m_bLast(false)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	
}

void CCh7Demo1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	
	DDX_Text(pDX, IDC_EDIT_SMAJOR, m_strSMajor);
	DDX_Text(pDX, IDC_EDIT_SNAME, m_strSName);
	DDX_Text(pDX, IDC_EDIT_SNo, m_strSNo);
	DDX_Text(pDX, IDC_EDIT_SSDATE, m_strSSDate);
	DDX_Text(pDX, IDC_EDIT_SSEX, m_strSSex);
	DDX_Text(pDX, IDC_EDIT_SBDATE, m_strSBDate);
	DDX_Text(pDX, IDC_EDIT_SNo_Q, m_strSNo_Q);
	DDX_Text(pDX, IDC_EDIT_SNAME_Q, m_strSNAME_Q);
}

BEGIN_MESSAGE_MAP(CCh7Demo1Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BN_NEXT, &CCh7Demo1Dlg::OnBnClickedBnNext)
	ON_BN_CLICKED(IDC_BN_PREV, &CCh7Demo1Dlg::OnBnClickedBnPrev)
	ON_BN_CLICKED(IDC_BN_FIRST, &CCh7Demo1Dlg::OnBnClickedBnFirst)
	ON_BN_CLICKED(IDC_BN_LAST, &CCh7Demo1Dlg::OnBnClickedBnLast)
	ON_BN_CLICKED(IDC_BN_QUERY, &CCh7Demo1Dlg::OnBnClickedBnQuery)
	ON_BN_CLICKED(IDC_BN_ADD, &CCh7Demo1Dlg::OnBnClickedBnAdd)
	ON_BN_CLICKED(IDC_BN_EDIT, &CCh7Demo1Dlg::OnBnClickedBnEdit)
	ON_BN_CLICKED(IDC_BN_DELETE, &CCh7Demo1Dlg::OnBnClickedBnDelete)
	ON_BN_CLICKED(IDC_BN_EXIT, &CCh7Demo1Dlg::OnBnClickedBnExit)
	ON_BN_CLICKED(IDC_BN_SAVE, &CCh7Demo1Dlg::OnBnClickedBnSave)
	ON_BN_CLICKED(IDC_BN_CANCEL, &CCh7Demo1Dlg::OnBnClickedBnCancel)
END_MESSAGE_MAP()


// CCh7Demo1Dlg 消息处理程序

BOOL CCh7Demo1Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	m_rsStudentSet.Open(AFX_DB_USE_DEFAULT_TYPE);
	if(!m_rsStudentSet.IsOpen())
	{
		AfxMessageBox("数据库打开失败!");
		return TRUE;
	}
	else if(m_rsStudentSet.IsBOF())
	{
		AfxMessageBox("数据集空！");
		m_bEmpty=true;
		ClearEditData();
		SetMoveBNState();
		SetEditState();
	}
	else
	{
		m_bEmpty=false;
		m_rsStudentSet.MoveFirst();
		m_bFirst=true;
		m_bLast=false;
		ReadRecord();
		SetMoveBNState();
		SetEditState();
	}
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CCh7Demo1Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CCh7Demo1Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		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;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCh7Demo1Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CCh7Demo1Dlg::OnBnClickedBnNext()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bFirst=false;
	m_bLast=false;
	m_rsStudentSet.MoveNext();
	if(m_rsStudentSet.IsEOF())
	{
		m_rsStudentSet.MoveLast();
		m_bLast=true;
	}
	SetMoveBNState();
	ReadRecord();
}


void CCh7Demo1Dlg::OnBnClickedBnPrev()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bFirst=false;
	m_bLast=false;
	m_rsStudentSet.MovePrev();
	if(m_rsStudentSet.IsBOF())
	{
		m_rsStudentSet.MoveFirst();
		m_bFirst=TRUE;
	}
	SetMoveBNState();
	ReadRecord();
}


void CCh7Demo1Dlg::OnBnClickedBnFirst()
{
	// TODO: 在此添加控件通知处理程序代码
	m_rsStudentSet.MoveFirst();
	m_bFirst=true;
	m_bLast=false;
	SetMoveBNState();
	ReadRecord();

}


void CCh7Demo1Dlg::OnBnClickedBnLast()
{
	// TODO: 在此添加控件通知处理程序代码
	m_rsStudentSet.MoveLast();
	m_bFirst=false;
	m_bLast=true;
	SetMoveBNState();
	ReadRecord();
}


void CCh7Demo1Dlg::OnBnClickedBnQuery()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if(m_strSNo_Q.IsEmpty() && m_strSNAME_Q.IsEmpty())
	{	
		m_rsStudentSet.m_strFilter="";
		m_rsStudentSet.Requery();
		if(m_rsStudentSet.IsBOF())
		{
			AfxMessageBox("数据集空！");
			m_bEmpty=true;
			ClearEditData();
			SetMoveBNState();
		}
		else
		{
			m_bEmpty=false;
			m_rsStudentSet.MoveFirst();
			m_bFirst=true;
			m_bLast=false;
			ReadRecord();
			SetMoveBNState();
		}
		return;
	}
	BOOL mbSNoQInput=FALSE;
	if(!m_strSNo_Q.IsEmpty())
	{
		m_rsStudentSet.m_strFilter="SNo='"+m_strSNo_Q;
		m_rsStudentSet.m_strFilter=m_rsStudentSet.m_strFilter+"'";
		
		mbSNoQInput=TRUE;	
	}
	if(!m_strSNAME_Q.IsEmpty())
	{
		if(mbSNoQInput)
		{
			m_rsStudentSet.m_strFilter=m_rsStudentSet.m_strFilter
			                        +" AND SName='";
			m_rsStudentSet.m_strFilter=m_rsStudentSet.m_strFilter
			                        +m_strSNAME_Q;
			m_rsStudentSet.m_strFilter=m_rsStudentSet.m_strFilter
									+"'";
		}	                        							
		else
		{
			m_rsStudentSet.m_strFilter="SName='"+m_strSNAME_Q;
			m_rsStudentSet.m_strFilter=m_rsStudentSet.m_strFilter+"'";

		}
		
	}
	m_rsStudentSet.Requery();
	if(m_rsStudentSet.IsEOF())
	{
		AfxMessageBox("没有查到相关记录！");
		m_bEmpty=true;
		ClearEditData();
		SetMoveBNState();
	}
	else
	{
		m_bEmpty=false;
		m_rsStudentSet.MoveFirst();
		m_bFirst=true;
		ReadRecord();
		SetMoveBNState();
	}	

}

void CCh7Demo1Dlg::ReadRecord(void)
{
	m_strSNo=m_rsStudentSet.m_SNo;
	m_strSName=m_rsStudentSet.m_SName;
	m_strSSex=m_rsStudentSet.m_SSex;
	m_strSMajor=m_rsStudentSet.m_SMajor;
	m_strSBDate=m_rsStudentSet.m_SBirthDate.Format("%Y-%m-%d");
	m_strSSDate=m_rsStudentSet.m_SSchoolDate.Format("%Y-%m-%d");
	
	UpdateData(FALSE);
	return;
}

void CCh7Demo1Dlg::WriteRecord(void)
{
	UpdateData(TRUE);

	m_rsStudentSet.m_SNo=m_strSNo;
	m_rsStudentSet.m_SName=m_strSName;
	m_rsStudentSet.m_SSex=m_strSSex;
	m_rsStudentSet.m_SMajor=m_strSMajor;
	
	int a,b,c;
	CString   timestrB=m_strSBDate;   
	sscanf(timestrB.GetBuffer(timestrB.GetLength()),"%d-%d-%d",&a,&b,&c);   
	CTime   timeB(a,b,c,0,0,0);
	m_rsStudentSet.m_SBirthDate=timeB;
	CString   timestrS=m_strSSDate; 
	sscanf(timestrS.GetBuffer(timestrS.GetLength()),"%d-%d-%d",&a,&b,&c);   
	CTime   timeS(a,b,c,0,0,0);
	m_rsStudentSet.m_SSchoolDate=timeS;
}

void CCh7Demo1Dlg::OnBnClickedBnAdd()
{
	// TODO: 在此添加控件通知处理程序代码
	ClearEditData();
	m_bEdit=true;
	m_bAdd=true;
	SetMoveBNState();
	SetEditState();
	CWnd *pWnd;
	pWnd=GetDlgItem(IDC_EDIT_SNo);
	pWnd->SetFocus();

}
void CCh7Demo1Dlg::OnBnClickedBnEdit()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bEdit=true;
	m_bAdd=false;
	SetMoveBNState();
	SetEditState();
	CWnd *pWnd;
	pWnd=GetDlgItem(IDC_EDIT_SNAME);
	pWnd->SetFocus();

}


void CCh7Demo1Dlg::OnBnClickedBnDelete()
{
	// TODO: 在此添加控件通知处理程序代码
	if(AfxMessageBox("确定要删除此条记录吗？",MB_YESNO)!=IDYES)
		return;
	m_rsStudentSet.Delete();
	//m_rsSCChoiceSet.MoveNext();
	m_rsStudentSet.Requery();
	if(m_rsStudentSet.IsBOF())
	{
		AfxMessageBox("表中已没有记录了！");
		ClearEditData();
		m_bEmpty=true;
		SetMoveBNState();
		return;
	}
	m_bFirst=true;
	m_bLast=false;
	ReadRecord();
	SetMoveBNState();
}


void CCh7Demo1Dlg::OnBnClickedBnExit()
{
	// TODO: 在此添加控件通知处理程序代码
	CDialog::OnOK();
}





void CCh7Demo1Dlg::ClearEditData(void)
{
	m_strSNo="";
	m_strSName="";
	m_strSSex="";
	m_strSMajor="";
	m_strSBDate="";
	m_strSSDate="";

	UpdateData(FALSE);
	return;
}

void CCh7Demo1Dlg::OnBnClickedBnSave()
{
	// TODO: 在此添加控件通知处理程序代码
	UpdateData(TRUE);
	if(m_strSNo.IsEmpty() || m_strSName.IsEmpty())
	{
		AfxMessageBox("请输入相应数据！");
		CWnd *pWnd;
		pWnd=GetDlgItem(IDC_EDIT_SNo);
		pWnd->SetFocus();
		return;
	}
	if(m_bAdd)
	{
		m_rsStudentSet.AddNew();
	}
	else
	{
		m_rsStudentSet.Edit();
	}
	WriteRecord();
	m_rsStudentSet.Update();
	m_rsStudentSet.Requery();
	m_rsStudentSet.MoveFirst();
	m_bFirst=true;
	m_bLast=false;
	m_bAdd=false;
	m_bEdit=false;
	ReadRecord();
	SetMoveBNState();
	SetEditState();
}


void CCh7Demo1Dlg::OnBnClickedBnCancel()
{
	// TODO: 在此添加控件通知处理程序代码
	m_bAdd=false;
	m_bEdit=false;
	ReadRecord();
	SetMoveBNState();
	SetEditState();
}


void CCh7Demo1Dlg::SetMoveBNState(void)
{
	CWnd *pWnd;
	if(m_bEmpty||m_bEdit)
	{
		pWnd=GetDlgItem(IDC_BN_FIRST);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_PREV);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_LAST);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_NEXT);
		pWnd->EnableWindow(FALSE);
		return;
	}
	if(m_bFirst)
	{
		pWnd=GetDlgItem(IDC_BN_FIRST);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_PREV);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_LAST);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_NEXT);
		pWnd->EnableWindow(TRUE);
	}
	else if(m_bLast)
	{
		pWnd=GetDlgItem(IDC_BN_FIRST);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_PREV);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_LAST);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_NEXT);
		pWnd->EnableWindow(FALSE);
	}
	else
	{
		pWnd=GetDlgItem(IDC_BN_FIRST);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_PREV);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_LAST);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_NEXT);
		pWnd->EnableWindow(TRUE);
	}
	return;
}


void CCh7Demo1Dlg::SetEditState(void)
{
	CWnd *pWnd;
	if(m_bEdit)
	{
		pWnd=GetDlgItem(IDC_BN_ADD);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_EDIT);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_DELETE);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_QUERY);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_SAVE);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_CANCEL);
		pWnd->EnableWindow(TRUE);

		pWnd=GetDlgItem(IDC_EDIT_SNo);
		if(m_bAdd)
			pWnd->EnableWindow(TRUE);
		else
			pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SNAME);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_EDIT_SSEX);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_EDIT_SMAJOR);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_EDIT_SBDATE);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_EDIT_SSDATE);
		pWnd->EnableWindow(TRUE);
	}
	else
	{
		pWnd=GetDlgItem(IDC_BN_ADD);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_EDIT);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_DELETE);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_QUERY);
		pWnd->EnableWindow(TRUE);
		pWnd=GetDlgItem(IDC_BN_SAVE);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_BN_CANCEL);
		pWnd->EnableWindow(FALSE);

		pWnd=GetDlgItem(IDC_EDIT_SNo);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SNAME);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SSEX);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SMAJOR);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SBDATE);
		pWnd->EnableWindow(FALSE);
		pWnd=GetDlgItem(IDC_EDIT_SSDATE);
		pWnd->EnableWindow(FALSE);
	}
	return;

}



