I've got the code (using VS2010, C++, Windows Forms Application) below:
#pragma once
#include <cv.h>
#include <highgui.h>
#ifdef _DEBUG
//debug
#pragma comment(lib,"cv210d.lib")
#pragma comment(lib,"cxcore210d.lib")
#pragma comment(lib,"cvaux210d.lib")
#pragma comment(lib,"highgui210d.lib")
#else
//release
#pragma comment(lib,"cv210.lib")
#pragma comment(lib,"cxcore210.lib")
#pragma comment(lib,"cvaux210.lib")
#pragma comment(lib,"highgui210.lib")
#endif
//Global variables
IplImage* src = NULL;
IplImage* hsv = NULL;
IplImage* dst = NULL;
IplImage* v_plane = NULL;
IplImage* h_plane = NULL;
IplImage* s_plane = NULL;
namespace HW4 {
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
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:
private: System::Windows::Forms::PictureBox^ pictureBox2;
private: System::Windows::Forms::Button^ ExitButton;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox());
this->ExitButton = (gcnew System::Windows::Forms::Button());
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->BeginInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->BeginInit();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->Location = System::Drawing::Point(12, 49);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(255, 212);
this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
//
// pictureBox2
//
this->pictureBox2->Location = System::Drawing::Point(354, 49);
this->pictureBox2->Name = L"pictureBox2";
this->pictureBox2->Size = System::Drawing::Size(255, 212);
this->pictureBox2->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pictureBox2->TabIndex = 1;
this->pictureBox2->TabStop = false;
//
// ExitButton
//
this->ExitButton->Location = System::Drawing::Point(272, 278);
this->ExitButton->Name = L"ExitButton";
this->ExitButton->Size = System::Drawing::Size(64, 31);
this->ExitButton->TabIndex = 2;
this->ExitButton->Text = L"Exit";
this->ExitButton->UseVisualStyleBackColor = true;
this->ExitButton->Click += gcnew System::EventHandler(this, &Form1::ExitButton_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(635, 333);
this->Controls->Add(this->ExitButton);
this->Controls->Add(this->pictureBox2);
this->Controls->Add(this->pictureBox1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox1))->EndInit();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^ >(this->pictureBox2))->EndInit();
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
src = cvLoadImage("D:\\Pictures\\hungry.jpg");
dst = cvCreateImage(cvGetSize(src),8,3);
hsv = cvCreateImage(cvGetSize(src),8,3);
h_plane = cvCreateImage(cvGetSize(src),8,1);
s_plane = cvCreateImage(cvGetSize(src),8,1);
v_plane = cvCreateImage(cvGetSize(src),8,1);
cvCvtColor(src,hsv,CV_BGR2HSV);
cvSplit(hsv,h_plane,s_plane,v_plane,0);
cvEqualizeHist(v_plane,v_plane);
cvMerge(h_plane,s_plane,v_plane,0,hsv);
cvCvtColor(hsv,dst,CV_HSV2BGR);
//Before you debug, choose SizeMode property of PictureBoxs = StretchImage.
pictureBox1->Image = gcnew //replacement of cvShowImage
System::Drawing::Bitmap(src->width,src->height,src->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) src->imageData);
pictureBox1->Refresh();
pictureBox2->Image = gcnew //replacement of cvShowImage
System::Drawing::Bitmap(dst->width,dst->height,dst->widthStep,
System::Drawing::Imaging::PixelFormat::Format24bppRgb,(System::IntPtr) dst->imageData);
pictureBox2->Refresh();
/*cvReleaseImage(&src);
cvReleaseImage(&hsv);
cvReleaseImage(&dst);
cvReleaseImage(&h_plane);
cvReleaseImage(&s_plane);
cvReleaseImage(&v_plane);*/
}
private: System::Void ExitButton_Click(System::Object^ sender, System::EventArgs^ e) {
cvReleaseImage(&src);
cvReleaseImage(&hsv);
cvReleaseImage(&dst);
cvReleaseImage(&h_plane);
cvReleaseImage(&s_plane);
cvReleaseImage(&v_plane);
this->Close();
}
};
}
Consider the part of code:
cvReleaseImage(&src);
cvReleaseImage(&hsv);
cvReleaseImage(&dst);
cvReleaseImage(&h_plane);
cvReleaseImage(&s_plane);
cvReleaseImage(&v_plane);
if i put this part at the end of the Form1_Load, there'll be an error as the following picture:
However, if i put this part in the ExitButton_Click function, it's ok!
i don't know why ? hope someone can give me an explaination ! thanks for your help !^~^
In the following line you are using opencv image buffers and if you fee these buffers in the end of form load function, .net engine could not load image anymore
But if you free the buffer in form exit, it won't care ...
pictureBox1->Image = gcnew ...
The Form-Load event in .Net can be used to allocate resources before the form is loaded and rendered. By freeing the image buffer at the end of the load, you remove the image resource that the form needs to display. Just imagine that there is some internal method that calls your Load() method and then it calls an internal Display() method, which uses data from the Load() method. If you load the data and then release it before the "Display()" method is called, you are freeing memory that is required in the Display() method.
As Roozbeh said, if you put it in the exit, the form is not trying to display anything so it doesn't need that image resource (from the buffer). The exit method is naturally a good place to deallocate things.
Related
I am making an app that has a button that attaches to a running process but when I try to use windows.h functions eg. FindWindowA it says unresolved token (0A000038) extern C.
if working correctly it should attach to process when the button is pressed and then run some code that i havent coded in yet
Here is the button code
#pragma once
#include<Windows.h>
namespace projname {
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 MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ injectButton;
protected:
private: System::Windows::Forms::TextBox^ windowName;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->injectButton = (gcnew System::Windows::Forms::Button());
this->windowName = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// injectButton
//
this->injectButton->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 48, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->injectButton->Location = System::Drawing::Point(12, 384);
this->injectButton->Name = L"injectButton";
this->injectButton->Size = System::Drawing::Size(537, 87);
this->injectButton->TabIndex = 0;
this->injectButton->Text = L"Inject";
this->injectButton->UseVisualStyleBackColor = true;
this->injectButton->Click += gcnew System::EventHandler(this, &MyForm::injectButton_Click);
//
// windowName
//
this->windowName->Location = System::Drawing::Point(13, 358);
this->windowName->Name = L"windowName";
this->windowName->Size = System::Drawing::Size(536, 20);
this->windowName->TabIndex = 1;
this->windowName->Text = L"Window Name";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(561, 483);
this->Controls->Add(this->windowName);
this->Controls->Add(this->injectButton);
this->Name = L"MyForm";
this->Text = L"projname";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void injectButton_Click(System::Object^ sender, System::EventArgs^ e) {
HWND hwnd = FindWindowA(NULL, "window to attach to");
if (hwnd == NULL) { exit(-1); }
else {
DWORD procID;
GetWindowThreadProcessId(hwnd, &procID);
HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, procID);
if (procID == NULL) { exit(-1); }
}
}
};
}
If you add kernel32.lib and a few others to linker it works if you need more help search up
unresolved token (0A000038) extern C and there is a stack overflow question
I'm working on a C++/CLI project and I'm new in it.In my project I have 2 forms, one is mainpage and the other one is Loginpage.
I want to have an event that when it logins successfully in the Loginpage, starts to get some jpeg pictures from the server.
I have defined the delegate, event and the method for it as below. It compiles completely and runs, but when the event is fired, it doesn't run the method.
I don't find an answer for it. I was wondering if you could help me?
MainPage.h:
#pragma once
#include "IPPort.h"
#include "Login.h"
#include "stdafx.h"
#include "Utils.h"
namespace UIv10 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class MainPage : public System::Windows::Forms::Form
{
public:
MainPage(void)
{
InitializeComponent();
Login ^ loginpage = gcnew Login();
loginpage->JPEGgenerate += gcnew UIv10::Login::JPEGgeneratorEventHandler(this, &MainPage::OnJPEGgenerate);
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MainPage()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ fileToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ connectToolStripMenuItem;
private: System::Windows::Forms::ToolStrip^ toolStrip1;
private: System::Windows::Forms::ToolStripButton^ toolStripButton1;
private: System::Windows::Forms::PictureBox^ pictureBox1;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(MainPage::typeid));
this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->fileToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->connectToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->toolStrip1 = (gcnew System::Windows::Forms::ToolStrip());
this->toolStripButton1 = (gcnew System::Windows::Forms::ToolStripButton());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->menuStrip1->SuspendLayout();
this->toolStrip1->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->fileToolStripMenuItem });
resources->ApplyResources(this->menuStrip1, L"menuStrip1");
this->menuStrip1->Name = L"menuStrip1";
//
// fileToolStripMenuItem
//
this->fileToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->connectToolStripMenuItem });
this->fileToolStripMenuItem->Name = L"fileToolStripMenuItem";
resources->ApplyResources(this->fileToolStripMenuItem, L"fileToolStripMenuItem");
//
// connectToolStripMenuItem
//
this->connectToolStripMenuItem->Name = L"connectToolStripMenuItem";
resources->ApplyResources(this->connectToolStripMenuItem, L"connectToolStripMenuItem");
this->connectToolStripMenuItem->Click += gcnew System::EventHandler(this, &MainPage::connectToolStripMenuItem_Click);
//
// toolStrip1
//
this->toolStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) { this->toolStripButton1 });
resources->ApplyResources(this->toolStrip1, L"toolStrip1");
this->toolStrip1->Name = L"toolStrip1";
//
// toolStripButton1
//
resources->ApplyResources(this->toolStripButton1, L"toolStripButton1");
this->toolStripButton1->Name = L"toolStripButton1";
this->toolStripButton1->Click += gcnew System::EventHandler(this, &MainPage::toolStripButton1_Click);
//
// pictureBox1
//
resources->ApplyResources(this->pictureBox1, L"pictureBox1");
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->TabStop = false;
//
// MainPage
//
resources->ApplyResources(this, L"$this");
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->toolStrip1);
this->Controls->Add(this->menuStrip1);
this->MainMenuStrip = this->menuStrip1;
this->Name = L"MainPage";
this->WindowState = System::Windows::Forms::FormWindowState::Maximized;
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->toolStrip1->ResumeLayout(false);
this->toolStrip1->PerformLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void toolStripButton1_Click(System::Object^ sender, System::EventArgs^ e) {
IPPort ^ ipportpage = gcnew IPPort();
ipportpage->Show();
}
private: System::Void connectToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
/*IPPort ^ ipportpage = gcnew IPPort();
ipportpage->Show();*/
Login^ L = gcnew Login();
L->Show();
}
void OnJPEGgenerate(System::Object^ sender,System::EventArgs^ e);
};
}
void UIv10::MainPage::OnJPEGgenerate(System::Object^ sender,System::EventArgs^ e)
{
MessageBoxA(NULL, "Failed to Login", "Attention", MB_OK);
throw gcnew System::NotImplementedException();
}
Login.h:
#include <msclr/marshal.h>
#include <Windows.h>
#ifndef Login_h
#define Login_h
bool FLogin(char use[128], char pass[64]);
#pragma once
namespace UIv10 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Login : public System::Windows::Forms::Form
{
public: delegate void JPEGgeneratorEventHandler(System::Object^ sender, EventArgs^ e);
public: event JPEGgeneratorEventHandler^ JPEGgenerate;
public:
Login(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Login()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ btnCancel2;
private: System::Windows::Forms::Button^ btnLogin;
private: System::Windows::Forms::TextBox^ txtbPassword;
private: System::Windows::Forms::TextBox^ txtbUserName;
private: System::Windows::Forms::Label^ lblPassword;
private: System::Windows::Forms::Label^ lblUserName;
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->btnCancel2 = (gcnew System::Windows::Forms::Button());
this->btnLogin = (gcnew System::Windows::Forms::Button());
this->txtbPassword = (gcnew System::Windows::Forms::TextBox());
this->txtbUserName = (gcnew System::Windows::Forms::TextBox());
this->lblPassword = (gcnew System::Windows::Forms::Label());
this->lblUserName = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// btnCancel2
//
this->btnCancel2->Location = System::Drawing::Point(158, 174);
this->btnCancel2->Name = L"btnCancel2";
this->btnCancel2->Size = System::Drawing::Size(71, 23);
this->btnCancel2->TabIndex = 11;
this->btnCancel2->Text = L"Cancel";
this->btnCancel2->UseVisualStyleBackColor = true;
//
// btnLogin
//
this->btnLogin->Location = System::Drawing::Point(61, 174);
this->btnLogin->Name = L"btnLogin";
this->btnLogin->Size = System::Drawing::Size(71, 23);
this->btnLogin->TabIndex = 10;
this->btnLogin->Text = L"Login";
this->btnLogin->UseVisualStyleBackColor = true;
this->btnLogin->Click += gcnew System::EventHandler(this, &Login::btnLogin_Click);
//
// txtbPassword
//
this->txtbPassword->Location = System::Drawing::Point(112, 109);
this->txtbPassword->Name = L"txtbPassword";
this->txtbPassword->PasswordChar = '*';
this->txtbPassword->Size = System::Drawing::Size(126, 20);
this->txtbPassword->TabIndex = 9;
this->txtbPassword->Text = L"roseek";
//
// txtbUserName
//
this->txtbUserName->Location = System::Drawing::Point(112, 65);
this->txtbUserName->Name = L"txtbUserName";
this->txtbUserName->Size = System::Drawing::Size(126, 20);
this->txtbUserName->TabIndex = 8;
this->txtbUserName->Text = L"roseek";
//
// lblPassword
//
this->lblPassword->Location = System::Drawing::Point(47, 117);
this->lblPassword->Name = L"lblPassword";
this->lblPassword->Size = System::Drawing::Size(59, 12);
this->lblPassword->TabIndex = 7;
this->lblPassword->Text = L"Password";
//
// lblUserName
//
this->lblUserName->AutoSize = true;
this->lblUserName->Location = System::Drawing::Point(47, 73);
this->lblUserName->Name = L"lblUserName";
this->lblUserName->Size = System::Drawing::Size(60, 13);
this->lblUserName->TabIndex = 6;
this->lblUserName->Text = L"User Name";
//
// Login
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->btnCancel2);
this->Controls->Add(this->btnLogin);
this->Controls->Add(this->txtbPassword);
this->Controls->Add(this->txtbUserName);
this->Controls->Add(this->lblPassword);
this->Controls->Add(this->lblUserName);
this->Name = L"Login";
this->Text = L"Login";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void btnLogin_Click(System::Object^ sender, System::EventArgs^ e) {
String ^ username;
String ^ password;
username = this->txtbUserName->Text;
password = this->txtbPassword->Text;
using namespace System::Runtime::InteropServices;
IntPtr Cusername = Marshal::StringToHGlobalAnsi(username);
char* UserName = static_cast<char*>(Cusername.ToPointer());
IntPtr Cpassword = Marshal::StringToHGlobalAnsi(password);
char* Password = static_cast<char*>(Cpassword.ToPointer());
bool Lret = FLogin(UserName, Password);
if (Lret == TRUE)
{
JPEGgenerate(this,e);
this->Close();
}
else
{
JPEGgenerate(this, e);
MessageBoxA(NULL, "Failed to Login", "Attention", MB_OK);
this->Close();
}
}
};
}
#endif
Sorry for the long codes, I just don't know where the problem may be.
The actuall problem is that when JPEGgenerate(this,e); runs it should run OnJPEGgenerate. But it doesn't.
This code seems wrong for me:
MainPage(void)
{
InitializeComponent();
Login ^ loginpage = gcnew Login();
loginpage->JPEGgenerate += gcnew UIv10::Login::JPEGgeneratorEventHandler(this, &MainPage::OnJPEGgenerate);
}
As I understood you should keep reference to such object, otherwise, it will be deleted (and appropriated handler will not be invoked)
Other piece of code (it will not contain registration of event's handler)
Login^ L = gcnew Login();
L->Show();
I am trying to write a program in Visual C++ that will play a sound based on volume and frequency inputs gotten from pixels of a black and white video. To start, I am first trying to get a sound to play sans parameters (which I will add in later). I've already included winmm.lib into the project.
I've got code that compiles, but does not play a sound. The code I have so far is as follows:
sound.h (provided for me, works on another project):
//file: soundthread.h
#include <Windows.h> //new
#include <mmsystem.h>
#ifndef SOUNDTHREAD_H
#define SOUNDTHREAD_H
class Sound {
public:
static void init();
static void close(){waveOutReset(hWaveOut); waveOutClose(hWaveOut);};
static void writeAudioBlock(LPSTR block);
private:
static HWAVEOUT hWaveOut;
};
#endif
sound.cpp (provided for me, works on another project):
#include "stdafx.h"
#include "sound.h"
#include <mmsystem.h>
#include <mmreg.h>
HWAVEOUT Sound::hWaveOut;
void Sound::init(){
WAVEFORMATEX wfx;
wfx.nSamplesPerSec = 8000;
wfx.wBitsPerSample = 8;
wfx.nChannels = 1;
wfx.cbSize = 0;
wfx.wFormatTag = WAVE_FORMAT_PCM;
// wfx.nBlockAlign = (wfx.wBitsPerSample >> 2) * wfx.nChannels;
wfx.nBlockAlign = (wfx.wBitsPerSample >> 3) * wfx.nChannels;
wfx.nAvgBytesPerSec = wfx.nBlockAlign * wfx.nSamplesPerSec;
/*
if(waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL)
!= MMSYSERR_NOERROR)
// int tt;
// tt = waveOutOpen(&hWaveOut, ((UINT)1), &wfx, 0, 0, CALLBACK_NULL);
// if(tt != MMSYSERR_NOERROR) {
fprintf(stderr, "unable to open WAVE_MAPPER device\n");
int tt;
tt = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL);
MessageBox(0, "unable to open WAVE_MAPPER device\n", "Error", MB_ICONERROR|MB_OK);
/* DBG: tt=MMSYSERR_ALLOCATED;
tt=MMSYSERR_BADDEVICEID;
tt=MMSYSERR_NODRIVER;
tt=MMSYSERR_NOMEM;
tt=WAVERR_BADFORMAT; //** this is it //
tt=WAVERR_SYNC; // END DBG */
// ExitProcess(1);
// }
}
void Sound::writeAudioBlock(LPSTR block) {
WAVEHDR header;
ZeroMemory(&header, sizeof(WAVEHDR));
header.dwBufferLength = 500;
header.lpData = block;
waveOutPrepareHeader(hWaveOut, &header, sizeof(WAVEHDR));
waveOutWrite(hWaveOut, &header, sizeof(WAVEHDR));
do {
Sleep(100);
}while(waveOutUnprepareHeader(hWaveOut,&header,sizeof(WAVEHDR)) == WAVERR_STILLPLAYING);
}
soundplay.h (created by me for holding the provided OnSound() function):
#ifndef SOUNDPLAY_H //SOUNDPLAY_H_INCLUDED
#define SOUNDPLAY_H //SOUNDPLAY_H_INCLUDED
#include "sound.h"
void OnSound(); //update this w/ freq and intensity paramaters
#endif // SOUNDPLAY_H_INCLUDED
soundplay.cpp (created by me for holding the provided OnSound() function):
#include "stdafx.h"
#include <windows.h>
#pragma comment(lib, "winmm.lib")
#include <math.h> //used to be under sound.h
#include "sound.h"
#include <mmsystem.h>
#define PI 3.141592653f
#include "soundplay.h" //new
void OnSound()
{
// produce a sin wave sound.
float freq = 440.f;
DWORD Fs=8000;
int N=500;
float* tt=new float[N];
for(int i=0;i<N;i++)
{
tt[i]=(float)i/(float)Fs;
}
float intensity = 0.5f; //volume
float *signal=new float[N];
for(int i=0;i<N;i++)
{
signal[i]=intensity*sin(2.f*PI*freq*tt[i]);
}
BYTE* data=new BYTE[N];
for(int i=0;i<N;i++)
{
data[i]=(BYTE)(signal[i]+128.f);
}
delete []signal;
Sound::writeAudioBlock((LPSTR)data);
delete []data;
delete []tt;
}
Form1.h (code from the GUI designer containing the button that triggers OnSound to play a sound):
#include "sound.h" //new
#include "soundplay.h" //new
#pragma once
namespace a2c {
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
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(13, 225);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"Sound Test";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(292, 273);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
//code for sound production
OnSound();
}
//NEW STUFF
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
Sound::init();
}
};
}
The contents of OnSound and sound.h and sound.cpp are from a sound example project that was created in Visual Studio 6.0 and modified for Visual Studio 2010 that works at playing the sound. My problem is that when I copy the code and try to use it in my own project, it compiles but doesn't play a sound when the button is pressed. Can anyone point me in the right direction?
Your problem is scaling the floating point samples into 8-bit unsigned data. Since intensity is 0.5, samples is going to be between -0.5 and 0.5. Adding 128 and truncating to a byte will give you data values of 128 or 127 - which is effectively zero. To scale to the full range of the BYTE you need to multiply by 127.
BYTE* data=new BYTE[N];
for(int i=0;i<N;i++)
{
data[i]=(BYTE)(signal[i]*127.f+128.f);
}
Additionally, unless you are okay with the quantization distortion, I would recommend adding some dither.
Michael Petch was right about the incomplete initialization of the sound system.
I added:
int tt;
tt = waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, 0, 0, CALLBACK_NULL);
to Sound::init() and it worked!
Thanks to all those who read and/or replied.
I'm a C++-CLI beginner and I have to get familiar with some simple things.
I made a form with 1 label and 1 button.
I want to change the text of the label by clicking on the button and calling some text via a void in another cpp class (changetext.cpp)
Structure:
Form1.h //just a regular form code with 1 label and 1 button handler at the end
#pragma once
namespace ms {
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
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
public:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button1;
public:
public:
public:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(313, 140);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"label1";
//
// button1
//
this->button1->Location = System::Drawing::Point(101, 117);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(100, 58);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(467, 262);
this->Controls->Add(this->button1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
public: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
changetext my = new changetext();
my.changeText();
}
};
}
ms.cpp (main) //just creates the form
// ms.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace ms;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
changetext.cpp
#include "stdafx.h"
#include "Form1.h"
int number;
void changeText()
{
number = 5;
Form1.label1->Text=number;
}
I am making a little mistake somewhere and I guess it has to do with header files, I'm used to code in java so headerfiles are not my cup of tea yet.
This is what to do:
Create getter in changetext.cpp
#include "stdafx.h"
#include "Form1.h"
int number = 5;
void getNumber()
{
return number;
}
Create header.h
#pragma once
void getNumber();
Include header.h and edit Form1.h
#pragma once
#include header.h
namespace ms {
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
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
public:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button1;
public:
public:
public:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->label1 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(313, 140);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(35, 13);
this->label1->TabIndex = 0;
this->label1->Text = L"label1";
//
// button1
//
this->button1->Location = System::Drawing::Point(101, 117);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(100, 58);
this->button1->TabIndex = 1;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this,&Form1::button1_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(467, 262);
this->Controls->Add(this->button1);
this->Controls->Add(this->label1);
this->Name = L"Form1";
this->Text = L"Form1";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
public: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
label1->Text=""+getNumber();
}
};
}
I have two file generated in visual studio c++ 2010 express.
test2.cpp
// test2.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace test2;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
return 0;
}
and Form1.h
#pragma once
namespace test2 {
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
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::WebBrowser^ webBrowser1;
public: System::Windows::Forms::Timer^ timer1;
private:
private: System::ComponentModel::IContainer^ components;
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->components = (gcnew System::ComponentModel::Container());
this->webBrowser1 = (gcnew System::Windows::Forms::WebBrowser());
this->timer1 = (gcnew System::Windows::Forms::Timer(this->components));
this->SuspendLayout();
//
// webBrowser1
//
this->webBrowser1->Dock = System::Windows::Forms::DockStyle::Fill;
this->webBrowser1->IsWebBrowserContextMenuEnabled = false;
this->webBrowser1->Location = System::Drawing::Point(0, 0);
this->webBrowser1->MinimumSize = System::Drawing::Size(20, 20);
this->webBrowser1->Name = L"webBrowser1";
this->webBrowser1->ScrollBarsEnabled = false;
this->webBrowser1->Size = System::Drawing::Size(284, 262);
this->webBrowser1->TabIndex = 0;
this->webBrowser1->Url = (gcnew System::Uri(L"http://wp.pl", System::UriKind::Absolute));
this->webBrowser1->DocumentCompleted += gcnew System::Windows::Forms::WebBrowserDocumentCompletedEventHandler(this, &Form1::webBrowser1_DocumentCompleted);
//
// timer1
//
this->timer1->Enabled = true;
this->timer1->Interval = 10000;
this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->webBrowser1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
this->Name = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
private: System::Void webBrowser1_DocumentCompleted(System::Object^ sender, System::Windows::Forms::WebBrowserDocumentCompletedEventArgs^ e) {
}
public: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
};
}
How to using argument from command prompt? For example how change
this->timer1->Interval = 10000;
on user var? Something like
this->timer1->Interval = time;
You can get a string array containing the command-line arguments by calling Environment::GetCommandLineArgs.