home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / capture / amcap / status.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  7.7 KB  |  311 lines

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