ResizableDialog.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // ResizableDialog.cpp : implementation file
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Copyright (C) 2000-2001 by Paolo Messina
  6. // (http://www.geocities.com/ppescher - ppescher@yahoo.com)
  7. //
  8. // The contents of this file are subject to the Artistic License (the "License").
  9. // You may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at:
  11. // http://www.opensource.org/licenses/artistic-license.html
  12. //
  13. // If you find this code useful, credits would be nice!
  14. //
  15. /////////////////////////////////////////////////////////////////////////////
  16. #include "stdafx.h"
  17. #include "ResizableDialog.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CResizableDialog
  25. inline void CResizableDialog::Construct()
  26. {
  27. m_bInitDone = FALSE;
  28. m_bEnableSaveRestore = FALSE;
  29. }
  30. CResizableDialog::CResizableDialog()
  31. {
  32. Construct();
  33. }
  34. CResizableDialog::CResizableDialog(UINT nIDTemplate, CWnd* pParentWnd)
  35. : CDialog(nIDTemplate, pParentWnd)
  36. {
  37. Construct();
  38. }
  39. CResizableDialog::CResizableDialog(LPCTSTR lpszTemplateName, CWnd* pParentWnd)
  40. : CDialog(lpszTemplateName, pParentWnd)
  41. {
  42. Construct();
  43. }
  44. CResizableDialog::~CResizableDialog()
  45. {
  46. }
  47. BEGIN_MESSAGE_MAP(CResizableDialog, CDialog)
  48. //{{AFX_MSG_MAP(CResizableDialog)
  49. ON_WM_GETMINMAXINFO()
  50. ON_WM_SIZE()
  51. ON_WM_DESTROY()
  52. ON_WM_CREATE()
  53. ON_WM_ERASEBKGND()
  54. //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CResizableDialog message handlers
  58. int CResizableDialog::OnCreate(LPCREATESTRUCT lpCreateStruct)
  59. {
  60. if (CDialog::OnCreate(lpCreateStruct) == -1)
  61. return -1;
  62. // keep client area
  63. CRect rect;
  64. GetClientRect(&rect);
  65. // set resizable style
  66. ModifyStyle(DS_MODALFRAME, WS_POPUP | WS_THICKFRAME);
  67. // adjust size to reflect new style
  68. ::AdjustWindowRectEx(&rect, GetStyle(),
  69. ::IsMenu(GetMenu()->GetSafeHmenu()), GetExStyle());
  70. SetWindowPos(NULL, 0, 0, rect.Width(), rect.Height(), SWP_FRAMECHANGED|
  71. SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_NOREPOSITION);
  72. if (!InitGrip())
  73. return -1;
  74. return 0;
  75. }
  76. BOOL CResizableDialog::OnInitDialog()
  77. {
  78. CDialog::OnInitDialog();
  79. // gets the template size as the min track size
  80. CRect rc;
  81. GetWindowRect(&rc);
  82. SetMinTrackSize(rc.Size());
  83. // init
  84. UpdateGripPos();
  85. m_bInitDone = TRUE;
  86. return TRUE; // return TRUE unless you set the focus to a control
  87. }
  88. void CResizableDialog::OnDestroy()
  89. {
  90. if (m_bEnableSaveRestore)
  91. SaveWindowRect(m_sSection, m_bRectOnly);
  92. // remove child windows
  93. RemoveAllAnchors();
  94. CDialog::OnDestroy();
  95. }
  96. void CResizableDialog::OnSize(UINT nType, int cx, int cy)
  97. {
  98. CWnd::OnSize(nType, cx, cy);
  99. if (nType == SIZE_MAXHIDE || nType == SIZE_MAXSHOW)
  100. return; // arrangement not needed
  101. if (m_bInitDone)
  102. {
  103. // update size-grip
  104. UpdateGripPos();
  105. ArrangeLayout();
  106. }
  107. }
  108. void CResizableDialog::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  109. {
  110. if (!m_bInitDone)
  111. return;
  112. MinMaxInfo(lpMMI);
  113. }
  114. // NOTE: this must be called after setting the layout
  115. // to have the dialog and its controls displayed properly
  116. void CResizableDialog::EnableSaveRestore(LPCTSTR pszSection, BOOL bRectOnly)
  117. {
  118. m_sSection = pszSection;
  119. m_bEnableSaveRestore = TRUE;
  120. m_bRectOnly = bRectOnly;
  121. // restore immediately
  122. LoadWindowRect(pszSection, bRectOnly);
  123. }
  124. BOOL CResizableDialog::OnEraseBkgnd(CDC* pDC)
  125. {
  126. ClipChildren(pDC);
  127. return CDialog::OnEraseBkgnd(pDC);
  128. }