home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / ctlhtmlc / uiwinpl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-03-29  |  3.8 KB  |  114 lines

  1. /*---------------------------------------------------------------------------*\
  2.  | UIWinPl.cpp  - DevUI(tm) library window placement class                   |
  3.  |                From Developing User Interfaces for Microsoft Windows      |
  4.  |                Copyright (c) 1999, Everett N. McKay                       |
  5. \*---------------------------------------------------------------------------*/
  6.  
  7. #include "stdafx.h"
  8. #include "DevUI.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // class CMcWindowPlacement
  17.  
  18. // effect:  Saves the window placement to registry.
  19. HRESULT CMcWindowPlacement::SetProfile (LPCTSTR section, LPCTSTR entry)
  20. {
  21.    CWinApp *pApp = AfxGetApp();
  22.    int retVal;
  23.  
  24.    ASSERT_VALID(pApp);
  25.    ASSERT(section);
  26.    ASSERT(entry);
  27.  
  28.    retVal = pApp->WriteProfileBinary(section, entry, (BYTE *)this, sizeof(WINDOWPLACEMENT));
  29.    return (retVal > 0) ? NO_ERROR : E_FAIL;
  30. }
  31.  
  32. // effect:  Restores window placement from registry. If (checkValues), makes sure
  33. //          size will fit on screen and location is on screen.
  34. HRESULT CMcWindowPlacement::GetProfile (LPCTSTR section, LPCTSTR entry, BOOL checkValues)
  35. {
  36.    CWinApp *pApp = AfxGetApp();
  37.    WINDOWPLACEMENT *pWp;
  38.    UINT  size;
  39.  
  40.    ASSERT_VALID(pApp);
  41.    ASSERT(section);
  42.    ASSERT(entry);
  43.  
  44.    if (!pApp->GetProfileBinary(section, entry, (BYTE **)&pWp, &size))
  45.       return E_FAIL;
  46.    if (size == 0)
  47.    {
  48.       // size will be 0 if reset - just return now
  49.       delete pWp; // still must delete, since memory has been allocated
  50.       return E_FAIL;
  51.    }
  52.    ASSERT(size == sizeof(WINDOWPLACEMENT));
  53.    if (size != sizeof(WINDOWPLACEMENT))
  54.    {
  55.       delete pWp;
  56.       return E_FAIL;
  57.    }
  58.  
  59.    if (checkValues)
  60.    {
  61.       CRect tempRect, screenRect(0, 0, GetSystemMetrics(SM_CXSCREEN),
  62.                                        GetSystemMetrics(SM_CYSCREEN));
  63.  
  64.       // make sure at least part of the window is visible
  65.       if (!IntersectRect(&tempRect, &pWp->rcNormalPosition, &screenRect))
  66.       {
  67.          // move window to screen origin
  68.          pWp->rcNormalPosition.right  -= pWp->rcNormalPosition.left;
  69.          pWp->rcNormalPosition.left    = 0;
  70.          pWp->rcNormalPosition.bottom -= pWp->rcNormalPosition.top;
  71.          pWp->rcNormalPosition.top     = 0;
  72.       }
  73.  
  74.       // make window no larger than screen
  75.       if (pWp->rcNormalPosition.right  - pWp->rcNormalPosition.left > GetSystemMetrics(SM_CXSCREEN))
  76.          pWp->rcNormalPosition.right   = pWp->rcNormalPosition.left + GetSystemMetrics(SM_CXSCREEN);
  77.       if (pWp->rcNormalPosition.bottom - pWp->rcNormalPosition.top  > GetSystemMetrics(SM_CYSCREEN))
  78.          pWp->rcNormalPosition.bottom  = pWp->rcNormalPosition.top  + GetSystemMetrics(SM_CYSCREEN);
  79.    }
  80.  
  81.    memcpy(this, pWp, sizeof(WINDOWPLACEMENT));
  82.    delete pWp;
  83.    return NO_ERROR;
  84. }
  85.  
  86. // effect:  Save window placement for the given window, setting this to the given 
  87. //          window's placement.
  88. HRESULT CMcWindowPlacement::SaveWindowPlacement (LPCTSTR section, LPCTSTR entry, 
  89.                                  CWnd *pWnd)
  90. {
  91.    ASSERT_VALID(pWnd);
  92.  
  93.    if (!pWnd->GetWindowPlacement(this))
  94.       return E_FAIL;
  95.    return SetProfile(section, entry);
  96. }
  97.  
  98. // effect:  Restores window placement for the given window, setting this to the 
  99. //          given window's placement. If (checkValues), makes sure
  100. //          size will fit on screen and location is on screen.
  101. HRESULT CMcWindowPlacement::RestoreWindowPlacement (LPCTSTR section, LPCTSTR entry, 
  102.                                  CWnd *pWnd, BOOL checkValues)
  103. {
  104.    ASSERT_VALID(pWnd);
  105.  
  106.    if (FAILED(GetProfile(section, entry, checkValues)))
  107.       return E_FAIL;
  108.  
  109.    if (!pWnd->SetWindowPlacement(this))
  110.       return E_FAIL;
  111.    else
  112.       return NO_ERROR;
  113. }
  114.