home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectShow / Capture / AMCap / status.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  8.7 KB  |  321 lines

  1. //------------------------------------------------------------------------------
  2. // File: Status.cpp
  3. //
  4. // Desc: Status Bar Window Code
  5. //
  6. // Copyright (c) 1999-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9. #include <streams.h>
  10. #include <windowsx.h>
  11. #include <mmsystem.h>
  12. #include "status.h"
  13.  
  14.  
  15. // class names for status bar and static text windows
  16. TCHAR szStatusClass[] = TEXT("StatusClass");
  17. TCHAR szText[] = TEXT("SText");
  18. int   gStatusStdHeight;   // based on font metrics
  19.  
  20. static HANDLE ghFont;
  21. static HBRUSH ghbrHL, ghbrShadow;
  22.  
  23.  
  24. //------------------------------------------------------------------------------
  25. // Local Function Prototypes
  26. //------------------------------------------------------------------------------
  27. LRESULT CALLBACK statusWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  28. LRESULT CALLBACK fnText(HWND, UINT, WPARAM, LPARAM);
  29. static void PaintText(HWND hwnd, HDC hdc);
  30.  
  31.  
  32.  
  33. /*--------------------------------------------------------------+
  34. | statusCreateTools - create the objects needed for status bar
  35. |
  36. +--------------------------------------------------------------*/
  37. void
  38. statusCreateTools(void)
  39. {
  40.     HDC hdc;
  41.     TEXTMETRIC tm;
  42.     HFONT hfont;
  43.  
  44.     ghbrHL = CreateSolidBrush(GetSysColor(COLOR_BTNHIGHLIGHT));
  45.     ghbrShadow = CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW));
  46.  
  47.     /* Create the font we'll use for the status bar - use system as default */
  48.     ghFont = CreateFont(12, 0,      // height, width
  49.         0, 0,                       // escapement, orientation
  50.         FW_NORMAL,                  // weight,
  51.         FALSE, FALSE, FALSE,        // attributes
  52.         ANSI_CHARSET,               // charset
  53.         OUT_DEFAULT_PRECIS,         // output precision
  54.         CLIP_DEFAULT_PRECIS,        // clip precision
  55.         DEFAULT_QUALITY,            // quality
  56.         VARIABLE_PITCH | FF_MODERN,
  57.         TEXT("Helv"));
  58.  
  59.     if(ghFont == NULL)
  60.     {
  61.         ghFont = GetStockObject(SYSTEM_FONT);
  62.     }
  63.  
  64.     // find the char size to calc standard status bar height
  65.     hdc = GetDC(NULL);
  66.     hfont = (HFONT)SelectObject(hdc, ghFont);
  67.     GetTextMetrics(hdc, &tm);
  68.     SelectObject(hdc, hfont);
  69.     ReleaseDC(NULL, hdc);
  70.  
  71.     gStatusStdHeight = tm.tmHeight * 3 / 2;
  72. }
  73.  
  74.  
  75. /*--------------------------------------------------------------+
  76. | statusDeleteTools
  77. |
  78. +--------------------------------------------------------------*/
  79. void statusDeleteTools(void)
  80. {
  81.     DeleteObject(ghbrHL);
  82.     DeleteObject(ghbrShadow);
  83.  
  84.     DeleteObject(ghFont);
  85. }
  86.  
  87.  
  88. /*--------------------------------------------------------------+
  89. | statusInit - initialize for status window, register the
  90. |          Window's class.
  91. |
  92. +--------------------------------------------------------------*/
  93. BOOL statusInit(HANDLE hInst, HANDLE hPrev)
  94. {
  95.     WNDCLASS  cls;
  96.  
  97.     statusCreateTools();
  98.  
  99.     if(!hPrev)
  100.     {
  101.         cls.hCursor       = LoadCursor(NULL, IDC_ARROW);
  102.         cls.hIcon         = NULL;
  103.         cls.lpszMenuName  = NULL;
  104.         cls.lpszClassName = szStatusClass;
  105.         cls.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1);
  106.         cls.hInstance     = (HINSTANCE)hInst;
  107.         cls.style         = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  108.         cls.lpfnWndProc   = (WNDPROC)statusWndProc;
  109.         cls.cbClsExtra    = 0;
  110.         cls.cbWndExtra    = 0;
  111.  
  112.         if(!RegisterClass(&cls))
  113.             return FALSE;
  114.  
  115.         cls.hCursor        = LoadCursor(NULL,IDC_ARROW);
  116.         cls.hIcon          = NULL;
  117.         cls.lpszMenuName   = NULL;
  118.         cls.lpszClassName  = szText;
  119.         cls.hbrBackground  = (HBRUSH) (COLOR_BTNFACE + 1);
  120.         cls.hInstance      = (HINSTANCE)hInst;
  121.         cls.style          = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  122.         cls.lpfnWndProc    = (WNDPROC)fnText;
  123.         cls.cbClsExtra     = 0;
  124.         cls.cbWndExtra     = 0;
  125.  
  126.         if(!RegisterClass(&cls))
  127.             return FALSE;
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.  
  133.  
  134. /*--------------------------------------------------------------+
  135. | statusGetHeight
  136. |
  137. | Returns the recommended height for a status bar based on the
  138. | character dimensions of the font used
  139. +--------------------------------------------------------------*/
  140. int statusGetHeight(void)
  141. {
  142.     return(gStatusStdHeight);
  143. }
  144.  
  145.  
  146. /*--------------------------------------------------------------+
  147. | statusUpdateStatus - update the status line
  148. |
  149. | The argument can either be NULL or a string
  150. +--------------------------------------------------------------*/
  151. void statusUpdateStatus(HWND hwnd, LPCTSTR lpsz)
  152. {
  153.     HWND hwndtext;
  154.  
  155.     hwndtext = GetDlgItem(hwnd, 1);
  156.     if(!lpsz || *lpsz == '\0')
  157.     {
  158.         SetWindowText(hwndtext,TEXT(""));
  159.     }
  160.     else
  161.     {
  162.         SetWindowText(hwndtext, lpsz);
  163.     }
  164. }
  165.  
  166.  
  167. /*--------------------------------------------------------------+
  168. | statusWndProc - window proc for status window
  169. |
  170. +--------------------------------------------------------------*/
  171. LRESULT CALLBACK
  172. statusWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  173. {
  174.     PAINTSTRUCT ps;
  175.     HWND hwndSText;
  176.  
  177.     switch(msg)
  178.     {
  179.         case WM_CREATE:
  180.         {
  181.             /* we need to create the static text control for the status bar */
  182.             hwndSText = CreateWindow(szText,
  183.                 TEXT(""),
  184.                 WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS,
  185.                 0, 0, 0, 0,
  186.                 hwnd,
  187.                 (HMENU) 1,  // child id
  188.                 GetWindowInstance(hwnd),
  189.                 NULL);
  190.  
  191.             if(!hwndSText)
  192.             {
  193.                 return -1;
  194.             }
  195.             break;
  196.         }
  197.  
  198.         case WM_DESTROY:
  199.             statusDeleteTools();
  200.             break;
  201.  
  202.         case WM_SIZE:
  203.         {
  204.             RECT rc;
  205.  
  206.             GetClientRect(hwnd, &rc);
  207.  
  208.             MoveWindow(GetDlgItem(hwnd, 1),    // get child window handle
  209.                 2, 1,                   // xy just inside
  210.                 rc.right - 4,
  211.                 rc.bottom - 2,
  212.                 TRUE);
  213.  
  214.             break;
  215.         }
  216.  
  217.         case WM_PAINT:
  218.         {
  219.             BeginPaint(hwnd, &ps);
  220.  
  221.             // only the background and the child window need painting
  222.             EndPaint(hwnd, &ps);
  223.             break;
  224.         }
  225.  
  226.         case WM_SYSCOLORCHANGE:
  227.             statusDeleteTools();
  228.             statusCreateTools();
  229.             break;
  230.  
  231.         case WM_ERASEBKGND:
  232.             break;
  233.  
  234.     }
  235.  
  236.     return DefWindowProc(hwnd, msg, wParam, lParam);
  237. }
  238.  
  239.  
  240. /*--------------------------------------------------------------+
  241. | fnText - window proc for static text window
  242. |                               |
  243. +--------------------------------------------------------------*/
  244. LRESULT CALLBACK
  245. fnText(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam )
  246. {
  247.     switch(msg)
  248.     {
  249.         case WM_SETTEXT:
  250.             DefWindowProc(hwnd, msg, wParam, lParam);
  251.             InvalidateRect(hwnd,NULL,FALSE);
  252.             UpdateWindow(hwnd);
  253.             return 0L;
  254.  
  255.         case WM_ERASEBKGND:
  256.             return 0L;
  257.  
  258.         case WM_PAINT:
  259.         {
  260.             PAINTSTRUCT ps;
  261.  
  262.             BeginPaint(hwnd, &ps);
  263.             PaintText(hwnd, ps.hdc);
  264.             EndPaint(hwnd, &ps);
  265.             return 0L;
  266.         }
  267.     }
  268.  
  269.     return DefWindowProc(hwnd, msg, wParam, lParam);
  270. }
  271.  
  272.  
  273. /*--------------------------------------------------------------+
  274. | PaintText - paint the shadowed static text field
  275. |
  276. +--------------------------------------------------------------*/
  277. void PaintText(HWND hwnd, HDC hdc)
  278. {
  279.     RECT rc;
  280.     TCHAR ach[128];
  281.     int  len;
  282.     int  dx, dy;
  283.     RECT  rcFill;
  284.     HFONT hfontOld;
  285.     HBRUSH hbrSave;
  286.  
  287.     GetClientRect(hwnd, &rc);
  288.  
  289.     len = GetWindowText(hwnd,ach,sizeof(ach)/sizeof(ach[0]));
  290.  
  291.     SetBkColor(hdc, GetSysColor(COLOR_BTNFACE));
  292.     SetTextColor(hdc, GetSysColor(COLOR_BTNTEXT));
  293.  
  294.     hfontOld = (HFONT)SelectObject(hdc, ghFont);
  295.  
  296.     rcFill.left = rc.left + 1;
  297.     rcFill.right = rc.right - 1;
  298.     rcFill.top = rc.top + 1;
  299.     rcFill.bottom = rc.bottom - 1;
  300.  
  301.     /* move in some and do background and text in one swoosh */
  302.     ExtTextOut(hdc,4,1,ETO_OPAQUE,&rcFill,ach,len,NULL);
  303.  
  304.     dx = rc.right - rc.left;
  305.     dy = rc.bottom - rc.top;
  306.  
  307.     hbrSave = (HBRUSH)SelectObject(hdc, ghbrShadow);
  308.     PatBlt(hdc, rc.left, rc.top, 1, dy, PATCOPY);
  309.     PatBlt(hdc, rc.left, rc.top, dx, 1, PATCOPY);
  310.  
  311.     SelectObject(hdc, ghbrHL);
  312.     PatBlt(hdc, rc.right-1, rc.top+1, 1, dy-1, PATCOPY);
  313.     PatBlt(hdc, rc.left+1, rc.bottom -1, dx-1, 1,  PATCOPY);
  314.  
  315.     if(hfontOld)
  316.         SelectObject(hdc, hfontOld);
  317.     if(hbrSave)
  318.         SelectObject(hdc, hbrSave);
  319. }
  320.  
  321.