ResizableFormView.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // ResizableFormView.cpp : implementation file
  2. //
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Copyright (C) 2000-2002 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 "ResizableFormView.h"
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CResizableFormView
  25. IMPLEMENT_DYNAMIC(CResizableFormView, CFormView)
  26. inline void CResizableFormView::PrivateConstruct()
  27. {
  28. m_bInitDone = FALSE;
  29. m_dwGripTempState = GHR_SCROLLBAR | GHR_ALIGNMENT | GHR_MAXIMIZED;
  30. }
  31. CResizableFormView::CResizableFormView(UINT nIDTemplate)
  32. : CFormView(nIDTemplate)
  33. {
  34. PrivateConstruct();
  35. }
  36. CResizableFormView::CResizableFormView(LPCTSTR lpszTemplateName)
  37. : CFormView(lpszTemplateName)
  38. {
  39. PrivateConstruct();
  40. }
  41. CResizableFormView::~CResizableFormView()
  42. {
  43. }
  44. BEGIN_MESSAGE_MAP(CResizableFormView, CFormView)
  45. //{{AFX_MSG_MAP(CResizableFormView)
  46. ON_WM_SIZE()
  47. ON_WM_ERASEBKGND()
  48. ON_WM_CREATE()
  49. ON_WM_GETMINMAXINFO()
  50. ON_WM_DESTROY()
  51. //}}AFX_MSG_MAP
  52. END_MESSAGE_MAP()
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CResizableFormView diagnostics
  55. #ifdef _DEBUG
  56. void CResizableFormView::AssertValid() const
  57. {
  58. CFormView::AssertValid();
  59. }
  60. void CResizableFormView::Dump(CDumpContext& dc) const
  61. {
  62. CFormView::Dump(dc);
  63. }
  64. #endif //_DEBUG
  65. /////////////////////////////////////////////////////////////////////////////
  66. // CResizableFormView message handlers
  67. void CResizableFormView::OnSize(UINT nType, int cx, int cy)
  68. {
  69. CFormView::OnSize(nType, cx, cy);
  70. CWnd* pParent = GetParent();
  71. // hide zise grip when parent is maximized
  72. if (pParent->IsZoomed())
  73. HideSizeGrip(&m_dwGripTempState, GHR_MAXIMIZED);
  74. else
  75. ShowSizeGrip(&m_dwGripTempState, GHR_MAXIMIZED);
  76. // hide size grip when there are scrollbars
  77. CSize size = GetTotalSize();
  78. if (cx < size.cx || cy < size.cy)
  79. HideSizeGrip(&m_dwGripTempState, GHR_SCROLLBAR);
  80. else
  81. ShowSizeGrip(&m_dwGripTempState, GHR_SCROLLBAR);
  82. // hide size grip when the parent window is not resizable
  83. // or the form is not bottom-right aligned (e.g. there's a statusbar)
  84. DWORD dwStyle = pParent->GetStyle();
  85. CRect rectParent, rectChild;
  86. GetWindowRect(rectChild);
  87. ::MapWindowPoints(NULL, pParent->GetSafeHwnd(), (LPPOINT)&rectChild, 2);
  88. pParent->GetClientRect(rectParent);
  89. if (!(dwStyle & WS_THICKFRAME)
  90. || (rectChild.BottomRight() != rectParent.BottomRight()))
  91. HideSizeGrip(&m_dwGripTempState, GHR_ALIGNMENT);
  92. else
  93. ShowSizeGrip(&m_dwGripTempState, GHR_ALIGNMENT);
  94. // update grip and layout
  95. UpdateSizeGrip();
  96. ArrangeLayout();
  97. }
  98. void CResizableFormView::OnInitialUpdate()
  99. {
  100. CFormView::OnInitialUpdate();
  101. m_bInitDone = TRUE;
  102. // MDI child need this
  103. ArrangeLayout();
  104. }
  105. void CResizableFormView::GetTotalClientRect(LPRECT lpRect)
  106. {
  107. GetClientRect(lpRect);
  108. // get dialog template's size
  109. // (this is set in CFormView::Create)
  110. CSize size = GetTotalSize();
  111. // before initialization use dialog's size
  112. if (!m_bInitDone)
  113. {
  114. lpRect->right = lpRect->left + size.cx;
  115. lpRect->bottom = lpRect->top + size.cy;
  116. return;
  117. }
  118. // otherwise, give the correct size if scrollbars active
  119. if (m_nMapMode < 0) // scrollbars disabled
  120. return;
  121. // enlarge reported client area when needed
  122. CRect rect(lpRect);
  123. if (rect.Width() < size.cx)
  124. rect.right = rect.left + size.cx;
  125. if (rect.Height() < size.cy)
  126. rect.bottom = rect.top + size.cy;
  127. rect.OffsetRect(-GetScrollPosition());
  128. *lpRect = rect;
  129. }
  130. BOOL CResizableFormView::OnEraseBkgnd(CDC* pDC)
  131. {
  132. // Windows XP doesn't like clipping regions ...try this!
  133. EraseBackground(pDC);
  134. return TRUE;
  135. /* ClipChildren(pDC); // old-method (for safety)
  136. return CFormView::OnEraseBkgnd(pDC);
  137. */
  138. }
  139. int CResizableFormView::OnCreate(LPCREATESTRUCT lpCreateStruct)
  140. {
  141. if (CFormView::OnCreate(lpCreateStruct) == -1)
  142. return -1;
  143. // create and init the size-grip
  144. if (!CreateSizeGrip())
  145. return -1;
  146. return 0;
  147. }
  148. void CResizableFormView::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
  149. {
  150. MinMaxInfo(lpMMI);
  151. }
  152. void CResizableFormView::OnDestroy()
  153. {
  154. RemoveAllAnchors();
  155. CFormView::OnDestroy();
  156. }