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

  1. // GridView.cpp : implementation of the CGridView class
  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 "MDIBind.h"
  15.  
  16. #include "MDIDoc.h"
  17. #include "GridView.h"
  18.  
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // CGridView
  27.  
  28. IMPLEMENT_DYNCREATE(CGridView, CView)
  29.  
  30. BEGIN_MESSAGE_MAP(CGridView, CView)
  31.     //{{AFX_MSG_MAP(CGridView)
  32.     ON_WM_CREATE()
  33.     ON_WM_SIZE()
  34.     //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36.  
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CGridView construction/destruction
  39.  
  40. CGridView::CGridView()
  41. {
  42.     m_pGridCtl = new CMsDgridCtrl;
  43.  
  44. }
  45.  
  46. CGridView::~CGridView()
  47. {
  48.     delete m_pGridCtl;
  49. }
  50.  
  51. BOOL CGridView::PreCreateWindow(CREATESTRUCT& cs)
  52. {
  53.     // TODO: Modify the Window class or styles here by modifying
  54.     // the CREATESTRUCT cs
  55.  
  56.     return CView::PreCreateWindow(cs);
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // CGridView drawing
  61.  
  62. void CGridView::OnDraw(CDC* pDC)
  63. {
  64.     CMDIBindDoc* pDoc = GetDocument();
  65.     ASSERT_VALID(pDoc);
  66.  
  67.     // TODO: add draw code for native data here
  68. }
  69.  
  70. /////////////////////////////////////////////////////////////////////////////
  71. // CGridView diagnostics
  72.  
  73. #ifdef _DEBUG
  74. void CGridView::AssertValid() const
  75. {
  76.     CView::AssertValid();
  77. }
  78.  
  79. void CGridView::Dump(CDumpContext& dc) const
  80. {
  81.     CView::Dump(dc);
  82. }
  83.  
  84. CMDIBindDoc* CGridView::GetDocument() // non-debug version is inline
  85. {
  86.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMDIBindDoc)));
  87.     return (CMDIBindDoc*) m_pDocument;
  88. }
  89. #endif //_DEBUG
  90.  
  91. /////////////////////////////////////////////////////////////////////////////
  92. // CGridView message handlers
  93.  
  94. int CGridView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  95. {
  96.     if (CView::OnCreate(lpCreateStruct) == -1)
  97.         return -1;
  98.  
  99.     RECT r=  { 2,2,400,300 };
  100.  
  101.     m_pGridCtl->Create(NULL,_T(""),
  102.                        WS_VISIBLE | WS_CHILD, r, this, 1);
  103.  
  104.     return 0;
  105. }
  106.  
  107. void CGridView::OnSize(UINT nType, int cx, int cy)
  108. {
  109.     CView::OnSize(nType, cx, cy);
  110.  
  111.     if (::IsWindow(m_pGridCtl->m_hWnd) )
  112.         m_pGridCtl->MoveWindow(2,2,cx-4,cy-4,FALSE); // do not repaint
  113.  
  114.  
  115. }
  116.  
  117. // binds the grid control to the RDC control
  118. // sets the Grid Caption to the SQL string from RDC control
  119. void CGridView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint )
  120. {
  121.     CMDIBindDoc* pDoc=GetDocument();
  122.     if(pDoc->m_pRDC!=NULL)
  123.     {   // bind the control
  124.         LPUNKNOWN pCursor=pDoc->m_pRDC->GetDSCCursor();
  125.         ASSERT(pCursor!=NULL);
  126.         // bind the grid to the actual RDC and RDC to the Grid
  127.         m_pGridCtl->SetDataSource(pCursor);
  128.         m_pGridCtl->BindProperty(0x9,pDoc->m_pRDC);
  129.         // display the SQL caption
  130.         m_pGridCtl->SetCaption(pDoc->m_pRDC->GetSql());
  131.     }
  132.     else // unbind
  133.     {
  134.         m_pGridCtl->SetDataSource(NULL);
  135.         m_pGridCtl->BindProperty(0x9,NULL);
  136.         m_pGridCtl->SetCaption(NULL);
  137.     }
  138. }
  139.