ResizableWndState.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // This file is part of ResizableLib
  4. // https://github.com/ppescher/resizablelib
  5. //
  6. // Copyright (C) 2000-2015 by Paolo Messina
  7. // mailto:ppescher@hotmail.com
  8. //
  9. // The contents of this file are subject to the Artistic License 2.0
  10. // http://opensource.org/licenses/Artistic-2.0
  11. //
  12. // If you find this code useful, credits would be nice!
  13. //
  14. /////////////////////////////////////////////////////////////////////////////
  15. /*!
  16. * @file
  17. * @brief Implementation of the CResizableWndState class.
  18. */
  19. #include "stdafx.h"
  20. #include "ResizableWndState.h"
  21. //////////////////////////////////////////////////////////////////////
  22. // Construction/Destruction
  23. //////////////////////////////////////////////////////////////////////
  24. CResizableWndState::CResizableWndState()
  25. {
  26. }
  27. CResizableWndState::~CResizableWndState()
  28. {
  29. }
  30. // used to save/restore window's size and position
  31. // either in the registry or a private .INI file
  32. // depending on your application settings
  33. #define PLACEMENT_ENT _T("WindowPlacement")
  34. #define PLACEMENT_FMT _T("%d,%d,%d,%d,%d,%d,%d,%d")
  35. /*!
  36. * This function saves the current window position and size using the base
  37. * class persist method. Minimized and maximized state is also optionally
  38. * preserved.
  39. * @sa CResizableState::WriteState
  40. * @note Window coordinates are in the form used by the system functions
  41. * GetWindowPlacement and SetWindowPlacement.
  42. *
  43. * @param pszName String that identifies stored settings
  44. * @param bRectOnly Flag that specifies wether to ignore min/max state
  45. *
  46. * @return Returns @a TRUE if successful, @a FALSE otherwise
  47. */
  48. BOOL CResizableWndState::SaveWindowRect(LPCTSTR pszName, BOOL bRectOnly)
  49. {
  50. CString data, id;
  51. WINDOWPLACEMENT wp;
  52. ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  53. wp.length = sizeof(WINDOWPLACEMENT);
  54. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  55. return FALSE;
  56. // use workspace coordinates
  57. RECT& rc = wp.rcNormalPosition;
  58. if (bRectOnly) // save size/pos only (normal state)
  59. {
  60. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  61. rc.right, rc.bottom, SW_SHOWNORMAL, 0, 0, 0);
  62. }
  63. else // save also min/max state
  64. {
  65. data.Format(PLACEMENT_FMT, rc.left, rc.top,
  66. rc.right, rc.bottom, wp.showCmd, wp.flags,
  67. wp.ptMinPosition.x, wp.ptMinPosition.y);
  68. }
  69. id = CString(pszName) + PLACEMENT_ENT;
  70. return WriteState(id, data);
  71. }
  72. /*!
  73. * This function loads and set the current window position and size using
  74. * the base class persist method. Minimized and maximized state is also
  75. * optionally preserved.
  76. * @sa CResizableState::WriteState
  77. * @note Window coordinates are in the form used by the system functions
  78. * GetWindowPlacement and SetWindowPlacement.
  79. *
  80. * @param pszName String that identifies stored settings
  81. * @param bRectOnly Flag that specifies wether to ignore min/max state
  82. *
  83. * @return Returns @a TRUE if successful, @a FALSE otherwise
  84. */
  85. BOOL CResizableWndState::LoadWindowRect(LPCTSTR pszName, BOOL bRectOnly)
  86. {
  87. CString data, id;
  88. WINDOWPLACEMENT wp;
  89. id = CString(pszName) + PLACEMENT_ENT;
  90. if (!ReadState(id, data)) // never saved before
  91. return FALSE;
  92. ZeroMemory(&wp, sizeof(WINDOWPLACEMENT));
  93. wp.length = sizeof(WINDOWPLACEMENT);
  94. if (!GetResizableWnd()->GetWindowPlacement(&wp))
  95. return FALSE;
  96. // use workspace coordinates
  97. RECT& rc = wp.rcNormalPosition;
  98. if (_stscanf(data, PLACEMENT_FMT, &rc.left, &rc.top,
  99. &rc.right, &rc.bottom, &wp.showCmd, &wp.flags,
  100. &wp.ptMinPosition.x, &wp.ptMinPosition.y) == 8)
  101. {
  102. if (bRectOnly) // restore size/pos only
  103. {
  104. wp.showCmd = SW_SHOWNORMAL;
  105. wp.flags = 0;
  106. return GetResizableWnd()->SetWindowPlacement(&wp);
  107. }
  108. else // restore also min/max state
  109. {
  110. return GetResizableWnd()->SetWindowPlacement(&wp);
  111. }
  112. }
  113. return FALSE;
  114. }