home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c11 / lab01 / ex01 / browseview.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.2 KB  |  178 lines

  1. // browseView.cpp : implementation of the CBrowseView class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "afxdisp.h"
  6. #include "browse.h"
  7.  
  8. #include "browseDoc.h"
  9. #include "browseView.h"
  10. #include "MainFrm.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CBrowseView
  20.  
  21. IMPLEMENT_DYNCREATE(CBrowseView, CView)
  22.  
  23. BEGIN_MESSAGE_MAP(CBrowseView, CView)
  24.     //{{AFX_MSG_MAP(CBrowseView)
  25.     ON_WM_SHOWWINDOW()
  26.     ON_WM_SIZE()
  27.     ON_WM_CREATE()
  28.     //}}AFX_MSG_MAP
  29.     // Standard printing commands
  30.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  31.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  32.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  33. END_MESSAGE_MAP()
  34.  
  35. BEGIN_EVENTSINK_MAP(CBrowseView, CView)
  36.     ON_EVENT(CBrowseView, IDC_WBC, 0x71, OnTitleChange, VTS_BSTR)
  37.     ON_EVENT(CBrowseView, IDC_WBC, 0x66, OnStatusTextChange, VTS_BSTR)
  38. END_EVENTSINK_MAP()
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CBrowseView construction/destruction
  41.  
  42. CBrowseView::CBrowseView()
  43. {
  44.     // TODO: add construction code here
  45.  
  46.     m_pBrowse= NULL;
  47.  
  48. }
  49.  
  50. CBrowseView::~CBrowseView()
  51. {
  52. }
  53.  
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CBrowseView drawing
  57.  
  58. void CBrowseView::OnDraw(CDC* pDC)
  59. {
  60.     CBrowseDoc* pDoc = GetDocument();
  61.     ASSERT_VALID(pDoc);
  62.  
  63.  
  64.     // TODO: Add your specialized code here and/or call the base class
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CBrowseView printing
  69.  
  70. BOOL CBrowseView::OnPreparePrinting(CPrintInfo* pInfo)
  71. {
  72.     // default preparation
  73.     return DoPreparePrinting(pInfo);
  74. }
  75.  
  76. void CBrowseView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  77. {
  78.     // TODO: add extra initialization before printing
  79. }
  80.  
  81. void CBrowseView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  82. {
  83.     // TODO: add cleanup after printing
  84. }
  85.  
  86. /////////////////////////////////////////////////////////////////////////////
  87. // CBrowseView diagnostics
  88.  
  89. #ifdef _DEBUG
  90. void CBrowseView::AssertValid() const
  91. {
  92.     CView::AssertValid();
  93. }
  94.  
  95. void CBrowseView::Dump(CDumpContext& dc) const
  96. {
  97.     CView::Dump(dc);
  98. }
  99.  
  100. CBrowseDoc* CBrowseView::GetDocument() // non-debug version is inline
  101. {
  102.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CBrowseDoc)));
  103.     return (CBrowseDoc*)m_pDocument;
  104. }
  105. #endif //_DEBUG
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CBrowseView message handlers
  109.  
  110. void CBrowseView::OnShowWindow(BOOL bShow, UINT nStatus) 
  111. {
  112.     CView::OnShowWindow(bShow, nStatus);
  113.     
  114.     // TODO: Add your message handler code here
  115.  
  116.     
  117. }
  118.  
  119.  
  120. void CBrowseView::OnSize(UINT nType, int cx, int cy) 
  121. {
  122.     CView::OnSize(nType, cx, cy);
  123.     
  124.     // TODO: Add your message handler code here
  125.  
  126.     if (m_pBrowse) {
  127.         m_pBrowse->SetWidth(cx);
  128.         m_pBrowse->SetHeight(cy);
  129.     }
  130.     
  131. }
  132.  
  133. int CBrowseView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  134. {
  135.     if (CView::OnCreate(lpCreateStruct) == -1)
  136.         return -1;
  137.     
  138.     // TODO: Add your specialized creation code here
  139.  
  140.     // Define the area where the control will reside.
  141.     CRect rect;
  142.     GetClientRect(&rect);
  143.     // 
  144.     // Create the control. 
  145.     // IDC_WBC is a unique identifier for the control and was defined
  146.     // using a dummy menu item.
  147.     m_pBrowse= new CWebBrowser;
  148.     ASSERT(m_pBrowse);
  149.     if (!m_pBrowse->Create(NULL,NULL, WS_VISIBLE,rect,this, IDC_WBC)) {
  150.         TRACE("failed to create browser\n");
  151.         return 0;
  152.     }
  153.     // Initialize the first URL.
  154.     COleVariant noArg;
  155.     m_pBrowse->Navigate("www.microsoft.com",&noArg,&noArg,&noArg,&noArg);
  156.     
  157.     return 0;
  158. }
  159.  
  160. // Adding an event handler.
  161. void CBrowseView::OnTitleChange(LPCTSTR Text)
  162. {
  163.     CString strTitle;
  164.     strTitle.Format("MyBrowser - %s", Text);
  165.     AfxGetMainWnd()->SetWindowText(strTitle);
  166. }
  167.  
  168.  
  169. // Need to indirectly call mainframe since the control is
  170. // a child of this view class, ie events only come to this class.
  171. void CBrowseView::OnStatusTextChange(LPCTSTR text)
  172. {
  173.     // Only write to status line if there is a non-zero string.
  174.     // (lstrlen work on either ANSI or UNICODE strings.)
  175.     if (lstrlen(text))
  176.         ((CMainFrame*)AfxGetMainWnd())->Status(text);
  177. }
  178.