home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 8.ddi / SOUNDER.ZIP / SOUNDER.C next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  11.6 KB  |  344 lines

  1. // Borland C++ - (C) Copyright 1992 by Borland International
  2.  
  3. //*******************************************************************
  4. //
  5. // program - sounder.c
  6. // purpose - a C windows program to demonstrate sound APIs in Win 3.1
  7. //
  8. //*******************************************************************
  9.  
  10. #define STRICT
  11.  
  12. #include <windows.h>
  13. #include <mmsystem.h>
  14.  
  15. #include "sounder.h"
  16.  
  17. // data initialized by first instance
  18. typedef struct tagSETUPDATA
  19.     {
  20.         char   szAppName[20]; // name of application
  21.     } SETUPDATA;
  22.  
  23. SETUPDATA SetUpData;
  24.  
  25. // Data that can be referenced throughout the
  26. // program but not passed to other instances
  27.  
  28. HANDLE    hInst;                              // hInstance of application
  29. HWND      hWndMain;                           // hWnd of main window
  30. char      szAppName[10];                      // name of application
  31.  
  32. // function prototypes
  33.  
  34. int      PASCAL        WinMain(HINSTANCE,HINSTANCE,LPSTR,int);
  35. void                   InitSound(HINSTANCE,HINSTANCE,LPSTR,int);
  36. void                   InitSoundFirst(HINSTANCE);
  37. void                   InitSoundAdded(HINSTANCE);
  38. void                   InitSoundEvery(HINSTANCE,int);
  39. LRESULT CALLBACK       SounderWndProc(HWND,UINT,WPARAM,LPARAM);
  40. void                   SoundPaint(HWND);
  41.  
  42. //*******************************************************************
  43. // WinMain - sounder main
  44. //
  45. // paramaters:
  46. //             hInstance     - The instance of this instance of this
  47. //                             application.
  48. //             hPrevInstance - The instance of the previous instance
  49. //                             of this application. This will be 0
  50. //                             if this is the first instance.
  51. //             lpszCmdLine   - A long pointer to the command line that
  52. //                             started this application.
  53. //             cmdShow       - Indicates how the window is to be shown
  54. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  55. //                             SW_MIMIMIZE.
  56. //
  57. // returns:
  58. //             wParam from last message.
  59. //
  60. //*******************************************************************
  61. int PASCAL WinMain(
  62. HINSTANCE hInstance,
  63. HINSTANCE hPrevInstance,
  64. LPSTR lpszCmdLine,
  65. int cmdShow)
  66. {
  67.         MSG   msg;
  68.  
  69.         // Go init this application.
  70.         InitSound(hInstance, hPrevInstance, lpszCmdLine, cmdShow);
  71.  
  72.         // Get and dispatch messages for this applicaton.
  73.         while (GetMessage(&msg, NULL, 0, 0))
  74.         {
  75.                 TranslateMessage(&msg);
  76.                 DispatchMessage(&msg);
  77.         }
  78.  
  79.         return(msg.wParam);
  80. }
  81.  
  82. //*******************************************************************
  83. // InitSound - init the Sound application
  84. //
  85. // paramaters:
  86. //             hInstance     - The instance of this instance of this
  87. //                             application.
  88. //             hPrevInstance - The instance of the previous instance
  89. //                             of this application. This will be 0
  90. //                             if this is the first instance.
  91. //             lpszCmdLine   - A long pointer to the command line that
  92. //                             started this application.
  93. //             cmdShow       - Indicates how the window is to be shown
  94. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  95. //                             SW_MIMIMIZE.
  96. //
  97. //*******************************************************************
  98. #pragma argsused
  99. void InitSound(
  100. HINSTANCE hInstance,
  101. HINSTANCE hPrevInstance,
  102. LPSTR lpszCmdLine,
  103. int cmdShow)
  104. {
  105.         if (! hPrevInstance)           // if no previous instance, this is first
  106.                 InitSoundFirst(hInstance);
  107.         else
  108.                 InitSoundAdded(hPrevInstance);   // this is not first instance
  109.  
  110.         InitSoundEvery(hInstance, cmdShow);  // initialization for all instances
  111. }
  112.  
  113. //*******************************************************************
  114. // InitSoundFirst - done only for first instance of Sounder
  115. //
  116. // paramaters:
  117. //             hInstance     - The instance of this instance of this
  118. //                             application.
  119. //
  120. //*******************************************************************
  121. void InitSoundFirst(
  122. HINSTANCE hInstance)
  123. {
  124.         WNDCLASS wcSoundClass;
  125.  
  126.         // Get string from resource with application name.
  127.         LoadString(hInstance, IDS_NAME, (LPSTR) SetUpData.szAppName, 10);
  128.  
  129.         // Define the window class for this application.
  130.         wcSoundClass.lpszClassName = SetUpData.szAppName;
  131.         wcSoundClass.hInstance     = hInstance;
  132.         wcSoundClass.lpfnWndProc   = SounderWndProc;
  133.         wcSoundClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  134.         wcSoundClass.hIcon         = LoadIcon(hInstance, SetUpData.szAppName);
  135.         wcSoundClass.lpszMenuName  = NULL;
  136.         wcSoundClass.hbrBackground = GetStockObject(WHITE_BRUSH);
  137.         wcSoundClass.style         = CS_HREDRAW | CS_VREDRAW;
  138.         wcSoundClass.cbClsExtra    = 0;
  139.         wcSoundClass.cbWndExtra    = 0;
  140.  
  141.         // Register the class
  142.         RegisterClass(&wcSoundClass);
  143. }
  144.  
  145. //*******************************************************************
  146. // InitSoundAdded - done only for added instances of Sound
  147. //
  148. // paramaters:
  149. //             hPrevInstance - The instance of the previous instance
  150. //                             of this application.
  151. //
  152. //*******************************************************************
  153. void InitSoundAdded(
  154. HINSTANCE hPrevInstance)
  155. {
  156.         // get the results of the initialization of first instance
  157.         GetInstanceData(hPrevInstance, (BYTE*)&SetUpData, sizeof(SETUPDATA));
  158. }
  159.  
  160. //*******************************************************************
  161. // InitSoundEvery - done for every instance of Sound
  162. //
  163. // paramaters:
  164. //             hInstance     - The instance of this instance of this
  165. //                             application.
  166. //             cmdShow       - Indicates how the window is to be shown
  167. //                             initially. ie. SW_SHOWNORMAL, SW_HIDE,
  168. //                             SW_MIMIMIZE.
  169. //
  170. //*******************************************************************
  171. void InitSoundEvery(
  172. HINSTANCE hInstance,
  173. int cmdShow)
  174. {
  175.         hInst = hInstance;       // save for use by window procs
  176.  
  177.         // Create applications main window.
  178.         hWndMain = CreateWindow(
  179.                                     SetUpData.szAppName,     // window class name
  180.                                     SetUpData.szAppName,     // window title
  181.                                     WS_OVERLAPPEDWINDOW,     // type of window
  182.                                     100,                        // x  window location
  183.                                     100,                     // y
  184.                                     450,                        // cx and size
  185.                                     250,                     // cy
  186.                                     NULL,                    // no parent for this window
  187.                                     NULL,                    // use the class menu
  188.                                     hInstance,               // who created this window
  189.                                     NULL                     // no parms to pass on
  190.                                     );
  191.  
  192.         // Update display of main window.
  193.         ShowWindow(hWndMain, cmdShow);
  194.         UpdateWindow(hWndMain);
  195. }
  196.  
  197.  
  198. //*******************************************************************
  199. // SoundWndProc - handles messages for this application
  200. //
  201. // paramaters:
  202. //             hWnd          - The window handle for this message
  203. //             message       - The message number
  204. //             wParam        - The WPARAM parmater for this message
  205. //             lParam        - The LPARAM parmater for this message
  206. //
  207. // returns:
  208. //             depends on message.
  209. //
  210. //*******************************************************************
  211. LRESULT CALLBACK SounderWndProc(
  212. HWND hWnd,
  213. UINT message,
  214. WPARAM wParam,
  215. LPARAM lParam)
  216. {
  217.         HDC hdc;
  218.         RECT rct;
  219.         DWORD res;
  220.         static char Buffer[100], Buffer1[100];
  221.         static MCI_GENERIC_PARMS MciGenParm;
  222.         static MCI_OPEN_PARMS MciOpenParm;
  223.         static MCI_PLAY_PARMS MciPlayParm;
  224.         WORD wDeviceID;
  225.  
  226.         switch (message)
  227.         {
  228.                 case WM_CREATE:
  229.                         CreateWindow ("button", "&Clear",
  230.                                                     WS_CHILD| WS_VISIBLE| BS_PUSHBUTTON,
  231.                                                     20, 40, 120, 30,
  232.                                                     hWnd, (HMENU)IDBT_CLEAR, hInst, NULL);
  233.  
  234.                         CreateWindow ("button", "&sndSoundPlay",
  235.                                                     WS_CHILD| WS_VISIBLE| BS_PUSHBUTTON,
  236.                                                     20, 80, 120, 30,
  237.                                                     hWnd, (HMENU)IDBT_SNDPLAY, hInst, NULL);
  238.  
  239.                         CreateWindow ("button", "Mci &Command",
  240.                                                     WS_CHILD| WS_VISIBLE| BS_PUSHBUTTON,
  241.                                                     20, 120, 120, 30,
  242.                                                     hWnd, (HMENU)IDBT_MCICMD, hInst, NULL);
  243.  
  244.                         CreateWindow ("button", "Mci &String",
  245.                                                     WS_CHILD| WS_VISIBLE| BS_PUSHBUTTON,
  246.                                                     20, 160, 120, 30,
  247.                                                     hWnd, (HMENU)IDBT_MCISTR, hInst, NULL);
  248.  
  249.                         return(DefWindowProc(hWnd, message, wParam, lParam));
  250.  
  251.                 case WM_COMMAND:
  252.                         switch (wParam)
  253.                         {
  254.                                 case IDBT_CLEAR:
  255.                                         hdc = GetDC (hWnd);
  256.                                         GetClientRect (hWnd, &rct);
  257.                                         BitBlt (hdc, 150, 40, rct.right, rct.bottom, NULL, 0, 0, WHITENESS);
  258.                                         TextOut (hdc, 20, 5, "Is your sound board and setup installed?", 40);
  259.                                         ReleaseDC (hWnd, hdc);
  260.                                         break;
  261.  
  262.                                 case IDBT_SNDPLAY:
  263.                                         hdc = GetDC (hWnd);
  264.                                         TextOut (hdc, 150, 80, "API: sndPlaySound()", 19);
  265.                                         ReleaseDC (hWnd, hdc);
  266.                                         sndPlaySound ("sounder.wav", SND_SYNC);
  267.                                         break;
  268.  
  269.                                 case IDBT_MCISTR:
  270.                                         hdc = GetDC (hWnd);
  271.                                         wsprintf (Buffer1, "API: mciSendString() open/play/stop/close");
  272.                                         TextOut (hdc, 150, 160, Buffer1, lstrlen(Buffer1));
  273.                                         res = mciSendString ((LPSTR)"open sounder.wav alias sounder", Buffer, 80, NULL);
  274.                                         if (!res)
  275.                                             MessageBox (hWnd, "Play", "Sounder", MB_OK);
  276.  
  277.                                         res = mciSendString ((LPSTR)"play sounder", Buffer, 80, NULL);
  278.                                         if (!res)
  279.                                             MessageBox (hWnd, "Stop", "Sounder", MB_OK);
  280.  
  281.                                         res = mciSendString ((LPSTR)"stop sounder", NULL, 0, NULL);
  282.                                         if (!res)
  283.                                             MessageBox (hWnd, "Close", "Sounder", MB_OK);
  284.  
  285.                                         res = mciSendString ((LPSTR)"close sounder", NULL, 0, NULL);
  286.                                         ReleaseDC (hWnd, hdc);
  287.                                         break;
  288.  
  289.                                 case IDBT_MCICMD:
  290.                                         hdc = GetDC (hWnd);
  291.                                         wsprintf (Buffer1, "API: mciSendCommand()");
  292.                                         TextOut (hdc, 150, 120, Buffer1, lstrlen(Buffer1));
  293.                                         MciOpenParm.dwCallback = 0L;
  294.                                         MciOpenParm.wDeviceID = 0;
  295.                                         MciOpenParm.wReserved0 = 0;
  296.                                         MciOpenParm.lpstrDeviceType = NULL;
  297.                                         MciOpenParm.lpstrElementName = (LPSTR)"SOUNDER.WAV";
  298.                                         MciOpenParm.lpstrAlias = NULL;
  299.                                         res = mciSendCommand (0, MCI_OPEN, MCI_WAIT| MCI_OPEN_ELEMENT,
  300.                                                                                     (DWORD)(LPMCI_OPEN_PARMS)&MciOpenParm);
  301.                                         if (!res)
  302.                                             MessageBox (hWnd, "Play", "Sounder", MB_OK);
  303.  
  304.                                         MciPlayParm.dwCallback = (unsigned long)hWnd;
  305.                                         MciPlayParm.dwFrom = 0;
  306.                                         MciPlayParm.dwTo = 0;
  307.                                         wDeviceID = MciOpenParm.wDeviceID;
  308.                                         res = mciSendCommand (wDeviceID, MCI_PLAY, MCI_NOTIFY,
  309.                                                                                     (DWORD) (LPMCI_PLAY_PARMS)&MciPlayParm);
  310.                                         if (!res)
  311.                                             MessageBox (hWnd, "Stop", "Sounder", MB_OK);
  312.  
  313.                                         MciGenParm.dwCallback = 0L;
  314.                                         res = mciSendCommand (wDeviceID, MCI_STOP, MCI_WAIT,
  315.                                                                                     (DWORD)(LPMCI_GENERIC_PARMS)&MciGenParm);
  316.                                         if (!res)
  317.                                             MessageBox (hWnd, "Close", "Sounder", MB_OK);
  318.  
  319.                                         res = mciSendCommand (wDeviceID, MCI_CLOSE, MCI_WAIT,
  320.                                                                                     (DWORD)(LPMCI_GENERIC_PARMS)&MciGenParm);
  321.                                         ReleaseDC (hWnd, hdc);
  322.                                         break;
  323.  
  324.                                 default:
  325.                                         break;
  326.                         }
  327.                     break;
  328.  
  329.                 case WM_DESTROY:
  330.                         PostQuitMessage(0);   // this is the end...
  331.                         break;
  332.  
  333.                 case WM_CLOSE:
  334.                         // Tell windows to destroy our window.
  335.                         DestroyWindow(hWnd);
  336.                         break;
  337.  
  338.                 default:
  339.                     // Let windows handle all messages we choose to ignore.
  340.                     return(DefWindowProc(hWnd, message, wParam, lParam));
  341.         }
  342.         return(0L);
  343. }
  344.