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

  1. // mainfrm.cpp : implementation of the CMainFrame 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.  
  14. #include "stdafx.h"
  15. #include "superpad.h"
  16. #include "mainfrm.h"
  17.  
  18.  
  19. IMPLEMENT_DYNCREATE(CMainFrame, CMDIFrameWnd)
  20. BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
  21.     //{{AFX_MSG_MAP(CMainFrame)
  22.     ON_WM_CREATE()
  23.     ON_WM_CLOSE()
  24.     //}}AFX_MSG_MAP
  25. END_MESSAGE_MAP()
  26.  
  27. static UINT BASED_CODE buttons[] =
  28. {
  29.     // same order as in the bitmap 'toolbar.bmp'
  30.     ID_FILE_NEW, ID_FILE_OPEN, ID_FILE_SAVE, 0,
  31.     ID_EDIT_CUT, ID_EDIT_COPY, ID_EDIT_PASTE, 0,
  32.     ID_FILE_PRINT, ID_APP_ABOUT,
  33. };
  34.  
  35. static UINT BASED_CODE indicators[] =
  36. {
  37.     0, ID_INDICATOR_CAPS, ID_INDICATOR_NUM, ID_INDICATOR_SCRL,
  38. };
  39.  
  40. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  41. {
  42.     if (CMDIFrameWnd::OnCreate(lpCreateStruct) == -1)
  43.         return -1;
  44.     if (!m_wndToolBar.Create(this) ||
  45.         !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
  46.         !m_wndToolBar.SetButtons(buttons, sizeof(buttons)/sizeof(UINT)))
  47.     {
  48.         return -1;      // fail to create
  49.     }
  50.     if (!m_wndStatusBar.Create(this) ||
  51.         !m_wndStatusBar.SetIndicators(indicators,
  52.         sizeof(indicators)/sizeof(UINT)))
  53.     {
  54.         return -1;      // fail to create
  55.     }
  56.     return 0;
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // Helpers for saving/restoring window state
  61.  
  62. static TCHAR BASED_CODE szSection[] = _T("Settings");
  63. static TCHAR BASED_CODE szWindowPos[] = _T("WindowPos");
  64. static TCHAR szFormat[] = _T("%u,%u,%d,%d,%d,%d,%d,%d,%d,%d");
  65.  
  66. static BOOL PASCAL NEAR ReadWindowPlacement(LPWINDOWPLACEMENT pwp)
  67. {
  68.     CString strBuffer = AfxGetApp()->GetProfileString(szSection, szWindowPos);
  69.     if (strBuffer.IsEmpty())
  70.         return FALSE;
  71.  
  72.     WINDOWPLACEMENT wp;
  73.     int nRead = _stscanf(strBuffer, szFormat,
  74.         &wp.flags, &wp.showCmd,
  75.         &wp.ptMinPosition.x, &wp.ptMinPosition.y,
  76.         &wp.ptMaxPosition.x, &wp.ptMaxPosition.y,
  77.         &wp.rcNormalPosition.left, &wp.rcNormalPosition.top,
  78.         &wp.rcNormalPosition.right, &wp.rcNormalPosition.bottom);
  79.  
  80.     if (nRead != 10)
  81.         return FALSE;
  82.  
  83.     wp.length = sizeof wp;
  84.     *pwp = wp;
  85.     return TRUE;
  86. }
  87.  
  88. static void PASCAL NEAR WriteWindowPlacement(LPWINDOWPLACEMENT pwp)
  89.     // write a window placement to settings section of app's ini file
  90. {
  91.     TCHAR szBuffer[sizeof("-32767")*8 + sizeof("65535")*2];
  92.  
  93.     wsprintf(szBuffer, szFormat,
  94.         pwp->flags, pwp->showCmd,
  95.         pwp->ptMinPosition.x, pwp->ptMinPosition.y,
  96.         pwp->ptMaxPosition.x, pwp->ptMaxPosition.y,
  97.         pwp->rcNormalPosition.left, pwp->rcNormalPosition.top,
  98.         pwp->rcNormalPosition.right, pwp->rcNormalPosition.bottom);
  99.     AfxGetApp()->WriteProfileString(szSection, szWindowPos, szBuffer);
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103.  
  104. void CMainFrame::InitialShowWindow(UINT nCmdShow)
  105. {
  106.     WINDOWPLACEMENT wp;
  107.     if (!ReadWindowPlacement(&wp))
  108.     {
  109.         ShowWindow(nCmdShow);
  110.         return;
  111.     }
  112.     if (nCmdShow != SW_SHOWNORMAL)
  113.         wp.showCmd = nCmdShow;
  114.     SetWindowPlacement(&wp);
  115.     ShowWindow(wp.showCmd);
  116. }
  117.  
  118. void CMainFrame::OnClose()
  119. {
  120.     // before it is destroyed, save the position of the window
  121.     WINDOWPLACEMENT wp;
  122.     wp.length = sizeof wp;
  123.     if (GetWindowPlacement(&wp))
  124.     {
  125.         wp.flags = 0;
  126.         if (IsZoomed())
  127.             wp.flags |= WPF_RESTORETOMAXIMIZED;
  128.         // and write it to the .INI file
  129.         WriteWindowPlacement(&wp);
  130.     }
  131.  
  132.     CMDIFrameWnd::OnClose();
  133. }
  134.  
  135. /////////////////////////////////////////////////////////////////////////////
  136.