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