home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / toolbar / mainfrm.cpp next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.0 KB  |  206 lines

  1. // MainFrm.cpp : implementation of the CMainFrame class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Toolbar.h"
  6.  
  7. #include "MainFrm.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CMainFrame
  17.  
  18. IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
  19.  
  20. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
  21.     ON_WM_CLOSE()
  22.     //{{AFX_MSG_MAP(CMainFrame)
  23.     ON_WM_CREATE()
  24.     ON_COMMAND(ID_VIEW_COLORTOOLBAR, OnViewColortoolbar)
  25.     ON_UPDATE_COMMAND_UI(ID_VIEW_COLORTOOLBAR, OnUpdateViewColortoolbar)
  26.     ON_WM_DESTROY()
  27.     //}}AFX_MSG_MAP
  28. END_MESSAGE_MAP()
  29.  
  30. /////////////////////////////////////////////////////////////////////////////
  31. // CMainFrame construction/destruction
  32.  
  33. CMainFrame::CMainFrame()
  34.     :m_pColorToolbar(0)
  35. {
  36. }
  37.  
  38. CMainFrame::~CMainFrame()
  39. {
  40. }
  41.  
  42. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  43. {
  44.     WINDOWPLACEMENT wp;
  45.     if (LoadWindowPlacement (&wp))
  46.     {
  47.         wp.showCmd = AfxGetApp()->m_nCmdShow;
  48.         SetWindowPlacement (&wp);
  49.     }
  50.     if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
  51.         return -1;
  52.     
  53.     if (!m_wndToolBar.Create(this) ||
  54.         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
  55.     {
  56.         TRACE0("Failed to create toolbar\n");
  57.         return -1;      // fail to create
  58.     }
  59.  
  60.     // TODO: Remove this if you don't want tool tips
  61.     // or a resizeable toolbar
  62.     m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
  63.         CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
  64.  
  65.     // TODO: Delete these three lines if you don't 
  66.     // want the toolbar to be dockable
  67.     m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
  68.     EnableDocking(CBRS_ALIGN_ANY);
  69.     DockControlBar(&m_wndToolBar);
  70.  
  71.     return 0;
  72. }
  73.  
  74. BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
  75. {
  76.     return CFrameWnd::PreCreateWindow(cs);
  77. }
  78.  
  79. /////////////////////////////////////////////////////////////////////////////
  80. // CMainFrame diagnostics
  81.  
  82. #ifdef _DEBUG
  83. void CMainFrame::AssertValid() const
  84. {
  85.     CFrameWnd::AssertValid();
  86. }
  87.  
  88. void CMainFrame::Dump(CDumpContext& dc) const
  89. {
  90.     CFrameWnd::Dump(dc);
  91. }
  92.  
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CMainFrame message handlers
  97.  
  98. void CMainFrame::OnViewColortoolbar() 
  99. {
  100.     // The very first time, the toolbar needs to be created.
  101.     // m_pColorToolbar is a pointer that is a member of
  102.     // CMainFrame. See OnDestroy for the code that deletes
  103.     // the object allocated here.
  104.     if (0 == m_pColorToolbar)
  105.     {
  106.         m_pColorToolbar = new CToolBar;
  107.         CString ErrMsg;
  108.         if (0 == m_pColorToolbar->Create(this))
  109.         {
  110.             ErrMsg.LoadString(IDS_COLORTB_CREATE);
  111.             ::AfxMessageBox(ErrMsg);
  112.             return;
  113.         }
  114.         if (0 == m_pColorToolbar->LoadToolBar(IDR_COLOR_TOOLBAR))
  115.         {
  116.             ErrMsg.LoadString(IDS_COLORTB_LOAD);
  117.             ::AfxMessageBox(ErrMsg);
  118.             return;
  119.         }
  120.         m_pColorToolbar->EnableDocking(CBRS_ALIGN_ANY);
  121.         
  122.         DockControlBar(m_pColorToolbar);
  123.     }
  124.     else
  125.         // If the window is visible, hide it.
  126.         if(m_pColorToolbar->IsWindowVisible() == TRUE)
  127.             ShowControlBar(m_pColorToolbar, FALSE, FALSE);
  128.         else
  129.             // Otherwise, show it.
  130.             ShowControlBar(m_pColorToolbar, TRUE, FALSE);
  131. }
  132.  
  133. void CMainFrame::OnUpdateViewColortoolbar(CCmdUI* pCmdUI) 
  134. {
  135.     // Initially, the color toolbar pointer is 0, so the UI
  136.     // handler must not call IsWindowVisible unless the 
  137.     // toolbar has been created.
  138.     if (0 == m_pColorToolbar)
  139.         pCmdUI->SetCheck(FALSE);
  140.     else
  141.         if(m_pColorToolbar->IsWindowVisible() == TRUE)
  142.             pCmdUI->SetCheck(TRUE);
  143.         else
  144.             pCmdUI->SetCheck(FALSE);
  145. }
  146.  
  147. void CMainFrame::OnDestroy() 
  148. {
  149.     CFrameWnd::OnDestroy();
  150.     
  151.     // Since the color toolbar was dynamically created,
  152.     // it must be destroyed when the frame is destroyed.
  153.     if (0 != m_pColorToolbar)
  154.     {
  155.         delete m_pColorToolbar;
  156.         m_pColorToolbar = 0;
  157.     }
  158. }
  159.  
  160. BOOL CMainFrame::LoadWindowPlacement (LPWINDOWPLACEMENT pwp)
  161. {
  162.     CString strBuffer = 
  163.         AfxGetApp()->GetProfileString ("Settings", "WindowPos");
  164.  
  165.     if (strBuffer.IsEmpty ())
  166.         return FALSE;
  167.  
  168.     int cRead = _stscanf (strBuffer, "%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  169.             &pwp->flags, &pwp->showCmd,
  170.             &pwp->ptMinPosition.x, &pwp->ptMinPosition.y,
  171.             &pwp->ptMaxPosition.x, &pwp->ptMaxPosition.y,
  172.             &pwp->rcNormalPosition.left, &pwp->rcNormalPosition.top,
  173.             &pwp->rcNormalPosition.right, &pwp->rcNormalPosition.bottom);
  174.  
  175.     if (cRead != 10)
  176.         return FALSE;
  177.  
  178.     return TRUE;
  179. }
  180.  
  181. VOID CMainFrame::SaveWindowPlacement (LPWINDOWPLACEMENT pwp)
  182. {
  183.     CString strBuffer;
  184.     strBuffer.Format ("%i:%i:%i:%i:%i:%i:%i:%i:%i:%i",
  185.         pwp->flags, pwp->showCmd,
  186.         pwp->ptMinPosition.x, pwp->ptMinPosition.y,
  187.         pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
  188.         pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
  189.         pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
  190.  
  191.     AfxGetApp()->WriteProfileString("Settings", "WindowPos", strBuffer);
  192. }
  193.  
  194. void CMainFrame::OnClose()
  195. {
  196.     WINDOWPLACEMENT wp;
  197.     if (GetWindowPlacement (&wp))
  198.     {
  199.         if (IsZoomed ())
  200.             wp.flags |= WPF_RESTORETOMAXIMIZED;
  201.  
  202.         SaveWindowPlacement (&wp);
  203.     }
  204.     CFrameWnd::OnClose ();
  205. }
  206.