WindowPlacement.cpp
上传用户:yatsl7111
上传日期:2007-01-08
资源大小:1433k
文件大小:4k
源码类别:

图形图象

开发平台:

Visual C++

  1. // MSJ 
  2. // It it works contact Paul DiLascia
  3. // if not you're own your own
  4. #include "StdAfx.h"
  5. #include "WindowPlacement.h"
  6. CWindowPlacement::CWindowPlacement()
  7. {
  8.    // Note: "length" is inherited from WINDOWPLACEMENT
  9.    length = sizeof(WINDOWPLACEMENT);
  10.    m_showCmd = -1;
  11. }
  12. CWindowPlacement::~CWindowPlacement()
  13. {
  14. }
  15. //////////////////
  16. // Restore window placement from profile key
  17. BOOL CWindowPlacement::Restore(LPCTSTR lpKeyName, CWnd* pWnd)
  18. {
  19.    if (!GetProfileWP(lpKeyName))
  20.    return FALSE;
  21.    // Only restore if window intersets the screen.
  22.    //
  23.    CRect rcTemp;
  24.    CRect rcScreen(0,0,GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN));
  25.    if (!::IntersectRect(&rcTemp, &rcNormalPosition, &rcScreen))
  26.       return FALSE;
  27.    pWnd->SetWindowPlacement(this);  
  28.    return TRUE;
  29. }
  30. //////////////////
  31. // Get window placement from profile.
  32. BOOL CWindowPlacement::GetProfileWP(LPCTSTR lpKeyName)
  33. {
  34.    CWinApp *pApp = AfxGetApp();
  35.    ASSERT_VALID(pApp);
  36.    m_showCmd = pApp->GetProfileInt(lpKeyName, _T("wp.showCmd"), -1);
  37.    if (m_showCmd == -1)
  38.    return FALSE;
  39.    showCmd = m_showCmd;
  40.    flags   = pApp->GetProfileInt(lpKeyName, _T("wp.flags"), flags);
  41. ptMinPosition.x = pApp->GetProfileInt(lpKeyName, _T("wp.ptMinPosition.x"),ptMinPosition.x);
  42. ptMinPosition.y = pApp->GetProfileInt(lpKeyName, _T("wp.ptMinPosition.y"),ptMinPosition.y);
  43. ptMaxPosition.x = pApp->GetProfileInt(lpKeyName, _T("wp.ptMaxPosition.x"),ptMaxPosition.x);
  44. ptMaxPosition.y = pApp->GetProfileInt(lpKeyName, _T("wp.ptMaxPosition.y"),ptMaxPosition.y);
  45.    RECT& rc = rcNormalPosition;  // because I hate typing
  46.    rc.left  = pApp->GetProfileInt(lpKeyName, _T("wp.left"),   rc.left);
  47.    rc.right = pApp->GetProfileInt(lpKeyName, _T("wp.right"),  rc.right);
  48.    rc.top   = pApp->GetProfileInt(lpKeyName, _T("wp.top"),    rc.top);
  49.    rc.bottom= pApp->GetProfileInt(lpKeyName, _T("wp.bottom"), rc.bottom);
  50.    return TRUE;
  51. }
  52. ////////////////
  53. // Save window placement in app profile
  54. void CWindowPlacement::Save(LPCTSTR lpKeyName, CWnd* pWnd)
  55. {
  56.    pWnd->GetWindowPlacement(this);
  57.    WriteProfileWP(lpKeyName);
  58. }
  59. //////////////////
  60. // Write window placement to app profile
  61. void CWindowPlacement::WriteProfileWP(LPCTSTR lpKeyName)
  62. {
  63.    CWinApp *pApp = AfxGetApp();
  64.    ASSERT_VALID(pApp);
  65.    pApp->WriteProfileInt(lpKeyName, _T("wp.showCmd"),         showCmd);
  66.    pApp->WriteProfileInt(lpKeyName, _T("wp.flags"),       flags);
  67.    pApp->WriteProfileInt(lpKeyName, _T("wp.ptMinPosition.x"), ptMinPosition.x);
  68.    pApp->WriteProfileInt(lpKeyName, _T("wp.ptMinPosition.y"), ptMinPosition.y);
  69.    pApp->WriteProfileInt(lpKeyName, _T("wp.ptMaxPosition.x"), ptMaxPosition.x);
  70.    pApp->WriteProfileInt(lpKeyName, _T("wp.ptMaxPosition.y"), ptMaxPosition.y);
  71.    pApp->WriteProfileInt(lpKeyName, _T("wp.left"), rcNormalPosition.left);
  72.    pApp->WriteProfileInt(lpKeyName, _T("wp.right"), rcNormalPosition.right);
  73.    pApp->WriteProfileInt(lpKeyName, _T("wp.top"),   rcNormalPosition.top);
  74.    pApp->WriteProfileInt(lpKeyName, _T("wp.bottom"),rcNormalPosition.bottom);
  75. }
  76. // The ugly casts are required to help the VC++ 3.0 compiler decide which
  77. // operator<< or operator>> to use. If you're using VC++ 4.0 or later, you 
  78. // can delete this stuff.
  79. //
  80. #if (_MSC_VER < 1000)      // 1000 = VC++ 4.0
  81. #define UINT_CAST (LONG)
  82. #define UINT_CASTREF (LONG&)
  83. #else
  84. #define UINT_CAST
  85. #define UINT_CASTREF
  86. #endif
  87. //////////////////
  88. // Write window placement to archive
  89. // WARNING: archiving functions are untested.
  90. CArchive& operator<<(CArchive& ar, const CWindowPlacement& wp)
  91. {
  92.    ar << UINT_CAST wp.length;
  93.    ar << UINT_CAST wp.flags;
  94.    ar << UINT_CAST wp.showCmd;
  95.    ar << wp.ptMinPosition;
  96.    ar << wp.ptMaxPosition;
  97.    ar << wp.rcNormalPosition;
  98.    return ar;
  99. }
  100. //////////////////
  101. // Read window placement from archive
  102. // WARNING: archiving functions are untested.
  103. CArchive& operator>>(CArchive& ar, CWindowPlacement& wp)
  104. {
  105.    ar >> UINT_CASTREF wp.length;
  106.    ar >> UINT_CASTREF wp.flags;
  107.    ar >> UINT_CASTREF wp.showCmd;
  108.    ar >> wp.ptMinPosition;
  109.    ar >> wp.ptMaxPosition;
  110.    ar >> wp.rcNormalPosition;
  111.    return ar;
  112. }
  113.