home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / database / dbfetch / fetchdlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  7KB  |  270 lines

  1. // fetchDlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "dbfetch.h"
  15. #include "fetchDlg.h"
  16. #include "datadlg.h"
  17.  
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CAboutDlg dialog used for App About
  26.  
  27. class CAboutDlg : public CDialog
  28. {
  29. public:
  30.     CAboutDlg();
  31.  
  32. // Dialog Data
  33.     //{{AFX_DATA(CAboutDlg)
  34.     enum { IDD = IDD_ABOUTBOX };
  35.     //}}AFX_DATA
  36.  
  37.     // ClassWizard generated virtual function overrides
  38.     //{{AFX_VIRTUAL(CAboutDlg)
  39.     protected:
  40.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  41.     //}}AFX_VIRTUAL
  42.  
  43. // Implementation
  44. protected:
  45.     //{{AFX_MSG(CAboutDlg)
  46.     //}}AFX_MSG
  47.     DECLARE_MESSAGE_MAP()
  48. };
  49.  
  50. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  51. {
  52.     //{{AFX_DATA_INIT(CAboutDlg)
  53.     //}}AFX_DATA_INIT
  54. }
  55.  
  56. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  57. {
  58.     CDialog::DoDataExchange(pDX);
  59.     //{{AFX_DATA_MAP(CAboutDlg)
  60.     //}}AFX_DATA_MAP
  61. }
  62.  
  63. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  64.     //{{AFX_MSG_MAP(CAboutDlg)
  65.         // No message handlers
  66.     //}}AFX_MSG_MAP
  67. END_MESSAGE_MAP()
  68.  
  69. /////////////////////////////////////////////////////////////////////////////
  70. // CDbfetchDlg dialog
  71.  
  72. CDbfetchDlg::CDbfetchDlg(CWnd* pParent /*=NULL*/)
  73.     : CDialog(CDbfetchDlg::IDD, pParent)
  74. {
  75.     //{{AFX_DATA_INIT(CDbfetchDlg)
  76.     //}}AFX_DATA_INIT
  77.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  78.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  79. }
  80.  
  81. void CDbfetchDlg::DoDataExchange(CDataExchange* pDX)
  82. {
  83.     CDialog::DoDataExchange(pDX);
  84.     //{{AFX_DATA_MAP(CDbfetchDlg)
  85.     DDX_Control(pDX, IDC_TABLE_COMBO, m_cbTable);
  86.     DDX_Control(pDX, IDC_GETDATASOURCE, m_buttonGetDataSource);
  87.     DDX_Control(pDX, ID_QUIT_SELECT, m_buttonQuit);
  88.     DDX_Control(pDX, ID_GETDATA, m_buttonGetData);
  89.     //}}AFX_DATA_MAP
  90. }
  91.  
  92. BEGIN_MESSAGE_MAP(CDbfetchDlg, CDialog)
  93.     //{{AFX_MSG_MAP(CDbfetchDlg)
  94.     ON_WM_SYSCOMMAND()
  95.     ON_WM_PAINT()
  96.     ON_WM_QUERYDRAGICON()
  97.     ON_BN_CLICKED(IDOK, OnOk)
  98.     ON_BN_CLICKED(IDC_GETDATASOURCE, OnGetdatasource)
  99.     ON_BN_CLICKED(ID_QUIT_SELECT, OnQuitSelect)
  100.     ON_BN_CLICKED(ID_GETDATA, OnGetData)
  101.     //}}AFX_MSG_MAP
  102. END_MESSAGE_MAP()
  103.  
  104. /////////////////////////////////////////////////////////////////////////////
  105. // CDbfetchDlg message handlers
  106.  
  107. BOOL CDbfetchDlg::OnInitDialog()
  108. {
  109.     CDialog::OnInitDialog();
  110.  
  111.     // Add "About..." menu item to system menu.
  112.  
  113.     // IDM_ABOUTBOX must be in the system command range.
  114.     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  115.     ASSERT(IDM_ABOUTBOX < 0xF000);
  116.  
  117.     CMenu* pSysMenu = GetSystemMenu(FALSE);
  118.     CString strAboutMenu;
  119.     strAboutMenu.LoadString(IDS_ABOUTBOX);
  120.     if (!strAboutMenu.IsEmpty())
  121.     {
  122.         pSysMenu->AppendMenu(MF_SEPARATOR);
  123.         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  124.     }
  125.  
  126.     // Set the icon for this dialog.  The framework does this automatically
  127.     // when the application's main window is not a dialog
  128.     SetIcon(m_hIcon, TRUE);         // Set big icon
  129.     SetIcon(m_hIcon, FALSE);        // Set small icon
  130.  
  131.     GetDlgItem(IDC_TABLE_COMBO)->EnableWindow(FALSE);
  132.     GetDlgItem(ID_GETDATA)->EnableWindow(FALSE);
  133.  
  134.     return TRUE;  // return TRUE  unless you set the focus to a control
  135. }
  136.  
  137. void CDbfetchDlg::OnSysCommand(UINT nID, LPARAM lParam)
  138. {
  139.     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  140.     {
  141.         CAboutDlg dlgAbout;
  142.         dlgAbout.DoModal();
  143.     }
  144.     else
  145.     {
  146.         CDialog::OnSysCommand(nID, lParam);
  147.     }
  148. }
  149.  
  150. // If you add a minimize button to your dialog, you will need the code below
  151. // to draw the icon.  For MFC applications using the document/view model,
  152. // this is automatically done for you by the framework.
  153.  
  154. void CDbfetchDlg::OnPaint()
  155. {
  156.     if (IsIconic())
  157.     {
  158.         CPaintDC dc(this); // device context for painting
  159.  
  160.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  161.  
  162.         // Center icon in client rectangle
  163.         int cxIcon = GetSystemMetrics(SM_CXICON);
  164.         int cyIcon = GetSystemMetrics(SM_CYICON);
  165.         CRect rect;
  166.         GetClientRect(&rect);
  167.         int x = (rect.Width() - cxIcon + 1) / 2;
  168.         int y = (rect.Height() - cyIcon + 1) / 2;
  169.  
  170.         // Draw the icon
  171.         dc.DrawIcon(x, y, m_hIcon);
  172.     }
  173.     else
  174.     {
  175.         CDialog::OnPaint();
  176.     }
  177. }
  178.  
  179. // The system calls this to obtain the cursor to display while the user drags
  180. // the minimized window.
  181. HCURSOR CDbfetchDlg::OnQueryDragIcon()
  182. {
  183.     return (HCURSOR) m_hIcon;
  184. }
  185.  
  186. void CDbfetchDlg::OnOk()
  187. {
  188.  
  189. }
  190.  
  191. void CDbfetchDlg::OnCancel()
  192. {
  193.  
  194.     CDialog::OnCancel();
  195. }
  196.  
  197. void CDbfetchDlg::OnGetdatasource()
  198. {
  199.     // Make sure the list box is empty
  200.     m_cbTable.ResetContent();
  201.  
  202.     // If already open, close it and reselect
  203.     if (m_db.IsOpen())
  204.         m_db.Close();
  205.     if (!m_db.OpenEx(NULL, CDatabase::forceOdbcDialog))
  206.     {
  207.         GetDlgItem(IDC_TABLE_COMBO)->EnableWindow(FALSE);
  208.         GetDlgItem(ID_GETDATA)->EnableWindow(FALSE);
  209.         return;
  210.     }
  211.  
  212.     // Get a list of the tables
  213.     CTables rs(&m_db);
  214.     rs.Open(NULL, NULL, NULL, "TABLE");
  215.  
  216.     // Cycle through all the tables
  217.     CString strTableRef;
  218.     while (!rs.IsEOF())
  219.     {
  220.         // Build the string from owner name and table name
  221.         strTableRef = _T("[");
  222.         if (!rs.m_strTableOwner.IsEmpty())
  223.             strTableRef += rs.m_strTableOwner + _T("].[");
  224.         strTableRef += rs.m_strTableName + _T("]");
  225.  
  226.         m_cbTable.AddString(strTableRef);
  227.         rs.MoveNext();
  228.     }
  229.  
  230.     rs.Close();
  231.  
  232.     // Make sure the controls are enabled
  233.     GetDlgItem(IDC_TABLE_COMBO)->EnableWindow(TRUE);
  234.     GetDlgItem(ID_GETDATA)->EnableWindow(TRUE);
  235. }
  236.  
  237. void CDbfetchDlg::OnQuitSelect()
  238. {
  239.     OnCancel();
  240. }
  241.  
  242. void CDbfetchDlg::OnGetData()
  243. {
  244.     CDynamicBulkSet rs(&m_db);
  245.     CDataDialog dlgData;
  246.  
  247.     // Get the current table selected and validate
  248.     int nCurSel = m_cbTable.GetCurSel();
  249.     if (nCurSel == CB_ERR)
  250.     {
  251.         CString strError;
  252.         strError.LoadString(IDS_ERROR_NOTABLE);
  253.         AfxMessageBox(strError);
  254.         return;
  255.     }
  256.  
  257.     CString strSQL;
  258.     m_cbTable.GetLBText(nCurSel, strSQL);
  259.     strSQL = _T("SELECT * FROM ") + strSQL;
  260.  
  261.     // Open the recordset, create the dialog and hand it the recordset
  262.     rs.Open(CRecordset::snapshot, strSQL,
  263.         CRecordset::readOnly | CRecordset::useMultiRowFetch);
  264.     dlgData.SetRecordset(&rs);
  265.     dlgData.DoModal();
  266.  
  267.     // Make sure the CDynamicBulkSet is cleaned up properly
  268.     rs.Close();
  269. }
  270.