home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / dbmsg / ado / employee / empdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-11-18  |  9.8 KB  |  475 lines

  1. // EmpDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Emp.h"
  6. #include "EmpDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. //#define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CAboutDlg dialog used for App About
  17.  
  18. class CAboutDlg : public CDialog
  19. {
  20. public:
  21.     CAboutDlg();
  22.  
  23. // Dialog Data
  24.     //{{AFX_DATA(CAboutDlg)
  25.     enum { IDD = IDD_ABOUTBOX };
  26.     //}}AFX_DATA
  27.  
  28.     // ClassWizard generated virtual function overrides
  29.     //{{AFX_VIRTUAL(CAboutDlg)
  30.     protected:
  31.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  32.     //}}AFX_VIRTUAL
  33.  
  34. // Implementation
  35. protected:
  36.     //{{AFX_MSG(CAboutDlg)
  37.     //}}AFX_MSG
  38.     DECLARE_MESSAGE_MAP()
  39. };
  40.  
  41. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  42. {
  43.     //{{AFX_DATA_INIT(CAboutDlg)
  44.     //}}AFX_DATA_INIT
  45. }
  46.  
  47. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  48. {
  49.     CDialog::DoDataExchange(pDX);
  50.     //{{AFX_DATA_MAP(CAboutDlg)
  51.     //}}AFX_DATA_MAP
  52. }
  53.  
  54. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  55.     //{{AFX_MSG_MAP(CAboutDlg)
  56.         // No message handlers
  57.     //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59.  
  60. /////////////////////////////////////////////////////////////////////////////
  61. // CEmpDlg dialog
  62.  
  63. CEmpDlg::CEmpDlg(CWnd* pParent /*=NULL*/)
  64.     : CDialog(CEmpDlg::IDD, pParent)
  65. {
  66.     //{{AFX_DATA_INIT(CEmpDlg)
  67.     m_nEmpNum = 0;
  68.     m_strFirstName = _T("");
  69.     m_strHomePhone = _T("");
  70.     m_strLastName = _T("");
  71.     m_strTitle = _T("");
  72.     //}}AFX_DATA_INIT
  73.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  74.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  75. }
  76.  
  77. CEmpDlg::~CEmpDlg()
  78. {    
  79.     if (m_pBiz != NULL)
  80.         delete m_pBiz;
  81. }
  82.  
  83. void CEmpDlg::DoDataExchange(CDataExchange* pDX)
  84. {
  85.     CDialog::DoDataExchange(pDX);
  86.     //{{AFX_DATA_MAP(CEmpDlg)
  87.     DDX_Text(pDX, IDC_EMPNUM, m_nEmpNum);
  88.     DDX_Text(pDX, IDC_FIRSTNAME, m_strFirstName);
  89.     DDX_Text(pDX, IDC_HOMEPHONE, m_strHomePhone);
  90.     DDX_Text(pDX, IDC_LASTNAME, m_strLastName);
  91.     DDX_Text(pDX, IDC_TITLE, m_strTitle);
  92.     //}}AFX_DATA_MAP
  93. }
  94.  
  95. BEGIN_MESSAGE_MAP(CEmpDlg, CDialog)
  96.     //{{AFX_MSG_MAP(CEmpDlg)
  97.     ON_WM_SYSCOMMAND()
  98.     ON_WM_PAINT()
  99.     ON_WM_QUERYDRAGICON()
  100.     ON_BN_CLICKED(ID_EDIT_ADD, OnEditAdd)
  101.     ON_BN_CLICKED(ID_EDIT_DELETE, OnEditDelete)
  102.     ON_BN_CLICKED(ID_EDIT_NEXT, OnEditNext)
  103.     ON_BN_CLICKED(ID_EDIT_PREVIOUS, OnEditPrevious)
  104.     ON_BN_CLICKED(ID_EDIT_FIRST, OnEditFirst)
  105.     ON_BN_CLICKED(ID_EDIT_LAST, OnEditLast)
  106.     ON_BN_CLICKED(ID_EDIT_FINDF, OnEditFindF)
  107.     ON_BN_CLICKED(ID_APP_OPEN_RECORD, OnAppOpenRecord)
  108.     //}}AFX_MSG_MAP
  109. END_MESSAGE_MAP()
  110.  
  111. /////////////////////////////////////////////////////////////////////////////
  112. // CEmpDlg message handlers
  113.  
  114. BOOL CEmpDlg::OnInitDialog()
  115. {
  116.     CDialog::OnInitDialog();
  117.  
  118.     // Add "About..." menu item to system menu.
  119.  
  120.     // IDM_ABOUTBOX must be in the system command range.
  121.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  122.     ASSERT(IDM_ABOUTBOX < 0xF000);
  123.  
  124.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  125.     CString strAboutMenu;
  126.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  127.     if (!strAboutMenu.IsEmpty())
  128.     {
  129.         pSysMenu->AppendMenu(MF_SEPARATOR);
  130.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  131.     }
  132.  
  133.     // Set the icon for this dialog.  The framework does this automatically
  134.     //  when the application's main window is not a dialog
  135.     SetIcon(m_hIcon, TRUE);            // Set big icon
  136.     SetIcon(m_hIcon, FALSE);        // Set small icon
  137.                                        
  138.     //Never enable the employee ID
  139.     (GetDlgItem(IDC_EMPNUM))->EnableWindow(FALSE);
  140.  
  141.     // TODO: Add extra initialization here
  142.     m_pBiz = new CEmpBiz;
  143.  
  144.     return TRUE;  // return TRUE  unless you set the focus to a control
  145. }
  146.  
  147. void CEmpDlg::OnSysCommand(UINT nID, LPARAM lParam)
  148. {
  149.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  150.     {
  151.         CAboutDlg dlgAbout;
  152.         dlgAbout.DoModal();
  153.     }
  154.     else
  155.     {
  156.         CDialog::OnSysCommand(nID, lParam);
  157.     }
  158. }
  159.  
  160. // If you add a minimize button to your dialog, you will need the code below
  161. //  to draw the icon.  For MFC applications using the document/view model,
  162. //  this is automatically done for you by the framework.
  163.  
  164. void CEmpDlg::OnPaint() 
  165. {
  166.     if (IsIconic())
  167.     {
  168.         CPaintDC dc(this); // device context for painting
  169.  
  170.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  171.  
  172.         // Center icon in client rectangle
  173.         int cxIcon = GetSystemMetrics(SM_CXICON);
  174.         int cyIcon = GetSystemMetrics(SM_CYICON);
  175.         CRect rect;
  176.         GetClientRect(&rect);
  177.         int x = (rect.Width() - cxIcon + 1) / 2;
  178.         int y = (rect.Height() - cyIcon + 1) / 2;
  179.  
  180.         // Draw the icon
  181.         dc.DrawIcon(x, y, m_hIcon);
  182.     }
  183.     else
  184.     {
  185.         CDialog::OnPaint();
  186.     }
  187.  
  188.     try 
  189.     {
  190.         m_nEmpNum =  m_pBiz->GetEmployeeId();
  191.         m_strFirstName = m_pBiz->GetFirstName();
  192.         m_strLastName = m_pBiz->GetLastName();
  193.         m_strHomePhone = m_pBiz->GetHomePhone();
  194.         m_strTitle = m_pBiz->GetTitle();
  195.     }
  196.     catch (HRESULT hr)
  197.     {
  198.         MessageBeep(0);
  199.         PopupErrorMessage(hr);
  200.         return ;
  201.     } 
  202.  
  203.     UpdateData(FALSE); //Invoke Data Exchange (copy member data to form controls)
  204.     return ;
  205. }
  206.  
  207. // The system calls this to obtain the cursor to display while the user drags
  208. //  the minimized window.
  209. HCURSOR CEmpDlg::OnQueryDragIcon()
  210. {
  211.     return (HCURSOR) m_hIcon;
  212. }
  213.  
  214.  
  215. void CEmpDlg::OnEditAdd() 
  216. {
  217.     // TODO: Add your control notification handler code here
  218.     try
  219.     {
  220.         if(!CommitAlteredEmpRec())
  221.             return;
  222.  
  223.         m_pBiz->AddRecord();
  224.  
  225.         OnPaint();
  226.     }
  227.     catch( HRESULT hr )
  228.     {
  229.         MessageBeep(0);
  230.         PopupErrorMessage(hr);
  231.     }
  232.     return ;
  233. }
  234.  
  235. void CEmpDlg::OnEditDelete() 
  236. {
  237.     // TODO: Add your control notification handler code here
  238.     try
  239.     {
  240.         //Delete method depends on current mode
  241.         m_pBiz->DeleteRecord();
  242.         OnPaint();
  243.     }
  244.     catch( HRESULT hr )
  245.     {
  246.         MessageBeep(0);
  247.         PopupErrorMessage(hr);
  248.     }
  249.     return ;
  250. }
  251.  
  252. void CEmpDlg::OnEditNext() 
  253. {
  254.     // TODO: Add your control notification handler code here
  255.     if(!CommitAlteredEmpRec()) 
  256.         return;
  257.  
  258.     try
  259.     {
  260.         if(m_pBiz->MoveNext())
  261.             OnPaint();
  262.         else
  263.             MessageBeep(0);
  264.     }
  265.     catch( HRESULT hr )
  266.     {
  267.         MessageBeep(0);
  268.         PopupErrorMessage(hr);
  269.     }
  270. }
  271.  
  272. void CEmpDlg::OnEditPrevious() 
  273. {
  274.     // TODO: Add your control notification handler code here
  275.     if(!CommitAlteredEmpRec())
  276.         return;
  277.  
  278.     try
  279.     {
  280.         if(m_pBiz->MovePrevious())
  281.             OnPaint();
  282.         else
  283.             MessageBeep(0);
  284.     }
  285.     catch( HRESULT hr )
  286.     {
  287.         MessageBeep(0);
  288.         PopupErrorMessage(hr);
  289.     }
  290. }
  291.  
  292.  
  293.  
  294. void CEmpDlg::OnEditFirst() 
  295. {
  296.     // TODO: Add your control notification handler code here
  297.     if(!CommitAlteredEmpRec())
  298.         return;
  299.  
  300.     try
  301.     {
  302.         if(m_pBiz->MoveFirst())
  303.             OnPaint();
  304.         else
  305.             MessageBeep(0);
  306.     }
  307.     catch( HRESULT hr )
  308.     {
  309.         PopupErrorMessage(hr);
  310.     }
  311. }
  312.  
  313. void CEmpDlg::OnEditLast() 
  314. {
  315.     // TODO: Add your control notification handler code here
  316.     if(!CommitAlteredEmpRec())
  317.         return;
  318.  
  319.     try
  320.     {
  321.         if(m_pBiz->MoveLast())
  322.             OnPaint();
  323.         else
  324.             MessageBeep(0);
  325.     }
  326.     catch( HRESULT hr )
  327.     {
  328.         PopupErrorMessage(hr);
  329.     }
  330. }
  331.  
  332. void CEmpDlg::OnEditFindF() 
  333. {
  334.     try
  335.     {
  336.         if(m_pBiz->FindForward(PrepareCriteria()))
  337.         {
  338.             OnPaint();
  339.         }
  340.         else
  341.         {    //not found.
  342.             MessageBeep(0);
  343.             //.. Should return to the last visited record.
  344.         }
  345.     }
  346.     catch( HRESULT hr )
  347.     {
  348.         MessageBeep(0);
  349.         PopupErrorMessage(hr);
  350.     }
  351. }
  352.  
  353.  
  354. BOOL CEmpDlg::CommitAlteredEmpRec()
  355. {
  356.     CString            strOldFirstName, strOldHomePhone, strOldLastName, strOldTitle;
  357.     COleVariant        vFirstName, vHomePhone, vLastName, vTitle, vID;
  358.  
  359.     if (!m_pBiz->m_fConnected)
  360.         return FALSE;
  361.  
  362.     //Get current record string values
  363.     try
  364.     {
  365.         strOldFirstName = m_pBiz->GetFirstName();
  366.         strOldLastName = m_pBiz->GetLastName();
  367.         strOldHomePhone = m_pBiz->GetHomePhone();
  368.         strOldTitle = m_pBiz->GetTitle();
  369.  
  370.         //Force DDX to update member values
  371.         if(!UpdateData())
  372.             return FALSE;
  373.  
  374.         // Did any fields change value?
  375.         if(
  376.         (m_strFirstName    == strOldFirstName    ) &&
  377.         (m_strHomePhone    == strOldHomePhone    ) &&
  378.         (m_strLastName    == strOldLastName    ) &&
  379.         (m_strTitle        == strOldTitle        ) && !m_pBiz->IsAddMode() )
  380.             return TRUE;
  381.  
  382.         //Save it, dump it or stay on it?
  383.         switch (AfxMessageBox(IDS_PROMPT_COMMIT_EMPREC, MB_YESNOCANCEL))
  384.         {
  385.                 case IDYES:
  386.                 {
  387.                         m_pBiz->UpdateEmpRec(m_strFirstName,
  388.                                             m_strHomePhone,
  389.                                             m_strLastName,
  390.                                             m_strTitle);
  391.  
  392.                     return TRUE;
  393.                 }
  394.  
  395.                 case IDNO:
  396.                 case IDCANCEL:
  397.                 {
  398.                     if (m_pBiz->IsAddMode())
  399.                         m_pBiz->CancelPendingAdd();
  400.                     return TRUE;
  401.                 }
  402.  
  403.                 default:
  404.                 {
  405.                     GetDlgItem(IDC_EMPNUM)->SetFocus();
  406.                     return TRUE;
  407.                 }
  408.         }
  409.     }
  410.     catch (CMemoryException &memx)
  411.     {
  412.         TCHAR szBuf[256];
  413.         memx.GetErrorMessage(szBuf, sizeof(szBuf), NULL);
  414.         AfxMessageBox(szBuf);
  415.         return FALSE;
  416.     }
  417.     catch(HRESULT hr)
  418.     {
  419.         PopupErrorMessage(hr);
  420.         return FALSE;
  421.     }
  422.  
  423.     return TRUE;
  424. }
  425.  
  426.   
  427.                              
  428.  
  429. CString CEmpDlg::PrepareCriteria()
  430. {
  431.     CString            strCriteria = "";
  432.  
  433.     //Force DDX to update member values
  434.     if(!UpdateData())
  435.         return strCriteria;
  436.  
  437.     if(m_strFirstName != "")
  438.         strCriteria = "FirstName = '" + m_strFirstName +"'";
  439.     if(m_strLastName != "")
  440.         strCriteria = strCriteria + ( strCriteria == "" ? "":" AND ") + "LastName = '" + m_strLastName +"'";
  441.     if(m_strHomePhone != "")
  442.         strCriteria = strCriteria + ( strCriteria == "" ? "":" AND ") + "HomePhone = '" + m_strHomePhone +"'";
  443.     if(m_strTitle != "")
  444.         strCriteria = strCriteria + ( strCriteria == "" ? "":" AND ") + "Title = '" + m_strTitle +"'";
  445.  
  446.     return strCriteria;
  447. }
  448.  
  449.  
  450.  
  451.  
  452. void CEmpDlg::OnAppOpenRecord() 
  453. {
  454.     // TODO: Add your control notification handler code here
  455.  
  456.     //Connect to the EMPLOYEE database
  457.     if(!m_pBiz->ConnectToDatabase())
  458.     {
  459.         m_pBiz->m_fConnected = FALSE;
  460.         return;
  461.     }
  462.  
  463.     m_pBiz->m_fConnected = TRUE;
  464.  
  465.     GetDlgItem(ID_EDIT_PREVIOUS)->EnableWindow(TRUE);
  466.     GetDlgItem(ID_EDIT_NEXT)->EnableWindow(TRUE);
  467.     GetDlgItem(ID_EDIT_DELETE)->EnableWindow(TRUE);
  468.     GetDlgItem(ID_EDIT_ADD)->EnableWindow(TRUE);
  469.     GetDlgItem(ID_EDIT_LAST)->EnableWindow(TRUE);
  470.     GetDlgItem(ID_EDIT_FIRST)->EnableWindow(TRUE);
  471.     GetDlgItem(ID_EDIT_FINDF)->EnableWindow(TRUE);
  472.     
  473.     OnPaint();
  474. }
  475.