首页 > 代码库 > windows编程:资源和播放声音

windows编程:资源和播放声音

要播放声音,要附加项:winmm.lib,然后包含头文件:#include <mmsystem.h>

播放声音用PlaySound函数,只能播放midi和wav波形文件。

 

#define WIN32_LEAN_AND_MEAN#include <windows.h>#include <windowsx.h>#include <mmsystem.h>#include <stdio.h>#include <stdlib.h>#include <math.h>#include "MenuSoundRes.h"#define WINDOW_CLASS_NAME "WINCLASS1"//全局变量HWND main_window_handle = NULL;HINSTANCE hInstance_App = NULL;//窗口处理函数LRESULT CALLBACK WindowProc(HWND hwnd,    UINT msg,    WPARAM wParam,    LPARAM lPram){    PAINTSTRUCT ps;    HDC hdc;    RECT rect;    switch (msg)    {    case WM_CREATE:    {                      return 0;    }break;    case WM_COMMAND:    {        switch (LOWORD(wParam))        {        case MENU_FILE_ID_EXIT:        {            PostQuitMessage(0);        }break;        case MENU_HELP_ABOUT:        {            MessageBox(hwnd, "欢迎使用声音演示程序", "声音", MB_OK | MB_ICONEXCLAMATION);        }break;        case MENU_PLAY_ID_BEAM:        {            PlaySound(MAKEINTRESOURCE(SOUND_ID_BEAM), hInstance_App, SND_RESOURCE | SND_ASYNC);        }break;        case MENU_PLAY_ID_TELEPORT:        {            PlaySound(MAKEINTRESOURCE(SOUND_ID_TELEPORT), hInstance_App, SND_RESOURCE | SND_ASYNC);        }break;        default:            break;        }    }break;    case WM_PAINT:    {        hdc = GetDC(hwnd);        ReleaseDC(hwnd, hdc);        GetClientRect(hwnd, &rect);        ValidateRect(hwnd, &rect);        return 0;    }break;    case WM_CLOSE:    {        if (IDYES != MessageBox(hwnd, "确实要退出应用程序?", "退出", MB_YESNO | MB_ICONEXCLAMATION))        {            return 0;        }    }break;    case WM_SIZE:    {    }break;    case WM_DESTROY:    {        PostQuitMessage(0);        return 0;    }break;    default:break;    }    return DefWindowProc(hwnd, msg, wParam, lPram);}void GameMain(){    return;}int WINAPI WinMain(HINSTANCE hInstance,    HINSTANCE hPrevInstance,    LPSTR lpCmdLine,    int nCmdShow){    //定义窗口类    WNDCLASSEX winClass;    HWND hWnd;    MSG msg;    //填充窗口类的各成员    winClass.cbSize = sizeof(WNDCLASSEX);    winClass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;    winClass.lpfnWndProc = WindowProc;    //窗口消息处理函数    winClass.cbClsExtra = 0;    winClass.cbWndExtra = 0;    winClass.hInstance = hInstance;    winClass.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICON_MENUSOUND));    winClass.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(CURSOR_CROSSSHAIR));    winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);    winClass.lpszMenuName = NULL;    winClass.lpszClassName = WINDOW_CLASS_NAME;        //窗口类名    winClass.hIconSm = LoadIcon(NULL, MAKEINTRESOURCE(ICON_MENUSOUND));    //保存实例句柄到全局变量    hInstance_App = hInstance;    //注册窗口类    if (!RegisterClassEx(&winClass))    {        return 0;    }    //创建窗口类的一个成员    if (!(hWnd = CreateWindowEx(NULL,        WINDOW_CLASS_NAME,        "hello world",        WS_OVERLAPPEDWINDOW | WS_VISIBLE,        200, 200,        640, 480,        NULL,        NULL,        hInstance,        NULL)))    {        return 0;    }    HMENU hMenuhandle = LoadMenu(hInstance, "SoundMenu");    SetMenu(hWnd, hMenuhandle);    //保存窗体句柄到全局变量中    main_window_handle = hWnd;    //得到设备上下文    HDC hdc = GetDC(hWnd);    //消息循环    while (TRUE)    {        if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))        {            if (msg.message == WM_QUIT)            {                break;            }            TranslateMessage(&msg);            DispatchMessage(&msg);        }        //SetTextColor(hdc, RGB(rand() % 256, rand() % 256, rand() % 256));        //SetBkColor(hdc, RGB(0,0,0));        //SetBkMode(hdc, TRANSPARENT);        //TextOut(hdc, rand() % 640, rand() % 480, "hello", strlen("hello"));        //GameMain();        //Sleep(10);    }    ReleaseDC(hWnd,hdc);    return msg.wParam;}

 

资源头文件

//声音资源#define SOUND_ID_BEAM        1#define SOUND_ID_TELEPORT    2//光标资源和图标资源#define ICON_MENUSOUND        100#define CURSOR_CROSSSHAIR    200//菜单资源#define MENU_FILE_ID_EXIT        1000#define MENU_PLAY_ID_BEAM        2000#define MENU_PLAY_ID_TELEPORT    2001#define MENU_HELP_ABOUT            3000//字符串资源#define ID_STRING_FILE            10000#define ID_STRING_PLAY            20000#define ID_STRING_EXIT            30000

.RC文件:

#include "MenuSoundRes.h"ICON_MENUSOUND            ICON        t3dx.icoCURSOR_CROSSSHAIR        CURSOR        crosshair.curSOUND_ID_BEAM            WAVE        beam.wavSOUND_ID_TELEPORT        WAVE        TELEPORT.WAV//字符串资源,没有名字,一个资源文件中只能有一个字符串资源。STRINGTABLE{    ID_STRING_FILE,"文件"    ID_STRING_PLAY,"播放"    ID_STRING_EXIT,"退出"}//菜单资源SoundMenu MENU    DISCARDABLE{    POPUP "File"    {        MENUITEM    "E&xit"            MENU_FILE_ID_EXIT    }    POPUP "&PlaySound"    {        MENUITEM    "BEAM"            MENU_PLAY_ID_BEAM        MENUITEM    "TELEPORT"        MENU_PLAY_ID_TELEPORT    }    POPUP "Help"    {        MENUITEM    "About"            MENU_HELP_ABOUT    }}

 

windows编程:资源和播放声音