iSaber-
2021-09-17 11:48
采纳率: 66.7%
浏览 43

如何让多个beep函数同时发出声音

我想实现按下一个键就能发出一个声音,调用的是beep函数,但是我给每个beep都开了一个线程但是他们还是一个音接着另一个音播放没有同时进行

#define _CRT_SECURE_NO_WARNINGS
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "resource.h"
HANDLE outputhandle = 0;
HINSTANCE Inst = 0;

void OnSize(HWND hWnd, LPARAM lParam)
{
    short width = LOWORD(lParam);
    short hight = HIWORD(lParam);
    char str[100];
    sprintf(str, "width = %d, hight = %d", width, hight);
    WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
}

//发声函数
DWORD CALLBACK soundProc(LPVOID pParam)
{
    WPARAM wParam = (WPARAM)pParam;
    char str[40];
    sprintf(str, "Press key%d \n", (int)wParam);
    //Sleep(5000);
    WriteConsole(outputhandle, str, strlen(str), NULL, NULL);
    int r = rand() % 2000 + 300;
    Beep(r, 200);
    ExitThread(0);
    return 0;
}

//一按键就开一个线程
void OnKeyDown(HWND hWnd, WPARAM wParam)
{
    DWORD nID;
    HANDLE hThread = CreateThread(NULL, 1, soundProc, (void*)wParam, 0, &nID);
    return;
}

LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msgID, WPARAM wParam, LPARAM lParam)
{
    switch (msgID)
    {
    case WM_SYSCOMMAND:
        if (wParam == SC_CLOSE)
            EndDialog(hWndDlg, 0);
    }
    return FALSE;
}

void OnCommand(HWND hWnd, WPARAM wParam)
{
    switch (LOWORD(wParam))
    {
    case ID_MOD:
        DialogBox(Inst, (char*)IDD_DIALOG1, hWnd, DlgProc);
        break;
    }
}

//处理窗口消息             窗口句柄    消息ID       附带参数  
LRESULT APIENTRY WindProc(HWND hWnd, UINT msgID, WPARAM wParam, LPARAM IParam)
{
    switch (msgID)
    {
    /*case WM_SIZE:
        OnSize(hWnd, IParam);
        break;*/
    case WM_COMMAND:
        OnCommand(hWnd, wParam);
        break;
    case WM_KEYDOWN:
        OnKeyDown(hWnd, wParam);
        break;
    case WM_SYSKEYDOWN:
        OnKeyDown(hWnd, wParam);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_SYSCOMMAND:
        if (wParam == SC_CLOSE)
        {
            int ret = MessageBox(hWnd, "是否退出", "退出", MB_YESNO);
            if (ret == IDYES)
                break;
            else
                return 0;
        }
        break;
    }

    return DefWindowProc(hWnd, msgID, wParam, IParam);
}


//入口函数           本进程的句柄         上一个进程的句柄        cmd命令行参数     窗口展示形式
int WINAPI WinMain( HINSTANCE hIns,  HINSTANCE hPreIns, LPSTR lpCmdline, int nCmdShow)
{
    //分配一个cmd
    AllocConsole();
    outputhandle = GetStdHandle(STD_OUTPUT_HANDLE);
    Inst = hIns;
    //注册窗口类
    WNDCLASS  wc = { 0 };
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.hCursor = NULL;
    wc.hIcon = NULL;
    wc.hInstance = hIns;
    wc.lpfnWndProc = WindProc;
    wc.lpszClassName = "Main";
    //wc.lpszMenuName = (char*)IDR_MENU1;_In_opt;
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;

    HMENU hMenu = LoadMenu(hIns, (char*)IDR_MENU1);

    RegisterClass(&wc);
    HWND hWnd = CreateWindowExW(0, L"Main", L"windows", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, NULL, hMenu, hIns, NULL);

    ShowWindow(hWnd, SW_SHOW);
    UpdateWindow(hWnd);

    MSG nMsg = { 0 };

    while (1)
    {
        if (PeekMessage(&nMsg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (GetMessage(&nMsg, NULL, 0, 0))
            {
                TranslateMessage(&nMsg);
                DispatchMessageW(&nMsg);
            }
            else
                return 0;
        }
    }

    return 0;
}


  • 好问题 提建议
  • 收藏

2条回答 默认 最新

  • 赵4老师 2021-09-17 17:30
    已采纳
    
    PlaySound
    The PlaySound function plays a sound specified by the given filename, resource, or system event. (A system event may be associated with a sound in the registry or in the WIN.INI file.) 
    
    BOOL PlaySound(
      LPCSTR pszSound,  
      HMODULE hmod,     
      DWORD fdwSound    
    );
     
    Parameters
    pszSound 
    A string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter. 
    Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename. 
    
    hmod 
    Handle of the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound. 
    fdwSound 
    Flags for playing the sound. The following values are defined: 
    SND_APPLICATION 
    The sound is played using an application-specific association. 
    SND_ALIAS 
    The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE. 
    SND_ALIAS_ID 
    The pszSound parameter is a predefined sound identifier. 
    SND_ASYNC 
    The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL. 
    SND_FILENAME 
    The pszSound parameter is a filename. 
    SND_LOOP 
    The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event. 
    SND_MEMORY 
    A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory. 
    SND_NODEFAULT 
    No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound. 
    SND_NOSTOP 
    The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound. 
    If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound. 
    
    SND_NOWAIT 
    If the driver is busy, return immediately without playing the sound. 
    SND_PURGE 
    Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped. 
    You must also specify the instance handle to stop SND_RESOURCE events. 
    
    SND_RESOURCE 
    The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource. 
    SND_SYNC 
    Synchronous playback of a sound event. PlaySound returns after the sound event completes. 
    Return Values
    Returns TRUE if successful or FALSE otherwise.
    
    Remarks
    The sound specified by pszSound must fit into available physical memory and be playable by an installed waveform-audio device driver. PlaySound searches the following directories for sound files: the current directory; the Windows directory; the Windows system directory; directories listed in the PATH environment variable; and the list of directories mapped in a network. For more information about the directory search order, see the documentation for the OpenFile function.
    
    If it cannot find the specified sound, PlaySound uses the default system event sound entry instead. If the function can find neither the system default entry nor the default sound, it makes no sound and returns FALSE.
    
    QuickInfo
      Windows NT: Requires version 3.1 or later.
      Windows: Requires Windows 95 or later.
      Windows CE: Unsupported.
      Header: Declared in mmsystem.h.
      Import Library: Use winmm.lib.
      Unicode: Implemented as Unicode and ANSI versions on Windows NT.
    
    See Also
    Waveform Audio Overview, Waveform Functions 
    
     
    
    
    评论
    解决 1 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题