home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac / wdj0796.zip / RAJA.ZIP / ZCAPFORM.CPP < prev    next >
C/C++ Source or Header  |  1996-03-08  |  7KB  |  189 lines

  1. /*
  2.     File: ZCapForm.cpp
  3.     Author: V.Ramachandran
  4.     Date: 07 March, 1996
  5.  
  6.     ZCaptionFormView is a form view with a caption, which can be used
  7.     anywhere, but most useful when used with a splitter window.
  8. */
  9.  
  10. #include "stdafx.h"
  11. #include "zcapform.h"
  12.  
  13. #ifdef _DEBUG
  14. #undef THIS_FILE
  15. static char BASED_CODE THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. IMPLEMENT_DYNAMIC (ZCaptionFormView, CFormView)
  19.  
  20. #define new DEBUG_NEW
  21.  
  22. #define LEFT_MARGIN 2
  23.  
  24. BEGIN_MESSAGE_MAP(ZCaptionFormView, CFormView)
  25.     //{{AFX_MSG_MAP(ZCaptionFormView)
  26.     ON_MESSAGE (WM_NCCALCSIZE, OnNcCalcSize)
  27.     ON_WM_NCPAINT()
  28.     ON_WM_NCHITTEST()
  29.     //}}AFX_MSG_MAP
  30. END_MESSAGE_MAP()
  31.  
  32. /************************************************************************/
  33. // OnActivateView - MFC virtual function called whenever the view loses
  34. // or gains activation.
  35. // Parameters: Read MFC documentation, not used in this implementation.
  36. // Force repaint of caption, and then call base class.
  37. /************************************************************************/
  38.  
  39. void ZCaptionFormView::OnActivateView (BOOL bActivate, CView *pActivateView,
  40.                     CView *pDeactivateView)
  41. {
  42.     m_bActive = bActivate;
  43.     if ((m_nCaptionHeight > 0) && (::IsWindow (m_hWnd))) 
  44.         SendMessage (WM_NCPAINT);
  45.     CFormView::OnActivateView (bActivate, pActivateView, pDeactivateView);
  46. }
  47.  
  48. /************************************************************************/
  49. // OnActivateFrame - MFC virtual function called whenever the MDI child
  50. // frame activation state changes.
  51. // Parameter: nState indicates the state.
  52. // If state is inactive, send a WM_NCPAINT after setting m_bActive.
  53. // If state is active, don't do anything, because you will get a
  54. // OnActivateView as well for the active view.
  55. /************************************************************************/
  56.  
  57. void ZCaptionFormView::OnActivateFrame (UINT nState, CFrameWnd* pFrameWnd)
  58. {
  59.     m_bActive = (nState == WA_INACTIVE) ? FALSE : TRUE;
  60.     if (nState == WA_INACTIVE) {
  61.         if ((m_nCaptionHeight > 0) && (::IsWindow (m_hWnd))) 
  62.             SendMessage (WM_NCPAINT);
  63.     }
  64.     CFormView::OnActivateFrame(nState, pFrameWnd);
  65. }
  66.  
  67. /************************************************************************/
  68. // OnNcCalcSize - MFC handler for WM_NCCALCSIZE (read documentation on
  69. // message).
  70. // This message is used to calculate the client area of a window. Call
  71. // Default first to get the actual client area, and then return an area
  72. // minus the caption area.
  73. /************************************************************************/
  74.  
  75. LRESULT ZCaptionFormView::OnNcCalcSize(WPARAM wParam, LPARAM lParam)
  76. {
  77.     LRESULT lResult = Default ();
  78.     if (m_nCaptionHeight > 0) {
  79.         NCCALCSIZE_PARAMS FAR* lpncsp = (NCCALCSIZE_PARAMS FAR *)lParam;
  80.         lpncsp->rgrc [0].top += m_nCaptionHeight;
  81.     }
  82.     return lResult;
  83. }
  84.  
  85. /************************************************************************/
  86. // OnNcPaint - MFC handler for WM_NCPAINT
  87. // No parameters
  88. // Draw the caption in the non-client area. The color depends on whether is
  89. // active or not. Some points to be noted in this function: Use of
  90. // GetWindowDC and using a coordinate system wrt top-left of non-client 
  91. // area rather than client coordinates. 
  92. // Calls out to virtual function DoDrawCaption to do the actual drawing.
  93. /************************************************************************/
  94.  
  95. void ZCaptionFormView::OnNcPaint() 
  96. {
  97.     Default ();            // Draw other parts as normal.
  98.     if (m_nCaptionHeight <= 0)        // Optimize if Caption is off
  99.         return;
  100.     
  101. // Get the rectangle to paint. We use GetWindowRect and then shift to 
  102. // window based (non-client) coordinates. Effectively, we are only using
  103. // the width of the window, other values are set explicitly.
  104.  
  105.     RECT rect, windowRect;
  106.     GetWindowRect (&windowRect); 
  107.     SetRect (&rect, 0, 0, windowRect.right - windowRect.left,
  108.                                 m_nCaptionHeight + 1);
  109.  
  110. // Get the window dc. Remember we are painting the non client area.
  111.     CDC *pDC = GetWindowDC ();
  112.     if (pDC) {
  113.         DoDrawCaption (pDC, rect);        // Call out to virtual fn.
  114.         ReleaseDC (pDC);
  115.     }
  116.     else {            // Could not get DC!
  117.         TRACE0 ("ZCaptionFormView::OnNcPaint - GetWindowDC failed!.\n");
  118.         ASSERT (FALSE);
  119.     }
  120. }
  121.  
  122. /************************************************************************/
  123. // OnNcHitTest - MFC handler for WM_NCHITTEST
  124. // Parameters: mouse at screen coordinates (read message documentation).
  125. // Call Default first. If mouse is HTNOWHERE, return HTCLIENT. If this
  126. // is not done, clicking on the pseudo-caption will not activate this
  127. // view.
  128. /************************************************************************/
  129.  
  130. UINT ZCaptionFormView::OnNcHitTest (CPoint point)
  131. {
  132.     LRESULT lResult = Default ();
  133.     // Optimize if Caption is off
  134.     if ((m_nCaptionHeight > 0) && (lResult == HTNOWHERE))
  135.         lResult = HTCLIENT;
  136.     return LOWORD (lResult);
  137. }
  138.  
  139. /************************************************************************/
  140. // DoDrawCaption - virtual function override to draw caption
  141. // Parameters: Pointer to DC to draw on, rectangle to indicate area. 
  142. // Point is wrt to 0,0 of window (not client).  
  143. // This version draws the caption in the active caption color, and 
  144. // prints out a caption as well. 
  145. // Override this function to extend draw capabilities.
  146. // m_bActive tells you whether to draw in active colors, or inactive
  147. // colors.
  148. /************************************************************************/
  149.  
  150. void ZCaptionFormView::DoDrawCaption (CDC *pDC, const RECT& rect)
  151. {
  152.     CFrameWnd *pFrame = (CFrameWnd *)GetParentFrame ();
  153.     ASSERT (pFrame->IsKindOf (RUNTIME_CLASS (CFrameWnd)));
  154.  
  155. // Decide color on whether the view is currently active.    
  156.     COLORREF color = (m_bActive) ?
  157.             GetSysColor (COLOR_ACTIVECAPTION) : 
  158.             GetSysColor (COLOR_INACTIVECAPTION);
  159.  
  160.     CPen blackPen (PS_SOLID, 1, RGB (0, 0, 0));
  161.     CBrush blueBrush (color);
  162.  
  163. // Select pen and brush and draw a rectangle.
  164.  
  165.     CPen *pOldPen = pDC->SelectObject (&blackPen);
  166.     CBrush *pOldBrush = pDC->SelectObject (&blueBrush);
  167.     pDC->Rectangle (&rect);
  168.  
  169. // Now, write the caption.
  170.  
  171.     pDC->SetTextColor ((m_bActive) ?
  172.                         GetSysColor (COLOR_CAPTIONTEXT) : 
  173.                         GetSysColor (COLOR_INACTIVECAPTIONTEXT));
  174.     pDC->SetBkMode (TRANSPARENT);
  175.     
  176.     RECT rectText = rect;
  177.     rectText.left += LEFT_MARGIN;        // Leave some margin, looks better.
  178.  
  179.     CFont *pFont = GetFont ();        
  180.     if (pFont)
  181.         pDC->SelectObject (pFont);
  182.     pDC->DrawText (m_sCaption, m_sCaption.GetLength (), &rectText, 
  183.                             DT_SINGLELINE | DT_VCENTER);
  184.     pDC->SelectObject (pOldBrush);
  185.     pDC->SelectObject (pOldPen);
  186. }
  187.  
  188. // End.
  189.