How can I use PlaySound or play a .WAV file in a Visual C++ Windows Forms Application (.NET) Project

  • Thread starter Thread starter Ángel Cabrera
  • Start date Start date
Á

Ángel Cabrera

I'm using Visual Studio .NET 2003.

I get a "Form1.h(242): error C2065: 'PlaySound' : undeclared identifier"
error all the time, I've been looking in MSDN for a couple of hours with no
luck, no idea, it's my first program, if you are so kind and could explain
me in detail.

Sorry for my english.

Regards.
 
Sure.

Form1.h:

-----

#pragma once


namespace sound
{
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
///
/// WARNING: If you change the name of this class, you will need to
change the
/// 'Resource File Name' property for the managed resource
compiler tool
/// associated with all .resx files this class depends on.
Otherwise,
/// the designers will not be able to interact properly
with localized
/// resources associated with this form.
/// </summary>
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Button * button1;

private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container * components;

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(8, 8);
this->button1->Name = S"button1";
this->button1->TabIndex = 0;
this->button1->Text = S"button1";
this->button1->Click += new System::EventHandler(this,
button1_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = S"Form1";
this->Text = S"Form1";
this->ResumeLayout(false);

}

private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
PlaySound("SOUND.WAV", NULL, SND_SYNC);
}

};
}

-----

Form1.cpp:

-----

#include "stdafx.h"
#include "Form1.h"
#include <windows.h>
#include <mmsystem.h>

using namespace sound;

int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
System::Threading::Thread::CurrentThread->ApartmentState =
System::Threading::ApartmentState::STA;
Application::Run(new Form1());
return 0;
}
 
Back
Top