www.pudn.com > 6-7cha.rar > STUDlg.cpp


// STUDlg.cpp : implementation file 
// 
 
#include "stdafx.h" 
#include "STU.h" 
#include "STUDlg.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() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSTUDlg dialog 
 
CSTUDlg::CSTUDlg(CWnd* pParent /*=NULL*/) 
	: CDialog(CSTUDlg::IDD, pParent) 
{ 
	//{{AFX_DATA_INIT(CSTUDlg) 
	m_editID = _T(""); 
	m_editDepart = _T(""); 
	m_editName = _T(""); 
	m_editBirth = _T(""); 
	//}}AFX_DATA_INIT 
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); 
} 
 
void CSTUDlg::DoDataExchange(CDataExchange* pDX) 
{ 
	CDialog::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CSTUDlg) 
	DDX_Control(pDX, IDC_BUTTON_CONFIRM, m_btnConfirm); 
	DDX_Control(pDX, IDC_BUTTON_UPDATE, m_btnUpdate); 
	DDX_Control(pDX, IDC_BUTTON_NEXT, m_btnNext); 
	DDX_Control(pDX, IDC_BUTTON_PRE, m_btnPre); 
	DDX_Text(pDX, IDC_EDIT_ID, m_editID); 
	DDV_MaxChars(pDX, m_editID, 6); 
	DDX_Text(pDX, IDC_EDIT_DEPART, m_editDepart); 
	DDV_MaxChars(pDX, m_editDepart, 20); 
	DDX_Text(pDX, IDC_EDIT_NAME, m_editName); 
	DDV_MaxChars(pDX, m_editName, 8); 
	DDX_Text(pDX, IDC_EDIT_DEPART2, m_editBirth); 
	DDV_MaxChars(pDX, m_editBirth, 8); 
	//}}AFX_DATA_MAP 
} 
 
BEGIN_MESSAGE_MAP(CSTUDlg, CDialog) 
	//{{AFX_MSG_MAP(CSTUDlg) 
	ON_WM_SYSCOMMAND() 
	ON_WM_PAINT() 
	ON_WM_QUERYDRAGICON() 
	ON_BN_CLICKED(IDC_BUTTON_NEXT, OnButtonNext) 
	ON_BN_CLICKED(IDC_BUTTON_PRE, OnButtonPre) 
	ON_BN_CLICKED(IDC_BUTTON_UPDATE, OnButtonUpdate) 
	ON_EN_CHANGE(IDC_EDIT_ID, OnChangeEditId) 
	ON_EN_CHANGE(IDC_EDIT_NAME, OnChangeEditName) 
	ON_EN_CHANGE(IDC_EDIT_DEPART, OnChangeEditDepart) 
	ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd) 
	ON_BN_CLICKED(IDC_BUTTON_CONFIRM, OnButtonConfirm) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSTUDlg message handlers 
 
BOOL CSTUDlg::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 
	if(!InitADO()) MessageBox("初始化ADO失败!"); 
	else  
	{ 
		if(SUCCEEDED(pRs->MoveFirst())) 
		{ 
			if(m_baseInfo.m_ul_IDStatus==adFldOK) 
				m_editID=m_baseInfo.m_ID; 
			if(m_baseInfo.m_ul_nameStatus==adFldOK) 
				m_editName=m_baseInfo.m_name; 
			if(m_baseInfo.m_ul_departStatus==adFldOK) 
				m_editDepart=m_baseInfo.m_department; 
		} 
	} 
	UpdateData(FALSE); 
	m_btnUpdate.EnableWindow(FALSE); 
	m_btnConfirm.EnableWindow(FALSE); 
	return TRUE;  // return TRUE  unless you set the focus to a control 
} 
 
void CSTUDlg::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 CSTUDlg::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 CSTUDlg::OnQueryDragIcon() 
{ 
	return (HCURSOR) m_hIcon; 
} 
 
 
bool CSTUDlg::InitADO() 
{    
   	 
	try  
	{    
		pCon.CreateInstance(__uuidof(Connection)); 
	    pRs.CreateInstance(__uuidof(Recordset)); 
		 
		pCon->ConnectionString="File Name=my_data3.udl"; 
		pCon->ConnectionTimeout=20; 
		pCon->Open("","","",adConnectUnspecified); 
        
        //_COM_SMARTPTR_TYPEDEF(IADORecordBinding, __uuidof(IADORecordBinding)); 
       	IADORecordBindingPtr picRs(pRs); 
        
         
		pRs->Open("SELECT * FROM baseInfo ORDER BY ID",  
		      pCon.GetInterfacePtr(), 
			adOpenStatic, adLockOptimistic, adCmdText); 
		 
		TESTHR(picRs->BindToRecordset(&m_baseInfo)); 
	} 
	catch (_com_error &e ) 
	{ 
		 
		CString errorMsg; 
		errorMsg.Format("Error:\n Code=%081x\n Meaning=%s\n Source=%s\n Description=%s\n", 
			             e.Error(),e.ErrorMessage(), 
						 (LPCSTR) e.Source(), 
						 (LPCSTR) e.Description()); 
		MessageBox(errorMsg); 
		return false; 
	} 
   
	return true; 
} 
 
void CSTUDlg::OnButtonNext()  
{ 
	// TODO: Add your control notification handler code here 
	m_btnPre.EnableWindow(TRUE); 
	pRs->MoveNext(); 
	if(!(pRs->EndOfFile)) 
	{ 
		m_editID=m_baseInfo.m_ID; 
		m_editName=m_baseInfo.m_name; 
		m_editDepart=m_baseInfo.m_department; 
        UpdateData(FALSE); 
	} 
     
	else if(pRs->EndOfFile) 
	{ pRs->MovePrevious(); 
	  m_btnNext.EnableWindow(FALSE); 
	} 
	     
	 
} 
 
void CSTUDlg::OnButtonPre()  
{ 
	// TODO: Add your control notification handler code here 
	m_btnNext.EnableWindow(TRUE); 
	 
	pRs->MovePrevious(); 
	if(!(pRs->BOF)) 
	{ 
		m_editID=m_baseInfo.m_ID; 
		m_editName=m_baseInfo.m_name; 
		m_editDepart=m_baseInfo.m_department; 
        UpdateData(FALSE); 
	} 
	else  
	{  
		pRs->MoveNext(); 
	    m_btnPre.EnableWindow(FALSE); 
	} 
 
} 
 
void CSTUDlg::OnButtonUpdate()  
{ 
	UpdateData(TRUE); 
	//把控件中的数据赋值给CADORecordBinding派生类 
	//baseInfo对象的各变量 
	strcpy(m_baseInfo.m_ID,m_editID); 
    strcpy(m_baseInfo.m_name,m_editName); 
	strcpy(m_baseInfo.m_department,m_editDepart); 
	//定义一个接口的智能指针实例 
	IADORecordBindingPtr picRs(pRs); 
	//调用接口的Update方法 
    if(FAILED(picRs->Update(&m_baseInfo))) 
	{ 
		MessageBox("修改记录失败!"); 
	} 
	else  
	{ 
		MessageBox("修改成功!"); 
	}	 
	m_btnUpdate.EnableWindow(FALSE); 
} 
 
void CSTUDlg::OnChangeEditId()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	m_btnUpdate.EnableWindow(TRUE); 
} 
 
void CSTUDlg::OnChangeEditName()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	m_btnUpdate.EnableWindow(TRUE); 
} 
 
void CSTUDlg::OnChangeEditDepart()  
{ 
	// TODO: If this is a RICHEDIT control, the control will not 
	// send this notification unless you override the CDialog::OnInitDialog() 
	// function and call CRichEditCtrl().SetEventMask() 
	// with the ENM_CHANGE flag ORed into the mask. 
	 
	// TODO: Add your control notification handler code here 
	m_btnUpdate.EnableWindow(TRUE); 
} 
 
void CSTUDlg::OnButtonAdd()  
{ 
	//清空所有EditBox控件 
	m_editID.Empty(); 
	m_editName.Empty(); 
	m_editDepart.Empty(); 
	UpdateData(FALSE); 
	//激活“确定添加”按钮 
	m_btnConfirm.EnableWindow(TRUE);	 
} 
 
void CSTUDlg::OnButtonConfirm()  
{ 
	UpdateData(TRUE); 
	if(m_editID.IsEmpty()||m_editID.GetLength()!=6) 
	{ 
		MessageBox("数据录入不正确!"); 
		return; 
	} 
	strcpy(m_baseInfo.m_ID,m_editID); 
    strcpy(m_baseInfo.m_name,m_editName); 
	strcpy(m_baseInfo.m_department,m_editDepart); 
	IADORecordBindingPtr picRs(pRs); 
	//调用接口的AddNew方法 
    if(FAILED(picRs->AddNew(&m_baseInfo))) 
	{ 
		MessageBox("添加记录失败!"); 
	} 
	else  
	{ 
		MessageBox("添加记录成功!"); 
	}	 
	m_btnConfirm.EnableWindow(FALSE); 
}