home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c05 / custsbar / mystatba.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  3.6 KB  |  153 lines

  1. // StatusBar.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "CustSBar.h"
  6. #include "MyStatBa.h"
  7.  
  8. #include "CustSDoc.h"
  9.  
  10. #include "mainfrm.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. const COLORREF BLACK=RGB(0,0,0);
  19. const COLORREF RED=RGB(255,0,0);
  20. const COLORREF GREEN=RGB(0,255,0);
  21. const COLORREF BLUE=RGB(0,0,255);
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CCustomStatusBar
  25.  
  26. CCustomStatusBar::CCustomStatusBar()
  27. {
  28. }
  29.  
  30. CCustomStatusBar::~CCustomStatusBar()
  31. {
  32. }
  33.  
  34. BEGIN_MESSAGE_MAP(CCustomStatusBar, CStatusBarCtrl)
  35.     //{{AFX_MSG_MAP(CCustomStatusBar)
  36.     ON_WM_SIZE()
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CCustomStatusBar message handlers
  42. void CCustomStatusBar::DrawItem(LPDRAWITEMSTRUCT lpdis)
  43. {
  44.     // Handle drawing of the ownerdraw pane(s) of the
  45.     // CGraphicStatusBar object.
  46.  
  47.     CDC dc;
  48.     dc.m_hDC = lpdis->hDC;
  49.  
  50.     // The parameter gives us a rectangle that needs to be drawn
  51.     CRect rect(lpdis->rcItem);
  52.  
  53.     // itemData gives us a pointer to a structure that contains
  54.     // the ID of the bitmap that is to be drawn in the current.
  55.     // This structure was originally filled by CMainFrame, and
  56.     // then changed by the View's event handler that modifies
  57.     // the displayed color.
  58.     CBitmap Bitmap;
  59.     PaneTool *pt = (PaneTool *)lpdis->itemData;
  60.     Bitmap.LoadBitmap(pt->bitmapID);
  61.  
  62.     CDC srcDC;
  63.     srcDC.CreateCompatibleDC(NULL);
  64.     srcDC.SelectObject(&Bitmap);
  65.     dc.BitBlt(rect.left, rect.top, rect.Width(), rect.Height(),
  66.                 &srcDC, 0, 0, SRCCOPY);
  67. }
  68.  
  69. void CCustomStatusBar::OnSize(UINT nType, int cx, int cy) 
  70. {
  71.     CStatusBarCtrl::OnSize(nType, cx, cy);
  72.  
  73.     int aWidths[5] = {cx / 2, cx / 2 + 30, cx / 2 + 100,
  74.                         cx / 2 + 130, -1};
  75.     SetParts(5, aWidths);
  76.  
  77.     // Pane 0's text is written by the framework for menu and
  78.     // toolbar status, so nothing needs to be done. The framework
  79.     // will only do this if the custom status bar is given the ID
  80.     // AFX_IDW_STATUS_BAR in the call to Create.
  81.  
  82.     // Pane 1 shows the status of the Caps Lock key. 
  83.     // Also See CMainFrame::OnCmdMsg
  84.     DetermineKeyboardState();
  85.  
  86.     // Pane 2 has static text.
  87.     DetermineTextColor();
  88.  
  89.     // Pane 3 has a bitmap placed in it. Thus it has to have
  90.     // owner draw. We pass it the address of a structure that
  91.     // contains the bitmap's ID.
  92.     CMainFrame * cmf = (CMainFrame *)GetParent();
  93.     SetText((LPCTSTR) &cmf->pt, 3, SBT_OWNERDRAW);
  94.  
  95.     // Pane 4 will, width -1, takes over the rest of the
  96.     // status bar and contains the sizing grip.
  97. }
  98.  
  99. void CCustomStatusBar::DetermineTextColor()
  100. {
  101.     CMainFrame * cmf = (CMainFrame *)GetParent();
  102.     CCustomStatusBarDoc * ccsbd = 
  103.         (CCustomStatusBarDoc *)cmf->GetActiveDocument();
  104.  
  105.     CString color = "Black";
  106.     
  107.     // If the document doesn't exist yet, just set the color
  108.     // to black.
  109.     if (0 != ccsbd)
  110.         // Convert an RGB value to a CString.
  111.         switch (ccsbd->GetColor())
  112.         {
  113.             case RED:
  114.                 color = "Red";
  115.                 break;
  116.             case GREEN:
  117.                 color = "Green";
  118.                 break;
  119.             case BLUE:
  120.                 color = "Blue";
  121.                 break;
  122.         }
  123.  
  124.     SetText(color, 2, 0);
  125. }
  126.  
  127. void CCustomStatusBar::DetermineKeyboardState()
  128. {
  129.     // We completely take over writing the word CAP.
  130.     BYTE ks[256];
  131.     
  132.     GetKeyboardState(ks);
  133.  
  134.     CRect rect;
  135.     GetRect(1, &rect);
  136.     rect.OffsetRect(3, 2);
  137.  
  138.     CClientDC dc(this);
  139.     CFont * cf = GetFont();
  140.     if (cf)
  141.         dc.SelectObject(cf);
  142.     dc.SetBkMode(OPAQUE);
  143.     dc.SetBkColor(GetSysColor(COLOR_MENU));
  144.     
  145.     if (ks[VK_CAPITAL] & 1)
  146.         dc.SetTextColor(RGB(0, 0, 0));
  147.     else
  148.         dc.SetTextColor(RGB(127,127,127));
  149.  
  150.     dc.DrawText("CAP", rect, DT_LEFT);
  151. }
  152.  
  153.