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

  1. // MySheet.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 "OdbcInfo.h"
  15. #include "MySheet.h"
  16. #include "catsets.h"
  17. #include "DrvInfo.h"
  18.  
  19. #include "aboutdlg.h"
  20.  
  21. #ifdef _DEBUG
  22. #define new DEBUG_NEW
  23. #undef THIS_FILE
  24. static char THIS_FILE[] = __FILE__;
  25. #endif
  26.  
  27. // stolen from afximpl.h...
  28. #define _countof(array) (sizeof(array)/sizeof(array[0]))
  29.  
  30. #define WM_NEW_DSN (WM_USER + 100)
  31. #define USC_DUMPTOFILE  0xEFFF      // user defined sys commands must be < 0xF000
  32.  
  33. /////////////////////////////////////////////////////////////////////////////
  34. // CMyPropertySheet
  35.  
  36. IMPLEMENT_DYNAMIC(CMyPropertySheet, CPropertySheet)
  37.  
  38. CMyPropertySheet::CMyPropertySheet(UINT nIDCaption, CWnd* pParentWnd, UINT iSelectPage)
  39.     :CPropertySheet(nIDCaption, pParentWnd, iSelectPage)
  40. {
  41. }
  42.  
  43. CMyPropertySheet::CMyPropertySheet(LPCTSTR pszCaption, CWnd* pParentWnd, UINT iSelectPage)
  44.     :CPropertySheet(pszCaption, pParentWnd, iSelectPage)
  45. {
  46. }
  47.  
  48. CMyPropertySheet::~CMyPropertySheet()
  49. {
  50.     delete m_pbtnDatabase;
  51.     delete m_pchkCursorLib;
  52.     delete m_pbtnAbout;
  53. }
  54.  
  55.  
  56. BEGIN_MESSAGE_MAP(CMyPropertySheet, CPropertySheet)
  57.     //{{AFX_MSG_MAP(CMyPropertySheet)
  58.     ON_WM_PAINT()
  59.     ON_WM_ERASEBKGND()
  60.     ON_WM_QUERYDRAGICON()
  61.     ON_WM_SYSCOMMAND()
  62.     ON_WM_INITMENUPOPUP()
  63.     //}}AFX_MSG_MAP
  64. END_MESSAGE_MAP()
  65.  
  66. /////////////////////////////////////////////////////////////////////////////
  67. // CMyPropertySheet message handlers
  68.  
  69. BOOL CMyPropertySheet::OnInitDialog()
  70. {
  71.  
  72.     // let MFC do it's thing...
  73.     BOOL bRet = CPropertySheet::OnInitDialog();
  74.  
  75.     // ...now we do ours.
  76.     int rgiButtons[] = { IDOK, IDCANCEL, ID_APPLY_NOW, IDHELP };
  77.  
  78.     // get the size of one button
  79.     CRect   rect;
  80.     GetDlgItem(rgiButtons[0])->GetWindowRect(&rect);
  81.     CFont* pFont = GetDlgItem(rgiButtons[0])->GetFont();
  82.  
  83.     // remove existing buttons
  84.     for (int i = 0; i < _countof(rgiButtons); i++)
  85.     {
  86.         HWND hWnd = ::GetDlgItem(m_hWnd, rgiButtons[i]);
  87.         if (hWnd != NULL)
  88.         {
  89.             ::ShowWindow(hWnd, SW_HIDE);
  90.             ::EnableWindow(hWnd, FALSE);
  91.         }
  92.     }
  93.  
  94.     // now make our own...
  95.  
  96.     // data source button
  97.     m_pbtnDatabase = new CButton;
  98.     ScreenToClient(&rect);
  99.     int nDiff = rect.right - rect.left;
  100.     rect.right = nDiff + 6;
  101.     rect.left = 6;
  102.     m_pbtnDatabase->Create(_T("&Data Source"),WS_CHILD | WS_VISIBLE |
  103.         BS_PUSHBUTTON | WS_TABSTOP,rect,this,1001);
  104.     m_pbtnDatabase->SetFont(pFont);
  105.  
  106.     // cursor library check box
  107.     rect.right += nDiff + 106;
  108.     rect.left += nDiff + 6;
  109.     m_pchkCursorLib = new CButton;
  110.     m_pchkCursorLib->Create(_T("&Use Cursor Library"),WS_CHILD | WS_VISIBLE |
  111.         BS_AUTOCHECKBOX | WS_TABSTOP,rect,this,1002);
  112.     m_pchkCursorLib->SetFont(pFont);
  113.     m_pchkCursorLib->SetCheck(1);
  114.  
  115.     // about button
  116.     m_pbtnAbout = new CButton;
  117.     CRect   clientRect;
  118.     GetClientRect(&clientRect);
  119.     rect.right = clientRect.right - 6;
  120.     rect.left = clientRect.right - 6 - nDiff;
  121.     m_pbtnAbout->Create(_T("&About"),WS_CHILD | WS_VISIBLE |
  122.         BS_PUSHBUTTON | WS_TABSTOP,rect,this,1003);
  123.     m_pbtnAbout->SetFont(pFont);
  124.  
  125.     // modify system menu & window styles so we can be
  126.     // minimized and restored
  127.     //
  128.     ModifyStyle(0,WS_MINIMIZEBOX | WS_SYSMENU);
  129.     GetSystemMenu(FALSE)->InsertMenu(1,MF_BYPOSITION,SC_MINIMIZE,
  130.         _T("Mi&nimize"));
  131.     GetSystemMenu(FALSE)->InsertMenu(0,MF_BYPOSITION,SC_RESTORE,
  132.         _T("&Restore"));
  133.     GetSystemMenu(FALSE)->InsertMenu(3,MF_BYPOSITION | MF_SEPARATOR);
  134.     GetSystemMenu(FALSE)->InsertMenu(3,MF_BYPOSITION,USC_DUMPTOFILE,
  135.         _T("&Dump to File"));
  136.     GetSystemMenu(FALSE)->InsertMenu(3,MF_BYPOSITION | MF_SEPARATOR);
  137.  
  138.     return bRet;
  139. }
  140.  
  141. BOOL CMyPropertySheet::OnCommand(WPARAM wParam, LPARAM lParam)
  142. {
  143.     if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == 1001)
  144.     {
  145.         if (((COdbcInfoApp*)AfxGetApp())->m_Database.IsOpen())
  146.             ((COdbcInfoApp*)AfxGetApp())->m_Database.Close();
  147.         if (m_bCursorLib = m_pchkCursorLib->GetCheck())
  148.             ((COdbcInfoApp*)AfxGetApp())->m_Database.Open(NULL);
  149.         else
  150.             ((COdbcInfoApp*)AfxGetApp())->m_Database.Open(
  151.                 NULL,FALSE,FALSE,"ODBC;",FALSE);
  152.         if (((COdbcInfoApp*)AfxGetApp())->m_Database.IsOpen())
  153.         {
  154.             int nPages = GetPageCount();
  155.             for (int i = 0; i < nPages; i++)
  156.                 ((CMyPage*)GetPage(i))->OnNewDSN();
  157.         }
  158.         return 0;
  159.     }
  160.     else if (HIWORD(wParam) == BN_CLICKED && LOWORD(wParam) == 1003)
  161.     {
  162.         CAboutDlg   dlg;
  163.         dlg.DoModal();
  164.         return 0;
  165.     }
  166.  
  167.     return CPropertySheet::OnCommand(wParam, lParam);
  168. }
  169.  
  170. // OnPaint, OnEraseBkgnd, and OnQueryDragIcon supplied so that we
  171. // appear properly when minimized on Windows NT (Not needed for Win95).
  172.  
  173. void CMyPropertySheet::OnPaint()
  174. {
  175.     CPaintDC dc(this); // device context for painting
  176.  
  177.     if (IsIconic())
  178.     {
  179.         DefWindowProc(WM_ICONERASEBKGND,(WORD)dc.m_hDC,0L);
  180.         CRect   rc;
  181.         GetClientRect(&rc);
  182.         rc.left = (rc.right - ::GetSystemMetrics(SM_CXICON)) >> 1;
  183.         rc.top = (rc.bottom - ::GetSystemMetrics(SM_CYICON)) >> 1;
  184.         HICON   hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  185.         dc.DrawIcon(rc.left,rc.top,hIcon);
  186.     }
  187.     // Do not call CPropertySheet::OnPaint() for painting messages
  188. }
  189.  
  190. BOOL CMyPropertySheet::OnEraseBkgnd(CDC* pDC)
  191. {
  192.     if (IsIconic())
  193.         return TRUE;
  194.     return CPropertySheet::OnEraseBkgnd(pDC);
  195. }
  196.  
  197. HCURSOR CMyPropertySheet::OnQueryDragIcon()
  198. {
  199.     return AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  200. }
  201.  
  202. void CMyPropertySheet::OnSysCommand(UINT nID, LPARAM lParam)
  203. {
  204.     // menu item will not be enabled if database is not opened
  205.     //
  206.     if (nID == USC_DUMPTOFILE)
  207.     {
  208.         // note: no fancy error handling here
  209.         //
  210.         CFileDialog dlg(TRUE);
  211.         if (dlg.DoModal() == IDOK)
  212.         {
  213.             CStdioFile  file;
  214.             if (file.Open(dlg.GetPathName(),
  215.                 CFile::typeText | CFile::modeCreate | CFile::modeWrite))
  216.             {
  217.                 if (m_bCursorLib)
  218.                     file.WriteString(_T("Cursor Library is Loaded\n\n"));
  219.                 else
  220.                     file.WriteString(_T("Cursor Library is Not Loaded\n\n"));
  221.                 int nPages = GetPageCount();
  222.                 for (int i = 0; i < nPages; i++)
  223.                     ((CMyPage*)GetPage(i))->DumpToFile(file);
  224.                 file.Close();
  225.             }
  226.             else
  227.                 AfxMessageBox(_T("Could not open output file"));
  228.         }
  229.         return;
  230.     }
  231.  
  232.     CPropertySheet::OnSysCommand(nID, lParam);
  233. }
  234.  
  235. void CMyPropertySheet::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
  236. {
  237.     CPropertySheet::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
  238.  
  239.     if (((COdbcInfoApp*)AfxGetApp())->m_Database.IsOpen())
  240.         GetSystemMenu(FALSE)->EnableMenuItem(4,MF_BYPOSITION | MF_ENABLED);
  241.     else
  242.         GetSystemMenu(FALSE)->EnableMenuItem(4,MF_BYPOSITION | MF_GRAYED);
  243.  
  244. }
  245.