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

如何让多个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条回答 默认 最新

相关推荐 更多相似问题