DBDialog.h
上传用户:benben_wyd
上传日期:2010-02-26
资源大小:1229k
文件大小:5k
- // DBDialog.h : Declaration of the CDBDialog
- #ifndef __DBDIALOG_H_
- #define __DBDIALOG_H_
- #include "resource.h" // main symbols
- //Added by Chuck Wood for OLE DB support
- #include "Department.h"
- #include <atlhost.h>
- /////////////////////////////////////////////////////////////////////////////
- // CDBDialog
- class CDBDialog :
- public CAxDialogImpl<CDBDialog>
- {
- /*
- Protected variables and function prototypes added by
- Chuck Wood for Visual C++ Database Developer's Guide
- */
- protected:
- CDepartment* m_pSet;
- BOOL m_bAddingRecord;
- enum { FIRST = 0,
- LAST = 1,
- NEXT = 2,
- PREV = 3 };
- void DisplayError(char *strMessage, HRESULT hr = S_OK, char *strFunction = NULL);
- void DisplayStatus(char *strMessage);
- HRESULT SaveDepartment();
- void OnMove(int position);
- void AddRecord();
- void ResetRowSet();
- void UpdateData(BOOL bSaveChangesToSet = TRUE);
- public:
- CDBDialog()
- {
- //Added by Chuck Wood to open & initialize the dialog box
- m_pSet = new CDepartment();
- m_bAddingRecord = FALSE;
- DoModal();
- }
- ~CDBDialog()
- {
- delete m_pSet;
- }
- enum { IDD = IDD_DBDIALOG };
- BEGIN_MSG_MAP(CDBDialog)
- MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
- COMMAND_ID_HANDLER(IDCANCEL, OnCancel)
- COMMAND_HANDLER(IDC_ADD, BN_CLICKED, OnClickedAdd)
- COMMAND_HANDLER(IDC_DELETE, BN_CLICKED, OnClickedDelete)
- COMMAND_HANDLER(IDC_FINDEPARTMENT, BN_CLICKED, OnClickedFindepartment)
- COMMAND_HANDLER(IDC_SAVE, BN_CLICKED, OnClickedSave)
- COMMAND_HANDLER(IDC_FIRST, BN_CLICKED, OnClickedFirst)
- COMMAND_HANDLER(IDC_LAST, BN_CLICKED, OnClickedLast)
- COMMAND_HANDLER(IDC_NEXT, BN_CLICKED, OnClickedNext)
- COMMAND_HANDLER(IDC_PREV, BN_CLICKED, OnClickedPrev)
- END_MSG_MAP()
- // Handler prototypes:
- // LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled);
- // LRESULT CommandHandler(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled);
- // LRESULT NotifyHandler(int idCtrl, LPNMHDR pnmh, BOOL& bHandled);
- LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
- {
- _Module.Lock();
- SendMessage(GetDlgItem(IDC_DEPARTMENTCODE),
- EM_LIMITTEXT, (WPARAM)4, 0);
- SendMessage(GetDlgItem(IDC_DEPARTMENTNAME),
- EM_LIMITTEXT, (WPARAM)50, 0);
- SendMessage(GetDlgItem(IDC_FINDDEPARTMENTEDIT),
- EM_LIMITTEXT, (WPARAM)50, 0);
- HRESULT hr = m_pSet->Open();
- if (FAILED(hr)) {
- DisplayError("Open Rowset failed", hr, "OnInitDialog");
- exit(1); //Better not start
- }
- m_pSet->MoveFirst();
- UpdateData(FALSE); //Write Values to Window
- return 1; // Let the system set the focus
- }
- LRESULT OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- if (SUCCEEDED(SaveDepartment())){ //Save the department if needed
- EndDialog(wID); //Close the dialog box
- _Module.Unlock(); //End this program
- }
- return 0;
- }
- LRESULT OnClickedAdd(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- if (FAILED(SaveDepartment())) {
- return 0;
- }
- AddRecord();
- return 0;
- }
- LRESULT OnClickedDelete(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- if (MessageBox( //Be sure to verify your deletes
- "Are you sure you want to delete?", "",
- MB_YESNO)
- != IDYES) {
- return 0;
- }
- //Delete record and test
- if (m_bAddingRecord) {
- //Just abort the add
- m_bAddingRecord = FALSE;
- }
- else {
- //Delete the current row in the rowset
- HRESULT hr = m_pSet->Delete();
- if (FAILED(hr)) { //Test Delete
- DisplayError("Delete Failed", hr, "OnClickedDelete");
- }
- }
- ResetRowSet();
- return 0;
- }
- LRESULT OnClickedFindepartment(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- char filterDepartment[5];
- if (FAILED(SaveDepartment())) {
- return 0;
- }
- GetDlgItemText(IDC_FINDDEPARTMENTEDIT,
- filterDepartment, 5);
- delete m_pSet->m_strFilter;
- m_pSet->m_strFilter = new char[25];
- strcpy (m_pSet->m_strFilter, "DepartmentCode = '");
- strcat (m_pSet->m_strFilter, filterDepartment);
- strcat (m_pSet->m_strFilter, "'");
- //Requery for new filter
- m_pSet->Close();
- m_pSet->OpenRowset();
- m_pSet->MoveFirst();
- UpdateData(FALSE); //Update the window
- return 0;
- }
- LRESULT OnClickedSave(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- return 0;
- }
- LRESULT OnClickedFirst(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- OnMove(FIRST);
- return 0;
- }
- LRESULT OnClickedLast(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- OnMove(LAST);
- return 0;
- }
- LRESULT OnClickedNext(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- OnMove(NEXT);
- return 0;
- }
- LRESULT OnClickedPrev(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
- {
- OnMove(PREV);
- return 0;
- }
- };
- #endif //__DBDIALOG_H_