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