PAGE1.CPP
上传用户:sanlisteel
上传日期:2008-06-19
资源大小:98k
文件大小:10k
- // Page1.cpp : implementation file
- //
- #include "stdafx.h"
- #include "tbvc.h"
- #include "Page1.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CPage1 property page
- IMPLEMENT_DYNCREATE(CPage1, CPropertyPage)
- CPage1::CPage1() : CPropertyPage(CPage1::IDD)
- {
- //{{AFX_DATA_INIT(CPage1)
- m_address = _T("");
- m_company = _T("");
- m_email = _T("");
- m_introduction = _T("");
- m_mobile = _T("");
- m_name = _T("");
- m_pager = _T("");
- m_position = _T("");
- m_relation = _T("");
- m_sex = _T("");
- m_telphone = _T("");
- m_age = 0;
- isAppend=FALSE;
- isEdit=FALSE;
- //}}AFX_DATA_INIT
-
- }
- CPage1::~CPage1()
- {
- if(m_pSet!=NULL)
- {
- m_pSet->Close();
- m_database->Close();
- delete m_pSet;
- delete m_database;
- }
- }
- void CPage1::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CPage1)
- DDX_Control(pDX, IDC_TELPHONE, m_edittelphone);
- DDX_Control(pDX, IDC_SEX, m_editsex);
- DDX_Control(pDX, IDC_RELATION, m_editrelation);
- DDX_Control(pDX, IDC_POSITION, m_editposition);
- DDX_Control(pDX, IDC_PAGER, m_editpager);
- DDX_Control(pDX, IDC_NAME, m_editname);
- DDX_Control(pDX, IDC_MOBILE, m_editmobile);
- DDX_Control(pDX, IDC_INTRODUCTION, m_editintroduction);
- DDX_Control(pDX, IDC_EMAIL, m_editemail);
- DDX_Control(pDX, IDC_COMPANY, m_editcompany);
- DDX_Control(pDX, IDC_AGE, m_editage);
- DDX_Control(pDX, IDC_ADDRESS, m_editaddress);
- DDX_Text(pDX, IDC_ADDRESS, m_address);
- DDX_Text(pDX, IDC_COMPANY, m_company);
- DDX_Text(pDX, IDC_EMAIL, m_email);
- DDX_Text(pDX, IDC_INTRODUCTION, m_introduction);
- DDX_Text(pDX, IDC_MOBILE, m_mobile);
- DDX_Text(pDX, IDC_NAME, m_name);
- DDX_Text(pDX, IDC_PAGER, m_pager);
- DDX_Text(pDX, IDC_POSITION, m_position);
- DDX_Text(pDX, IDC_RELATION, m_relation);
- DDX_Text(pDX, IDC_SEX, m_sex);
- DDX_Text(pDX, IDC_TELPHONE, m_telphone);
- DDX_Text(pDX, IDC_AGE, m_age);
- DDV_MinMaxInt(pDX, m_age, 0, 100);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CPage1, CPropertyPage)
- //{{AFX_MSG_MAP(CPage1)
- ON_BN_CLICKED(IDC_NEXT, OnNext)
- ON_BN_CLICKED(IDC_FIRST, OnFirst)
- ON_BN_CLICKED(IDC_LAST, OnLast)
- ON_BN_CLICKED(IDC_PREV, OnPrev)
- ON_BN_CLICKED(IDC_EDITRECORD, OnEditrecord)
- ON_WM_CTLCOLOR()
- ON_BN_CLICKED(IDC_APPENDRECORD, OnAppendrecord)
- ON_BN_CLICKED(IDC_DELETERECORD, OnDeleterecord)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CPage1 message handlers
- BOOL CPage1::OnInitDialog()
- { //MessageBox("CPage1::OnInitDialog","test");
- CPropertyPage::OnInitDialog();
- CWaitCursor wait;
- //Create Dialog BkColor Brush
- m_brush.CreateSolidBrush(RGB(0,170,170));
-
- //Set All Edits Control ReadOnly State
- SetEditReadOnly(TRUE);
- try
- {
- CString sDSN="ODBC;DRIVER=Microsoft Access Driver (*.mdb);DSN='';DBQ=telbook.mdb";
- m_database=new CDatabase;
- if(!m_database->Open(NULL,FALSE,FALSE,sDSN,FALSE))
- {
- //AfxMessageBox("ODBC Connection Faile!");
- CDialog::EndDialog(0);
- GetParent()->PostMessage(WM_CLOSE);
- return FALSE;
- }
-
- CString strSQL="select * from personlist";
- m_pSet=new CMyRecordset(m_database);
- m_pSet->Open(CRecordset::dynaset,strSQL);
- if(isEmpty())
- {
- SetNoRecordBtnState();
- return FALSE;
- }
-
- GetValue();
- SetButtonState();
-
- }
- catch(CDBException* pEx)
- {
- pEx->ReportError();
- }
-
- return TRUE;
-
- }
- void CPage1::ClearEditText()
- {
- m_address = _T("");
- m_company = _T("");
- m_email = _T("");
- m_introduction = _T("");
- m_mobile = _T("");
- m_name = _T("");
- m_pager = _T("");
- m_position = _T("");
- m_relation = _T("");
- m_sex = _T("");
- m_telphone = _T("");
- m_age = 0;
- UpdateData(FALSE);
- }
- void CPage1::GetValue()
- {
- m_name = m_pSet->m_name;
- m_age = m_pSet->m_age;
- m_sex = m_pSet->m_sex;
- m_email = m_pSet->m_email;
- m_relation = m_pSet->m_friendship;
- m_company = m_pSet->m_company;
- m_address = m_pSet->m_address;
- m_telphone = m_pSet->m_telphone;
- m_pager = m_pSet->m_pager;
- m_mobile = m_pSet->m_mobile;
- m_position = m_pSet->m_position;
- m_introduction = m_pSet->m_introduction;
- UpdateData(FALSE);
- }
- void CPage1::SetValue()
- {
- UpdateData(TRUE);
- m_pSet->m_name = m_name;
- m_pSet->m_age = m_age;
- m_pSet->m_sex = m_sex;
- m_pSet->m_email = m_email;
- m_pSet->m_friendship= m_relation;
- m_pSet->m_company = m_company;
- m_pSet->m_address = m_address;
- m_pSet->m_telphone = m_telphone;
- m_pSet->m_pager = m_pager;
- m_pSet->m_mobile = m_mobile;
- m_pSet->m_position = m_position;
- m_pSet->m_introduction = m_introduction;
- }
- void CPage1::OnFirst()
- {
- m_pSet->MoveFirst();
- OnPrev();
- }
- void CPage1::OnPrev()
- {
- m_pSet->MovePrev();
- GetValue();
- SetButtonState();
- }
- void CPage1::OnNext()
- {
- m_pSet->MoveNext();
- GetValue();
- SetButtonState();
- }
- void CPage1::OnLast()
- {
- m_pSet->MoveLast();
- OnNext();
- }
- void CPage1::SetButtonState()
- {
- if(IsFirstRecord())
- {
- GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
- GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
- }
- else
- {
- GetDlgItem(IDC_FIRST)->EnableWindow(TRUE);
- GetDlgItem(IDC_PREV)->EnableWindow(TRUE);
- }
- if(IsLastRecord())
- {
- GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
- GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
- }
- else
- {
- GetDlgItem(IDC_NEXT)->EnableWindow(TRUE);
- GetDlgItem(IDC_LAST)->EnableWindow(TRUE);
- }
- }
-
- BOOL CPage1::IsFirstRecord()
- {
- if(m_pSet->IsBOF()&&m_pSet->IsEOF())
- {
- return TRUE;
- }
- else if(m_pSet->IsBOF())
- {
- m_pSet->MoveFirst();
- return TRUE;
- }
- else
- return FALSE;
- }
- BOOL CPage1::IsLastRecord()
- {
- if(m_pSet->IsBOF()&&m_pSet->IsEOF())
- {
- return TRUE;
- }
- else if(m_pSet->IsEOF())
- {
- m_pSet->MoveLast();
- return TRUE;
- }
- else
- return FALSE;
- }
- HBRUSH CPage1::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = (HBRUSH)m_brush;
-
- if(nCtlColor==CTLCOLOR_STATIC)
- { pDC->SetBkMode(TRANSPARENT);
- return hbr;
- }
- if(nCtlColor==CTLCOLOR_EDIT)
- {
- return NULL;
- }
-
- return hbr;
- }
- void CPage1::SetEditReadOnly(BOOL isReadOnly)
- {
- m_editname.SetReadOnlyState(isReadOnly);
- m_editage.SetReadOnlyState(isReadOnly);
- m_editsex.SetReadOnlyState(isReadOnly);
- m_editrelation.SetReadOnlyState(isReadOnly);
- m_editposition.SetReadOnlyState(isReadOnly);
- m_editemail.SetReadOnlyState(isReadOnly);
- m_edittelphone.SetReadOnlyState(isReadOnly);
- m_editpager.SetReadOnlyState(isReadOnly);
- m_editmobile.SetReadOnlyState(isReadOnly);
- m_editaddress.SetReadOnlyState(isReadOnly);
- m_editcompany.SetReadOnlyState(isReadOnly);
- m_editintroduction.SetReadOnlyState(isReadOnly);
- }
- void CPage1::SetNavButtonVisible(int isVisible)
- {
-
- GetDlgItem(IDC_FIRST)->ShowWindow(isVisible);
- GetDlgItem(IDC_PREV)->ShowWindow(isVisible);
- GetDlgItem(IDC_NEXT)->ShowWindow(isVisible);
- GetDlgItem(IDC_LAST)->ShowWindow(isVisible);
- }
- void CPage1::SetButtonText(BOOL blSet)
- {
- if(blSet)
- {
- GetDlgItem(IDC_EDITRECORD)->ShowWindow(SW_HIDE);
- GetDlgItem(IDC_APPENDRECORD)->SetWindowText(_T("确 定"));
- GetDlgItem(IDC_DELETERECORD)->SetWindowText(_T("取 消"));
- }
- else
- {
- GetDlgItem(IDC_EDITRECORD)->ShowWindow(SW_SHOW);
- GetDlgItem(IDC_APPENDRECORD)->SetWindowText(_T("添加新记录"));
- GetDlgItem(IDC_DELETERECORD)->SetWindowText(_T("删除记录"));
- }
- }
- //添加新纪录功能 && 确定按钮事件
- void CPage1::OnAppendrecord()
- {
- if(isAppend||isEdit)
- {
- try
- { if(!UpdateData(TRUE)) return;
- if(m_name.IsEmpty())
- {
- MessageBox("[姓名] 字段必须填写内容!","注意",MB_ICONINFORMATION|MB_OK);
- return;
- }
- //if(!CheckData())
- // return;
- SetValue();
- m_pSet->Update();
- if(isAppend)
- {
- m_pSet->Requery();
- m_pSet->MoveLast();
- }
- SetNavButtonVisible(SW_SHOW);
- SetButtonText(FALSE);
- SetEditReadOnly(TRUE);
- isAppend=FALSE;
- isEdit=FALSE;
- GetValue();
- SetButtonState();
- }
- catch(CDBException* pEx)
- {
- pEx->ReportError();
- }
- }
- else
- {
- if(m_pSet->CanAppend())
- {
- SetNavButtonVisible(SW_HIDE);
- SetButtonText(TRUE);
- m_pSet->AddNew();
- if(isEmpty())
- {
- GetDlgItem(IDC_EDITRECORD)->EnableWindow(TRUE);
- GetDlgItem(IDC_DELETERECORD)->EnableWindow(TRUE);
- }
- SetEditReadOnly(FALSE);
- ClearEditText();
- isAppend=TRUE;
- m_editname.SetFocus();
- }
- else
- {
- AfxMessageBox("Can't Append New Record");
- }
- }
-
- }
- //删除记录功能 && 取消功能
- void CPage1::OnDeleterecord()
- {
- // TODO: Add your control notification handler code here
- if(isAppend||isEdit)
- {
- m_pSet->CancelUpdate();
- SetNavButtonVisible(SW_SHOW);
- SetButtonText(FALSE);
- SetEditReadOnly(TRUE);
- if(isEmpty())
- SetNoRecordBtnState();
- else
- GetValue();
- isEdit=FALSE;
- isAppend=FALSE;
- }
- else
-
- if(MessageBox("确定要删除这条记录吗?","删除记录",MB_YESNO|MB_ICONINFORMATION) == IDYES)
- {
- m_pSet->Delete();
- try
- {
- OnNext();
- if(isEmpty())
- {
- SetNoRecordBtnState();
- return;
- }
-
- SetButtonState();
- GetValue();
-
- }
- catch(CDBException* pEx)
- {
- pEx->ReportError();
- }
- }
- }
- void CPage1::OnEditrecord()
- {
- if(m_pSet->CanAppend())
- {
- SetNavButtonVisible(SW_HIDE);
- SetButtonText(TRUE);
- SetEditReadOnly(FALSE);
- m_pSet->Edit();
- isEdit=TRUE;
- m_editname.SetFocus();
- }
- else
- {
- AfxMessageBox("Can't Modify the Record");
- }
- }
- void CPage1::SetNoRecordBtnState()
- {
- GetDlgItem(IDC_FIRST)->EnableWindow(FALSE);
- GetDlgItem(IDC_PREV)->EnableWindow(FALSE);
- GetDlgItem(IDC_NEXT)->EnableWindow(FALSE);
- GetDlgItem(IDC_LAST)->EnableWindow(FALSE);
- GetDlgItem(IDC_EDITRECORD)->EnableWindow(FALSE);
- GetDlgItem(IDC_DELETERECORD)->EnableWindow(FALSE);
- ClearEditText();
- }
- BOOL CPage1::isEmpty()
- {
- if(m_pSet->IsBOF() && m_pSet->IsEOF())
- {
-
- return TRUE;
- }
- else
- return FALSE;
- }