sound.c
上传用户:jlteech
上传日期:2007-01-06
资源大小:349k
文件大小:12k
源码类别:

压缩解压

开发平台:

Visual C++

  1. /* Author Mike White, 1996. Based on original WizUnZip code by
  2.  * Robert Heath.
  3.  */
  4. #include <stdio.h>
  5. #include "sound.h"
  6. #include "wiz.h"
  7. #include "helpids.h"
  8. #include <mmsystem.h>
  9. /* WiZ sound control module, sound.c.
  10.  * This module controls what sound WiZ makes after during or after extracting a file.
  11.  * All users can optionally produce a beep after unzipping. Users with Windows 3.1
  12.  * or Windows 3.0 + Multimedia extensions can play wave files.
  13.  * WiZ uses the presence of the DLL MMSYSTEM.DLL/WIN16MM.DLL to determine
  14.  * whether MM capability is present.  It further queries the number of wave
  15.  * devices in the system to see if wave playing hardware is present.
  16.  * This approach gives maximum system useability without causing Windows
  17.  * errors such as "Can't find dynalink!"
  18.  */
  19. #define MAXFILTERBUF 50
  20. /* sndPlaySound is apparently an obsolete function not properly supported
  21.    by Win32 (or perhaps by WINMM.DLL), use PlaySound instead. Of course,
  22.    PlaySound is not supported under Windows 3.1x
  23. */
  24. #ifndef WIN32
  25. static char __based(__segname("STRINGS_TEXT")) szMMSystemDll[] = "MMSYSTEM.DLL";
  26. static char __based(__segname("STRINGS_TEXT")) szPlaySound[] = "sndPlaySound";
  27. #else
  28. static char __based(__segname("STRINGS_TEXT")) szMMSystemDll[] = "WINMM.DLL";
  29. static char __based(__segname("STRINGS_TEXT")) szPlaySound[] = "PlaySound";
  30. #endif
  31. static char __based(__segname("STRINGS_TEXT")) szDfltWaveFile[] = "WIZUNZIP.WAV";
  32. static char __based(__segname("STRINGS_TEXT")) szSoundNameKey[] = "SoundName"; /* key in .INI */
  33. static char __based(__segname("STRINGS_TEXT")) szSoundOptKey[] = "SoundOption";
  34. static char __based(__segname("STRINGS_TEXT")) szWaveBrowseTitle[] = "Browse Sound Files";
  35. static char __based(__segname("STRINGS_TEXT")) gszFilter[MAXFILTERBUF] =
  36.                            "Wave Files (*.wav)*.wavAll Files (*.*)*.*";
  37. static char  *SoundOptsTbl[] = /* for .INI file */
  38.    {"none", "beep", "PlayDuring", "PlayAfter" };
  39. static HINSTANCE hinstMMSystem;     /* MMSystem/WINMM DLL instance */
  40. static FARPROC lpSndPlaySound;      /* pointer to SndPlaySound()/PlaySound() */
  41. static FARPROC lpWaveOutGetNumDevs; /* pointer to WaveOutGetNumDevs() */
  42. static BOOL CanPlayWave(void);
  43. static WORD uSoundButtonSelected = IDM_SOUND_NONE;
  44.                                     /* state of sound option */
  45. static WORD uSoundButtonSelectedTmp;/* state of sound option while in dialog box */
  46. /* Forward Refs
  47.  */
  48. static BOOL DoOpenFile(HWND hWnd, LPSTR lpDestFileName);
  49. static BOOL CanPlayWave(void);
  50. /* Test for Wave Playing Capability
  51.  */
  52. static BOOL CanPlayWave(void)
  53. {
  54. static bTestedForWave = FALSE; /* true if test for wave playing has been done */
  55. static bCanPlayWave = FALSE;   /* true if system can play wave   */
  56. int   nPrevErrorMode;         /* previous error mode               */
  57. if (bTestedForWave)         /* deja vu ? */
  58.    return(bCanPlayWave);
  59. bTestedForWave = TRUE;
  60. nPrevErrorMode = SetErrorMode(SEM_NOOPENFILEERRORBOX);
  61. hinstMMSystem = LoadLibrary(szMMSystemDll);
  62. SetErrorMode(nPrevErrorMode);
  63. #ifndef WIN32
  64. if (hinstMMSystem >  (HINSTANCE) HINSTANCE_ERROR)
  65. #else
  66. if (hinstMMSystem != NULL)
  67. #endif
  68.    {
  69.       /* If can't load the function which looks up no. wave out devices or
  70.        * number of wave output devices is zero, we can't play waves.
  71.        */
  72. #ifdef __BORLANDC__
  73. #pragma warn -pro
  74. #endif
  75.    if ((lpWaveOutGetNumDevs =
  76.       GetProcAddress(hinstMMSystem, "waveOutGetNumDevs")) == NULL ||
  77.       (*lpWaveOutGetNumDevs)() == 0 ||
  78.       (lpSndPlaySound =
  79.       GetProcAddress(hinstMMSystem, szPlaySound)) == NULL)
  80. #ifdef __BORLANDC__
  81. #pragma warn .pro
  82. #endif
  83.       {
  84.       FreeLibrary(hinstMMSystem);   /* unload library   */
  85.       }
  86.    else /* we're set to play waves */
  87.       {
  88.       bCanPlayWave = TRUE;   /* flag that we can play waves   */
  89.       }
  90.    }
  91. return bCanPlayWave;
  92. }
  93. /* Initialize Sound Options is called on WiZ start-up to read
  94.  * the sound option and sound name.
  95.  * Read the Sound Option to see if user wants beep, sound, or nothing.
  96.  * Read chosen sound name or wave file.
  97.  */
  98. void InitSoundOptions(void)
  99. {
  100.    GetPrivateProfileString(szAppName, szSoundOptKey, SoundOptsTbl[0],
  101.                      lpumb->szBuffer, PATH_MAX,
  102.                      szWiZIniFile);
  103.    lpumb->szBuffer[255] = '';   /* force truncation */
  104.    for (uSoundButtonSelected = IDM_SOUND_NONE;
  105.        uSoundButtonSelected < IDM_SOUND_WAVE_AFTER &&
  106.        lstrcmpi(lpumb->szBuffer,
  107.           SoundOptsTbl[uSoundButtonSelected-IDM_SOUND_NONE]) ;
  108.        uSoundButtonSelected++)
  109.       ;
  110.    /* Do range check on sound option. Set to none if necessary.
  111.     */
  112.    if (uSoundButtonSelected > IDM_SOUND_WAVE_AFTER ||
  113.       (uSoundButtonSelected > IDM_SOUND_BEEP &&
  114.        !CanPlayWave()))
  115.       uSoundButtonSelected = IDM_SOUND_NONE;
  116.    GetPrivateProfileString(szAppName, szSoundNameKey, szDfltWaveFile,
  117.                      lpumb->szSoundName, PATH_MAX,
  118.                      szWiZIniFile);
  119. }
  120. /* Play Sound During extraction, test, or display  if requested.
  121.  * Don't use a default if nothing specified.
  122.  */
  123. void WINAPI SoundDuring(void)
  124. {
  125.    if (uSoundButtonSelected == IDM_SOUND_WAVE_DURING && CanPlayWave())
  126.       {
  127. #ifdef __BORLANDC__
  128. #pragma warn -pro
  129. #endif
  130. #ifdef WIN32
  131.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP|SND_NODEFAULT);
  132. #else
  133.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP|SND_NODEFAULT);
  134. #endif
  135. #ifdef __BORLANDC__
  136. #pragma warn .pro
  137. #endif
  138.       }
  139. }
  140. /* Play Sound After extraction, test, or display  if requested.
  141.  */
  142. void SoundAfter(void)
  143. {
  144.    switch (uSoundButtonSelected) {
  145.    case IDM_SOUND_BEEP:
  146.       MessageBeep(1);
  147.       break;
  148.    case IDM_SOUND_WAVE_AFTER:
  149.       if (CanPlayWave())
  150. #ifdef __BORLANDC__
  151. #pragma warn -pro
  152. #endif
  153. #ifdef WIN32
  154.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP);
  155. #else
  156.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP);
  157. #endif
  158. #ifdef __BORLANDC__
  159. #pragma warn .pro
  160. #endif
  161.       break;
  162.    }
  163. }
  164. /* DoFileOpen Dialog calls the common dialog function GetOpenFileName()
  165.  * to browse and select a table file.
  166.  */
  167. static BOOL DoOpenFile(HWND hWnd, LPSTR lpDestFileName)
  168. {
  169. BOOL fRetCode = FALSE;   /* assume failure            */
  170. /* DWORD dwExtdError; Not used */
  171.          memset(&lpumb->wofn, '', sizeof(OPENFILENAME));
  172.          lpumb->wofn.lStructSize       = sizeof(OPENFILENAME);
  173.          lpumb->wofn.hwndOwner         = (HWND)hWnd;
  174.          lpumb->wofn.hInstance         = (HANDLE)NULL;
  175.          lpumb->wofn.lpstrFilter       = gszFilter;
  176.          lpumb->wofn.lpstrCustomFilter = (LPSTR)NULL;
  177.          lpumb->wofn.nMaxCustFilter    = 0L;
  178.          lpumb->wofn.nFilterIndex      = 1L;
  179.          lpumb->wofn.lpstrFile         = lpDestFileName;
  180.          lpDestFileName[0]               = '';
  181.          lpumb->wofn.nMaxFile          = (DWORD)PATH_MAX;
  182.          lpumb->wofn.lpstrFileTitle    = (LPSTR)NULL;
  183.          lpumb->wofn.nMaxFileTitle     = 0;
  184.          lpumb->wofn.lpstrInitialDir   = (LPSTR)NULL;
  185.          lpumb->wofn.lpstrTitle        = (LPSTR)szWaveBrowseTitle;
  186.          lpumb->wofn.Flags             = OFN_HIDEREADONLY|
  187.                                          OFN_PATHMUSTEXIST|
  188.                                          OFN_FILEMUSTEXIST|
  189.                                          OFN_NOCHANGEDIR|
  190.                                          OFN_SHOWHELP ;
  191.          lpumb->wofn.nFileOffset       = 0;
  192.          lpumb->wofn.nFileExtension    = 0;
  193.          lpumb->wofn.lpstrDefExt       = "WAV";
  194.          lpumb->wofn.lCustData         = 0L;
  195.          lpumb->wofn.lpTemplateName    = (LPSTR)NULL;
  196.    if ( GetOpenFileName( &(lpumb->wofn) ) )
  197.       {
  198.         HFILE   hFile;
  199.         OFSTRUCT ofstruct;
  200.         hFile=OpenFile(lpumb->wofn.lpstrFile, &ofstruct, OF_EXIST);
  201.         if (hFile != -1)
  202.         {
  203.           fRetCode = TRUE;
  204.         }
  205. /* NOTE!!!  On a closed system (ie, not running on a network)
  206.  * this OpenFile call should NEVER fail.  This because we passed in the
  207.  * OFN_FILEMUSTEXIST flag to CD.  However, on a network system,
  208.  * there is a *very* small chance that between the time CD's checked
  209.  * for existance of the file and the time the call to OpenFile
  210.  * was made here, someone else on the network has deleted the file.
  211.  * MORAL: ALWAYS, ALWAYS, ALWAYS check the return code from your
  212.  * call to OpenFile() or _lopen.
  213.  */
  214.       }
  215.       else /* dialog failed */
  216.       {
  217. /*         dwExtdError = CommDlgExtendedError();   dwExtdError code not used */
  218.          CommDlgExtendedError();   /* get error code */
  219.       }
  220.    return(fRetCode);         /* return indication      */
  221. }
  222. /****************************************************************************
  223.     FUNCTION: SoundProc(HWND, unsigned, WPARAM, LPARAM)
  224.     PURPOSE:  Processes messages for "Sound Options" dialog box of WiZ
  225.     AUTHOR:   Robert A. Heath  2/15/93    Public Domain -- help yourself.
  226.     MESSAGES:
  227.     WM_INITDIALOG - initialize dialog box
  228.     WM_COMMAND    - Input received
  229. ****************************************************************************/
  230. #ifdef __BORLANDC__
  231. #pragma argsused
  232. #endif
  233. BOOL WINAPI
  234. SoundProc(HWND hwndDlg, WORD wMessage, WPARAM wParam, LPARAM lParam)
  235. {
  236. static HWND hSoundWaveDuring, hSoundWaveAfter, hFileText, hSoundEdit, hPlay, hBrowse;
  237. static UINT uMaxSoundRadioButton; /* upper boundary of uSoundButtonSelected   */
  238.    switch (wMessage) {
  239.    case WM_INITDIALOG:
  240.       if (CanPlayWave())
  241.       {
  242.          hSoundWaveDuring = GetDlgItem(hwndDlg, IDM_SOUND_WAVE_DURING);
  243.          hSoundWaveAfter = GetDlgItem(hwndDlg, IDM_SOUND_WAVE_AFTER);
  244.          hFileText = GetDlgItem(hwndDlg, IDM_SOUND_FILE_TEXT);
  245.          hSoundEdit = GetDlgItem(hwndDlg, IDM_SOUND_EDIT);
  246.          hPlay = GetDlgItem(hwndDlg, IDM_SOUND_PLAY);
  247.          hBrowse = GetDlgItem(hwndDlg, IDM_SOUND_BROWSE);
  248.          EnableWindow(hSoundWaveDuring, TRUE);
  249.          EnableWindow(hSoundWaveAfter, TRUE);
  250.          WinAssert(hFileText);
  251.          EnableWindow(hFileText, TRUE);
  252.          EnableWindow(hSoundEdit, TRUE);
  253.          EnableWindow(hPlay, TRUE);
  254.          EnableWindow(hBrowse, TRUE);
  255.          SetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName);
  256.          uMaxSoundRadioButton = IDM_SOUND_WAVE_AFTER;
  257.       }
  258.       else   /* Can't play wave */
  259.       {
  260.          uMaxSoundRadioButton = IDM_SOUND_BEEP;
  261.       }
  262.       uSoundButtonSelectedTmp = uSoundButtonSelected; /* initialize temp value */
  263.       CheckRadioButton(hwndDlg, IDM_SOUND_NONE, uMaxSoundRadioButton, uSoundButtonSelectedTmp);
  264.       CenterDialog(GetParent(hwndDlg), hwndDlg);
  265.       return TRUE;
  266.    case WM_COMMAND:
  267.       switch (LOWORD(wParam)) {
  268.       case IDM_SOUND_NONE:
  269.       case IDM_SOUND_BEEP:
  270.       case IDM_SOUND_WAVE_DURING:
  271.       case IDM_SOUND_WAVE_AFTER:
  272.          uSoundButtonSelectedTmp = LOWORD(wParam);
  273.          CheckRadioButton(hwndDlg, IDM_SOUND_NONE, uMaxSoundRadioButton,
  274.                         uSoundButtonSelectedTmp);
  275.          break;
  276.       case IDM_SOUND_PLAY:
  277.          GetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName, PATH_MAX);
  278. #ifdef __BORLANDC__
  279. #pragma warn -pro
  280. #endif
  281. #ifdef WIN32
  282.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, NULL, SND_ASYNC|SND_NOSTOP);
  283. #else
  284.       (*lpSndPlaySound)((LPSTR)lpumb->szSoundName, SND_ASYNC|SND_NOSTOP);
  285. #endif
  286. #ifdef __BORLANDC__
  287. #pragma warn .pro
  288. #endif
  289.          break;
  290.       case IDM_SOUND_BROWSE:
  291.          if (DoOpenFile(hwndDlg, lpumb->szSoundName))
  292.          {
  293.             /* transfer to command window          */
  294.                 SetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName);
  295.          }
  296.          break;
  297.        case IDOK:
  298.          uSoundButtonSelected = uSoundButtonSelectedTmp;
  299.          WritePrivateProfileString(szAppName, szSoundOptKey,
  300.             SoundOptsTbl[uSoundButtonSelected-IDM_SOUND_NONE], szWiZIniFile);
  301.          GetDlgItemText(hwndDlg, IDM_SOUND_EDIT, lpumb->szSoundName, PATH_MAX);
  302.          WritePrivateProfileString(szAppName, szSoundNameKey,
  303.                      lpumb->szSoundName,   szWiZIniFile);
  304.          EndDialog(hwndDlg, TRUE);
  305.          break;
  306.       case IDCANCEL:
  307.          /* restore former value of sound file name
  308.           */
  309.          GetPrivateProfileString(szAppName, szSoundNameKey, szDfltWaveFile,
  310.                      lpumb->szSoundName, PATH_MAX,
  311.                      szWiZIniFile);
  312.          EndDialog(hwndDlg, FALSE);
  313.          break;
  314.       case IDM_SOUND_HELP:
  315.             WinHelp(hwndDlg,szHelpFileName,HELP_CONTEXT, (DWORD)(HELPID_SOUND_OPTIONS));
  316.          break;
  317.       }
  318.       return TRUE;
  319.    }
  320.    return FALSE;
  321. }