ResizableDialog.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // ResizableDialog.cpp : implementation file
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // This file is part of ResizableLib
  6. // https://github.com/ppescher/resizablelib
  7. //
  8. // Copyright (C) 2000-2015 by Paolo Messina
  9. // mailto:ppescher@hotmail.com
  10. //
  11. // The contents of this file are subject to the Artistic License 2.0
  12. // http://opensource.org/licenses/Artistic-2.0
  13. //
  14. // If you find this code useful, credits would be nice!
  15. //
  16. /////////////////////////////////////////////////////////////////////////////
  17. #include "stdafx.h"
  18. #include "ResizableDialog.h"
  19. #ifdef _DEBUG
  20. #define new DEBUG_NEW
  21. #undef THIS_FILE
  22. static char THIS_FILE[] = __FILE__;
  23. #endif
  24. /////////////////////////////////////////////////////////////////////////////
  25. // CResizableDialog
  26. inline void CResizableDialog::PrivateConstruct()
  27. {
  28. m_bEnableSaveRestore = FALSE;
  29. m_dwGripTempState = 1;
  30. m_bRectOnly = FALSE;
  31. }
  32. CResizableDialog::CResizableDialog()
  33. {
  34. PrivateConstruct();
  35. }
  36. CResizableDialog::CResizableDialog(UINT nIDTemplate, CWnd* pParentWnd)
  37. : CDialog(nIDTemplate, pParentWnd)
  38. {
  39. PrivateConstruct();
  40. }
  41. CResizableDialog::CResizableDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
  42. : CDialog(lpszTemplateName, pParentWnd)
  43. {
  44. PrivateConstruct();
  45. }
  46. CResizableDialog::~CResizableDialog()
  47. {
  48. }
  49. BEGIN_MESSAGE_MAP(CResizableDialog, CDialog)
  50. //{{AFX_MSG_MAP(CResizableDialog)
  51. ON_WM_GETMINMAXINFO()
  52. ON_WM_SIZE()
  53. ON_WM_DESTROY()
  54. ON_WM_ERASEBKGND()
  55. ON_WM_NCCREATE()
  56. //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58. /////////////////////////////////////////////////////////////////////////////
  59. // CResizableDialog message handlers
  60. BOOL CResizableDialog::OnNcCreate(LPCREATESTRUCT lpCreateStruct)
  61. {
  62. if (!CDialog::OnNcCreate(lpCreateStruct))
  63. return FALSE;
  64. // child dialogs don't want resizable border or size grip,
  65. // nor they can handle the min/max size constraints
  66. BOOL bChild = lpCreateStruct->style & WS_CHILD;
  67. // create and init the size-grip
  68. if (!CreateSizeGrip(!bChild))
  69. return FALSE;
  70. if (!bChild)
  71. {
  72. // set the initial size as the min track size
  73. SetMinTrackSize(CSize(lpCreateStruct->cx, lpCreateStruct->cy));
  74. }
  75. MakeResizable(lpCreateStruct);
  76. return TRUE;
  77. }
  78. void CResizableDialog::OnDestroy()
  79. {
  80. if (m_bEnableSaveRestore)
  81. SaveWindowRect(m_sSection, m_bRectOnly);
  82. // reset instance data
  83. RemoveAllAnchors();
  84. ResetAllRects();
  85. PrivateConstruct();
  86. CDialog::OnDestroy();
  87. }
  88. void CResizableDialog::OnSize(UINT nType, int cx, int cy)
  89. {
  90. CDialog::OnSize(nType, cx, cy);
  91. if (nType == SIZE_MAXHIDE || nType == SIZE_MAXSHOW)
  92. return; // arrangement not needed
  93. if (nType == SIZE_MAXIMIZED)
  94. HideSizeGrip(&m_dwGripTempState);
  95. else
  96. ShowSizeGrip(&m_dwGripTempState);
  97. // update grip and layout
  98. UpdateSizeGrip();
  99. ArrangeLayout();
  100. }
  101. void CResizableDialog::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  102. {
  103. MinMaxInfo(lpMMI);
  104. }
  105. // NOTE: this must be called after setting the layout
  106. // to have the dialog and its controls displayed properly
  107. void CResizableDialog::EnableSaveRestore(LPCTSTR pszSection, BOOL bRectOnly)
  108. {
  109. m_sSection = pszSection;
  110. m_bEnableSaveRestore = TRUE;
  111. m_bRectOnly = bRectOnly;
  112. // restore immediately
  113. LoadWindowRect(pszSection, bRectOnly);
  114. }
  115. BOOL CResizableDialog::OnEraseBkgnd(CDC* pDC)
  116. {
  117. ClipChildren(pDC, FALSE);
  118. BOOL bRet = CDialog::OnEraseBkgnd(pDC);
  119. ClipChildren(pDC, TRUE);
  120. return bRet;
  121. }
  122. LRESULT CResizableDialog::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  123. {
  124. if (message != WM_NCCALCSIZE || wParam == 0)
  125. return CDialog::WindowProc(message, wParam, lParam);
  126. LRESULT lResult = 0;
  127. HandleNcCalcSize(FALSE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
  128. lResult = CDialog::WindowProc(message, wParam, lParam);
  129. HandleNcCalcSize(TRUE, (LPNCCALCSIZE_PARAMS)lParam, lResult);
  130. return lResult;
  131. }