home *** CD-ROM | disk | FTP | other *** search
/ Beginning Direct3D Game Programming / Direct3D.iso / directx / dxf / samples / multimedia / directinput / diconfig / flexinfobox.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  4.0 KB  |  171 lines

  1. //-----------------------------------------------------------------------------
  2. // File: flexinfobox.cpp
  3. //
  4. // Desc: Implements a simple text box that displays a text string.
  5. //       CFlexInfoBox is derived from CFlexWnd.  It is used by the page
  6. //       for displaying direction throughout the UI.  The strings are
  7. //       stored as resources.  The class has a static buffer which will
  8. //       be filled with the string by the resource API when needed.
  9. //
  10. // Copyright (C) 1999-2000 Microsoft Corporation. All Rights Reserved.
  11. //-----------------------------------------------------------------------------
  12.  
  13. #include "common.hpp"
  14.  
  15. CFlexInfoBox::CFlexInfoBox() :
  16.     m_iCurIndex(-1),
  17.     m_rgbText(RGB(255,255,255)),
  18.     m_rgbBk(RGB(0,0,0)),
  19.     m_rgbSelText(RGB(0,0,255)),
  20.     m_rgbSelBk(RGB(0,0,0)),
  21.     m_rgbFill(RGB(0,0,255)),
  22.     m_rgbLine(RGB(0,255,255)),
  23.     m_hFont(NULL)
  24. {
  25. }
  26.  
  27. CFlexInfoBox::~CFlexInfoBox()
  28. {
  29. }
  30.  
  31. void CFlexInfoBox::SetText(int iIndex)
  32. {
  33.     if (iIndex == m_iCurIndex)
  34.         return;
  35.  
  36.     // Load the string from resource
  37.     LoadString(g_hModule, iIndex, m_tszText, MAX_PATH);
  38.     DWORD dwe = GetLastError();
  39.  
  40.     m_iCurIndex = iIndex;
  41.     Invalidate();
  42. }
  43.  
  44. void CFlexInfoBox::SetFont(HFONT hFont)
  45. {
  46.     m_hFont = hFont;
  47.  
  48.     if (m_hWnd == NULL)
  49.         return;
  50.  
  51.     Invalidate();
  52. }
  53.  
  54. void CFlexInfoBox::SetColors(COLORREF text, COLORREF bk, COLORREF seltext, COLORREF selbk, COLORREF fill, COLORREF line)
  55. {
  56.     m_rgbText = text;
  57.     m_rgbBk = bk;
  58.     m_rgbSelText = seltext;
  59.     m_rgbSelBk = selbk;
  60.     m_rgbFill = fill;
  61.     m_rgbLine = line;
  62.     Invalidate();
  63. }
  64.  
  65. void CFlexInfoBox::SetRect()
  66. {
  67.     if (m_hWnd == NULL)
  68.         return;
  69.  
  70.     RECT rect = GetRect();
  71.     SetWindowPos(m_hWnd, NULL, rect.left, rect.top, rect.right, rect.bottom, SWP_NOZORDER | SWP_NOMOVE);
  72. }
  73.  
  74. RECT CFlexInfoBox::GetRect(const RECT &rect)
  75. {
  76.     int h = GetTextHeight(m_hFont);
  77.     RECT ret = {rect.left, rect.top, rect.right, rect.top + h + 2};
  78.     return ret;
  79. }
  80.  
  81. RECT CFlexInfoBox::GetRect()
  82. {
  83.     RECT rect;
  84.     GetClientRect(&rect);
  85.     return GetRect(rect);
  86. }
  87.  
  88. void CFlexInfoBox::OnPaint(HDC hDC)
  89. {
  90.     HDC hBDC = NULL, hODC = NULL;
  91.     CBitmap *pbm = NULL;
  92.  
  93.     if (!InRenderMode())
  94.     {
  95.         hODC = hDC;
  96.         pbm = CBitmap::Create(GetClientSize(), RGB(0,0,0), hDC);
  97.         if (pbm != NULL)
  98.         {
  99.             hBDC = pbm->BeginPaintInto();
  100.             if (hBDC != NULL)
  101.             {
  102.                 hDC = hBDC;
  103.             }
  104.         }
  105.     }
  106.  
  107.     InternalPaint(hDC);
  108.  
  109.     if (!InRenderMode())
  110.     {
  111.         if (pbm != NULL)
  112.         {
  113.             if (hBDC != NULL)
  114.             {
  115.                 pbm->EndPaintInto(hBDC);
  116.                 pbm->Draw(hODC);
  117.             }
  118.             delete pbm;
  119.         }
  120.     }
  121. }
  122.  
  123. void CFlexInfoBox::InternalPaint(HDC hDC)
  124. {
  125.     TCHAR tszResourceString[MAX_PATH];
  126.     HGDIOBJ hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, m_rgbLine);
  127.     if (hPen != NULL)
  128.     {
  129.         HGDIOBJ hOldPen = SelectObject(hDC, hPen);
  130.  
  131.         HGDIOBJ hBrush = (HGDIOBJ)CreateSolidBrush(m_rgbBk);
  132.         if (hBrush != NULL)
  133.         {
  134.             HGDIOBJ hOldBrush = SelectObject(hDC, hBrush);
  135.  
  136.             RECT rect = {0,0,0,0}, titlerc;
  137.             GetClientRect(&rect);
  138.             titlerc = rect;
  139.             Rectangle(hDC, rect.left, rect.top, rect.right, rect.bottom);
  140.  
  141.             rect.left++;
  142.             rect.top++;
  143.             rect.right--;
  144.             rect.bottom--;
  145.  
  146.             SetTextColor(hDC, m_rgbLine);  // User line color for Information title
  147.             SetBkMode(hDC, TRANSPARENT);
  148.  
  149.             LoadString(g_hModule, IDS_INFO_TITLE, tszResourceString, MAX_PATH);
  150.             // Get the title area rantangle
  151.             DrawText(hDC, tszResourceString, -1, &titlerc, DT_CENTER|DT_NOPREFIX|DT_CALCRECT);
  152.             // Adjust right edge position to old value
  153.             titlerc.right = rect.right + 1;
  154.             // Draw a rectangle around the title area.
  155.             Rectangle(hDC, titlerc.left, titlerc.top, titlerc.right, titlerc.bottom);
  156.             // Draw title text (Information)
  157.             DrawText(hDC, tszResourceString, -1, &titlerc, DT_CENTER|DT_NOPREFIX);
  158.             // Draw the message text
  159.             SetTextColor(hDC, m_rgbText);
  160.             rect.top = titlerc.bottom + 1;
  161.             DrawText(hDC, m_tszText, -1, &rect, DT_LEFT|DT_NOPREFIX|DT_WORDBREAK);
  162.  
  163.             SelectObject(hDC, hOldBrush);
  164.             DeleteObject(hBrush);
  165.         }
  166.  
  167.         SelectObject(hDC, hOldPen);
  168.         DeleteObject(hPen);
  169.     }
  170. }
  171.