Windows Programming: Resources and playing sounds

Source: Internet
Author: User

To play a sound, add an item: Winmm.lib, and then include the header file: #include <mmsystem.h>

Play sound with the PlaySound function, only MIDI and WAV waveform files can be played.

#defineWin32_lean_and_mean#include<windows.h>#include<windowsx.h>#include<mmsystem.h>#include<stdio.h>#include<stdlib.h>#include<math.h>#include"MenuSoundRes.h"#defineWindow_class_name "WINCLASS1"//Global VariablesHWND Main_window_handle =NULL; HINSTANCE Hinstance_app=NULL;//window processing FunctionsLRESULT CALLBACK WindowProc (HWND hwnd, UINT MSG, WPARAM WPARAM, LPARAM lpram) {paintstruct ps;    HDC hdc;    Rect rect; Switch(msg) { Casewm_create: {return 0; } Break;  Casewm_command: {Switch(LoWord (WParam)) { Casemenu_file_id_exit: {postquitmessage (0); } Break;  Casemenu_help_about: {MessageBox (hwnd,"Welcome to the Sound Demo program","Sound", MB_OK |mb_iconexclamation); } Break;  CaseMenu_play_id_beam: {PlaySound (Makeintresource (Sound_id_beam), Hinstance_app, Snd_resource|Snd_async); } Break;  CaseMenu_play_id_teleport: {PlaySound (Makeintresource (Sound_id_teleport), Hinstance_app, Snd_resource |Snd_async); } Break; default:             Break; }    } Break;  Casewm_paint: {hdc=GetDC (HWND);        ReleaseDC (hwnd, HDC); GetClientRect (hwnd,&rect); ValidateRect (hwnd,&rect); return 0; } Break;  Casewm_close: {if(Idyes! = MessageBox (hwnd,"Are you sure you want to exit the application? ","Exit", Mb_yesno |mb_iconexclamation)) {            return 0; }    } Break;  Casewm_size: {} Break;  CaseWm_destroy: {postquitmessage (0); return 0; } Break; default: Break; }    returnDefWindowProc (hwnd, MSG, WParam, Lpram);}voidGamemain () {return;}intWINAPI WinMain (hinstance hinstance, hinstance hprevinstance, LPSTR lpCmdLine,intncmdshow) {    //Defining window ClassesWndclassex Winclass;    HWND hwnd;    MSG msg; //populate the members of the window classWinclass.cbsize =sizeof(Wndclassex); Winclass.style= Cs_dblclks | CS_OWNDC | Cs_hredraw |Cs_vredraw; Winclass.lpfnwndproc= WindowProc;//window Message handler functionWinclass.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;//window class nameWINCLASS.HICONSM =LoadIcon (NULL, Makeintresource (Icon_menusound)); //saving an instance handle to a global variableHinstance_app =hinstance; //Register window class    if(! RegisterClassEx (&Winclass)) {        return 0; }    //create a member of a window class    if(! (HWnd =CreateWindowEx (NULL, Window_class_name,"Hello World", Ws_overlappedwindow|ws_visible, $, $,        640,480, NULL, NULL, HINSTANCE, NULL))) {        return 0; } HMENU Hmenuhandle= LoadMenu (HINSTANCE,"Soundmenu");    SetMenu (HWnd, Hmenuhandle); //Save the form handle to a global variableMain_window_handle =hWnd; //Get device contextHDC HDC =GetDC (HWND); //message Loops     while(TRUE) {if(PeekMessage (&msg, HWnd,0,0, Pm_remove)) {            if(Msg.message = =wm_quit) {                 Break; } translatemessage (&msg); DispatchMessage (&msg); }        //SetTextColor (HDC, RGB (rand ()%, rand ()%, rand ()% 256)); //SetBkColor (hdc, RGB (0,0,0)); //SetBkMode (hdc, TRANSPARENT); //TextOut (HDC, rand ()% 640, rand ()% 480, "Hello", strlen ("Hello")); //Gamemain (); //Sleep (ten);} releasedc (HWND,HDC); returnMsg.wparam;}

Resource header File

//Sound Resources#defineSound_id_beam 1#defineSound_id_teleport 2//cursor Resources and icon Resources#defineIcon_menusound 100#defineCursor_crossshair 200//Menu Resources#defineMenu_file_id_exit 1000#defineMenu_play_id_beam 2000#defineMenu_play_id_teleport 2001#defineMenu_help_about 3000//string Resource#defineId_string_file 10000#defineId_string_play 20000#defineId_string_exit 30000

. RC file:

#include"MenuSoundRes.h"icon_menusound ICON T3dx.icocursor_crossshair CURSOR crosshair.cursound_id_beam Wave Beam.wavsound_id_teleport Wave Teleport. WAV//A string resource, without a name, can have only one string resource in a resource file. stringtable{Id_string_file,"file"Id_string_play,"Play"Id_string_exit,"Exit"}//Menu Resourcessoundmenu 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 Programming: Resources and playing sounds

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.