home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / disolve.zip / FADER.C next >
C/C++ Source or Header  |  1990-06-19  |  11KB  |  333 lines

  1. /* Fader.c
  2.    source for Fader/Dissolve style controls
  3.  
  4.     Copyright 1990 by William R. Ross
  5. */
  6.  
  7. #include "windows.h"
  8. #define FADER_MAIN
  9. #include "fader.h"
  10. #include <sys\types.h> /* for ftime() */
  11. #include <sys\timeb.h> /* ditto */
  12.  
  13. #define FCN
  14.  
  15. static void FadeIn ();
  16. static void Dissolve ();
  17. static void FadeOut ();
  18. static void Wait ();
  19.  
  20. /******************************************
  21. * FaderInit ()
  22. * registers Fader window class
  23. *
  24. *******************************************/
  25. FCN BOOL FaderInit( hInstance )
  26. HANDLE hInstance;
  27. {
  28.     PWNDCLASS   pClass;
  29.  
  30.     pClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  31.  
  32.     /* this is the class for the FADER control */
  33.     pClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  34.     pClass->hIcon          = NULL;
  35.     pClass->lpszMenuName   = (LPSTR)NULL;
  36.     pClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  37.     pClass->hInstance      = hInstance;
  38.     pClass->style          = CS_HREDRAW | CS_VREDRAW;
  39.     /* here's the interesting parts of the class... */
  40.     pClass->lpfnWndProc    = FaderWndProc;
  41.     pClass->lpszClassName  = (LPSTR)"FADER";
  42.  
  43.     if (!RegisterClass( (LPWNDCLASS)pClass ) )
  44.         /* Initialization failed.
  45.          * Windows will automatically deallocate all allocated memory.
  46.          */
  47.         return FALSE;
  48.  
  49.     LocalFree( (HANDLE)pClass );
  50.     return TRUE;        /* Initialization succeeded */
  51. } /* end of FaderInit */
  52.  
  53. /***************************************************
  54. * FadeIn ()
  55. *
  56. ***************************************************/
  57. FCN void FadeIn (hWnd, lpszText)
  58. HWND hWnd;
  59. LPSTR lpszText;
  60. {
  61.     HDC hDC, hMemDC;
  62.     HBITMAP hBM;
  63.     RECT rect;
  64.     int nWidth, nHeight;
  65.     int i;
  66.  
  67.     GetClientRect (hWnd, &rect);
  68.     nWidth = rect.right - rect.left;
  69.     nHeight = rect.bottom - rect.top;
  70.  
  71.     hDC = GetDC (hWnd);
  72.     hMemDC = CreateCompatibleDC (hDC);
  73.     hBM = CreateCompatibleBitmap (hDC, nWidth, nHeight);
  74.  
  75.     SelectObject (hMemDC, hBM);
  76.     PatBlt (hMemDC, 0, 0, nWidth, nHeight, WHITENESS);
  77.     DrawText (hMemDC, (LPSTR)lpszText, -1, &rect,
  78.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  79.     SelectObject(hMemDC, GetStockObject(LTGRAY_BRUSH));
  80.     PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  81.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  82.     Wait(47);
  83.  
  84.      DrawText (hMemDC, (LPSTR)lpszText, -1, &rect,
  85.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  86.      SelectObject(hMemDC, GetStockObject(GRAY_BRUSH));
  87.      PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  88.      BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  89.     Wait(47);
  90.  
  91.      DrawText (hMemDC, (LPSTR)lpszText, -1, &rect,
  92.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  93.      SelectObject(hMemDC, GetStockObject(DKGRAY_BRUSH));
  94.      PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  95.      BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  96.      Wait(47);
  97.  
  98.      DrawText (hMemDC, (LPSTR)lpszText, -1, &rect,
  99.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  100.      SelectObject(hMemDC, GetStockObject(BLACK_BRUSH));
  101.      PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  102.      BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  103.  
  104.  
  105.      DeleteDC (hMemDC);
  106.      DeleteObject (hBM);
  107.      ReleaseDC(hWnd, hDC);
  108.  
  109.     SetWindowText (hWnd, (LPSTR)lpszText);
  110. } /* end of FadeIn */
  111.  
  112. /***************************************************
  113. * Dissolve ()
  114. *
  115. ***************************************************/
  116. FCN void Dissolve (hWnd, lpszNew, szOld)
  117. HWND hWnd;
  118. LPSTR lpszNew;
  119. char *szOld;
  120. {
  121.     HDC hDC, hMemDCOld, hMemDCNew;
  122.     HBITMAP hBMOld, hBMNew;
  123.     RECT rect;
  124.     int nWidth, nHeight;
  125.     unsigned int i;
  126.  
  127.     GetClientRect (hWnd, &rect);
  128.     nWidth = rect.right - rect.left;
  129.     nHeight = rect.bottom - rect.top;
  130.  
  131.     hDC = GetDC (hWnd);
  132.     hMemDCOld = CreateCompatibleDC (hDC);
  133.     hMemDCNew = CreateCompatibleDC (hDC);
  134.     hBMOld = CreateCompatibleBitmap (hDC, nWidth, nHeight);
  135.     hBMNew = CreateCompatibleBitmap (hDC, nWidth, nHeight);
  136.  
  137.     SelectObject (hMemDCOld, hBMOld);
  138.     SelectObject (hMemDCNew, hBMNew);
  139.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, WHITENESS);
  140.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, WHITENESS);
  141.  
  142.     DrawText (hMemDCOld, (LPSTR)szOld, -1, &rect,
  143.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  144.     SelectObject(hMemDCOld, GetStockObject(DKGRAY_BRUSH));
  145.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  146.     DrawText (hMemDCNew, (LPSTR)lpszNew, -1, &rect,
  147.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  148.     SelectObject(hMemDCNew, GetStockObject(LTGRAY_BRUSH));
  149.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  150.     BitBlt (hMemDCOld, 0, 0, nWidth, nHeight, hMemDCNew, 0, 0, (DWORD)0x8800C6);
  151.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDCOld, 0, 0, SRCCOPY);
  152.     Wait(55);
  153.  
  154.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, WHITENESS);
  155.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, WHITENESS);
  156.  
  157.     DrawText (hMemDCOld, (LPSTR)szOld, -1, &rect,
  158.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  159.     SelectObject(hMemDCOld, GetStockObject(GRAY_BRUSH));
  160.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  161.     DrawText (hMemDCNew, (LPSTR)lpszNew, -1, &rect,
  162.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  163.     SelectObject(hMemDCNew, GetStockObject(GRAY_BRUSH));
  164.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  165.     BitBlt (hMemDCOld, 0, 0, nWidth, nHeight, hMemDCNew, 0, 0, (DWORD)0x8800C6);
  166.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDCOld, 0, 0, SRCCOPY);
  167.     Wait(55);
  168.  
  169.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, WHITENESS);
  170.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, WHITENESS);
  171.  
  172.     DrawText (hMemDCOld, (LPSTR)szOld, -1, &rect,
  173.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  174.     SelectObject(hMemDCOld, GetStockObject(LTGRAY_BRUSH));
  175.     PatBlt (hMemDCOld, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  176.     DrawText (hMemDCNew, (LPSTR)lpszNew, -1, &rect,
  177.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  178.     SelectObject(hMemDCNew, GetStockObject(DKGRAY_BRUSH));
  179.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  180.     BitBlt (hMemDCOld, 0, 0, nWidth, nHeight, hMemDCNew, 0, 0, (DWORD)0x8800C6);
  181.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDCOld, 0, 0, SRCCOPY);
  182.     Wait(55);
  183.  
  184.     PatBlt (hMemDCNew, 0, 0, nWidth, nHeight, WHITENESS);
  185.  
  186.     DrawText (hMemDCNew, (LPSTR)lpszNew, -1, &rect,
  187.              DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  188.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDCNew, 0, 0, SRCCOPY);
  189.  
  190.     DeleteDC (hMemDCOld);
  191.     DeleteDC (hMemDCNew);
  192.     DeleteObject (hBMOld);
  193.     DeleteObject (hBMNew);
  194.     ReleaseDC(hWnd, hDC);
  195.  
  196.     SetWindowText (hWnd, (LPSTR)lpszNew);
  197. } /* end of Dissolve */
  198.  
  199. /***************************************************
  200. * FadeOut ()
  201. *
  202. ***************************************************/
  203. FCN void FadeOut (hWnd, szOld)
  204. HWND hWnd;
  205. char *szOld;
  206. {
  207.     HDC hDC, hMemDC;
  208.     HBITMAP hBM;
  209.     RECT rect;
  210.     int nWidth, nHeight;
  211.     int i;
  212.  
  213.     GetClientRect (hWnd, &rect);
  214.     nWidth = rect.right - rect.left;
  215.     nHeight = rect.bottom - rect.top;
  216.  
  217.     hDC = GetDC (hWnd);
  218.     hMemDC = CreateCompatibleDC (hDC);
  219.     hBM = CreateCompatibleBitmap (hDC, nWidth, nHeight);
  220.  
  221.     SelectObject (hMemDC, hBM);
  222.     PatBlt (hMemDC, 0, 0, nWidth, nHeight, WHITENESS);
  223.     DrawText (hMemDC, (LPSTR)szOld, -1, &rect,
  224.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  225.     SelectObject(hMemDC, GetStockObject(DKGRAY_BRUSH));
  226.     PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  227.     BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  228.     Wait(47);
  229.  
  230.      DrawText (hMemDC, (LPSTR)szOld, -1, &rect,
  231.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  232.      SelectObject(hMemDC, GetStockObject(GRAY_BRUSH));
  233.      PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  234.      BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  235.     Wait(47);
  236.  
  237.      DrawText (hMemDC, (LPSTR)szOld, -1, &rect,
  238.                 DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  239.      SelectObject(hMemDC, GetStockObject(LTGRAY_BRUSH));
  240.      PatBlt (hMemDC, 0, 0, nWidth, nHeight, (DWORD)0xFA0089);
  241.      BitBlt (hDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  242.     Wait(47);
  243.  
  244.     PatBlt (hDC, 0, 0, nWidth, nHeight, WHITENESS);
  245.  
  246.      DeleteDC (hMemDC);
  247.      DeleteObject (hBM);
  248.      ReleaseDC(hWnd, hDC);
  249.  
  250.     SetWindowText (hWnd, (LPSTR) '\0');
  251. } /* end of FadeOut */
  252.  
  253. /***************************************************
  254. * FaderWndProc ()
  255. * window procedure for fader control
  256. *
  257. ***************************************************/
  258. FCN long FAR PASCAL FaderWndProc( hWnd, message, wParam, lParam )
  259. HWND hWnd;
  260. unsigned message;
  261. WORD wParam;
  262. LONG lParam;
  263. {
  264.     char szText [80];
  265.     HDC hDC, hMemDC;
  266.     HBITMAP hBM;
  267.     RECT rect;
  268.  
  269.     switch (message)
  270.     {
  271.     case WM_FADER:
  272.         GetWindowText (hWnd, szText, sizeof(szText));
  273.  
  274.         /* lParam is new text, szText is current text */
  275.         if (!lParam && !szText)
  276.             break; /* nothing to do */
  277.  
  278.         else if (lParam && !szText)
  279.             FadeIn (hWnd, (LPSTR)lParam);
  280.  
  281.         else if (lParam && szText)
  282.             Dissolve(hWnd, (LPSTR)lParam, szText);
  283.  
  284.         else if (!lParam && szText)
  285.             FadeOut(hWnd, szText);
  286.  
  287.         break;
  288.  
  289.     case WM_PAINT:
  290.         GetClientRect (hWnd, &rect);
  291.         GetWindowText (hWnd, szText, sizeof(szText));
  292.         hDC = GetDC (hWnd);
  293.         DrawText (hDC, szText, -1, &rect,
  294.                  DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  295.         ReleaseDC (hWnd, hDC);
  296.         ValidateRect (hWnd, (LPRECT)NULL);
  297.         break;
  298.  
  299.     default:
  300.         return DefWindowProc( hWnd, message, wParam, lParam );
  301.         break;
  302.     } /* switch */
  303.  
  304.     return(0L);
  305. } /* end of FaderWndProc */
  306.  
  307. /***************************************************
  308. * Wait ()
  309. * wait for "interval" number of milliseconds
  310. * use only for small intervals < 1000 ms
  311. ***************************************************/
  312. FCN void Wait (int interval)
  313. {
  314.     struct timeb timebuffer;
  315.     unsigned short startMS;
  316.  
  317.     ftime (&timebuffer);
  318.     startMS = timebuffer.millitm;
  319.     while (TRUE)
  320.         {
  321.         ftime (&timebuffer);
  322.         if (startMS > timebuffer.millitm)
  323.             timebuffer.millitm += 1000;
  324.  
  325.         if ((timebuffer.millitm - startMS) > interval)
  326.             break;
  327.         } /* while */
  328.  
  329. } /* end of Wait */
  330.