[vc6] [tpe] simulation win32 d'un systeme d'alarme fait pour les tpe 2004/2005

Description

Pour les TPE, on a choisis de faire un systeme d'alarme. N'ayant pas eu le tps de le realiser, j'ai eu l'idee de faire cette simulation.
Le code est tres bien commente (au cas ou j'aurai eu a expliquer le code).
Je trouve donc que c'est un bon tutorial pour apprendre a utiliser les images et le son en win32 facilement et avec des dialogboxes (a par pour le clavier je vois pas quoi leurs reprocher ...).

Source / Exemple :


// TPE.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "windows.h"
#include "mmsystem.h" //Pour le son
#include "resource.h"
#include "stdlib.h"

#pragma comment( lib, "winmm.lib" ) //Pour le son

//Prototypes
LRESULT CALLBACK MainCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK InputCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK SchemaCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

//Variables Globales
bool DOORCLOSED = true;
bool WINDOW1CLOSED = true;
bool WINDOW2CLOSED = true;
HINSTANCE hInstance;
int tempo=3000;

//Point d'entree du prog
int APIENTRY WinMain(HINSTANCE hinstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	//Declaration variables
	MSG message;
	hInstance=hinstance;

	// Ouvre la Dialogbox
	DialogBox(hinstance, (LPCTSTR)IDD_MAIN, NULL, (DLGPROC)MainCallBack);
	
	//Recoit les msg de windows
	while (GetMessage (&message, NULL, 0, 0))
	{
		TranslateMessage (&message) ;
		DispatchMessage (&message) ;
	}

	return 0;
}

//CallBack du dialog Main
LRESULT CALLBACK MainCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HBITMAP bmp,bmpled;
	HWND handle;

	switch (message)
	{
		case WM_INITDIALOG:	
			break;

		case WM_COMMAND:

			if (LOWORD(wParam) == IDC_CDOOR) //Si c'est la porte
			{				
				handle=GetDlgItem(hwnd,IDC_CDOOR);
				//Change l'image de la porte, de la led et enclenche/declanche le son
				if(DOORCLOSED == true)
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_DOOROPEN));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDA));
					SetWindowText(handle,"Fermer Porte");
					KillTimer(hwnd,1); // au cas ou le type reouvre la porte ....
					PlaySound("alarme.wav", GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC|SND_LOOP );
					DOORCLOSED=false;
				}
				else
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_DOORCLOSE));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDE));
					SetWindowText(handle,"Ouvrir Porte");
					DOORCLOSED=true;
					if(DOORCLOSED==true && WINDOW1CLOSED==true && WINDOW2CLOSED==true)
						SetTimer(hwnd, 1, tempo, NULL);
				}
				
				//Affiche la porte
				handle = GetDlgItem(hwnd,IDC_DOOR);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmp);

				//Affiche la led
				handle = GetDlgItem(hwnd,IDC_LED1);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmpled);
			}
			
			if (LOWORD(wParam) == IDC_CWINDOW1) //Si c'est la fenetre1
			{
				//Change l'image de la porte, de la led et enclenche/declanche le son
				handle=GetDlgItem(hwnd,IDC_CWINDOW1);
				if(WINDOW1CLOSED == true)
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_WINDOWOPENED));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDA));
					SetWindowText(handle,"Fermer Fenetre");
					KillTimer(hwnd,1); // au cas ou le type réouvre la porte ....
					PlaySound("alarme.wav", GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC|SND_LOOP );
					WINDOW1CLOSED=false;
				}
				else
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_WINDOWCLOSED));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDE));
					SetWindowText(handle,"Ouvrir Fenetre");
					WINDOW1CLOSED=true;
					if(DOORCLOSED==true && WINDOW1CLOSED==true && WINDOW2CLOSED==true)
						SetTimer(hwnd, 1, tempo, NULL);
				}
				
				//Affiche la porte
				handle = GetDlgItem(hwnd,IDC_WINDOW1);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmp);

				//Affiche la led
				handle = GetDlgItem(hwnd,IDC_LED2);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmpled);
			}

			if (LOWORD(wParam) == IDC_CWINDOW2) //Si c'est la fenetre2
			{
				//Change l'image de la porte, de la led et enclenche/declanche le son
				handle=GetDlgItem(hwnd,IDC_CWINDOW2);
				if(WINDOW2CLOSED == true)
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_WINDOWOPENED));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDA));
					SetWindowText(handle,"Fermer Fenetre");
					KillTimer(hwnd,1); // au cas ou le type réouvre la porte ....
					PlaySound("alarme.wav", GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC|SND_LOOP );
					WINDOW2CLOSED=false;
				}
				else
				{
					bmp = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_WINDOWCLOSED));
					bmpled = LoadBitmap((HINSTANCE)GetWindowLong(hwnd,GWL_HINSTANCE),MAKEINTRESOURCE(IDB_LEDE));
					SetWindowText(handle,"Ouvrir Fenetre");
					WINDOW2CLOSED=true;
					if(DOORCLOSED==true && WINDOW1CLOSED==true && WINDOW2CLOSED==true)
						SetTimer(hwnd, 1, tempo, NULL);
				}
				
				//Affiche la porte
				handle = GetDlgItem(hwnd,IDC_WINDOW2);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmp);

				//Affiche la led
				handle = GetDlgItem(hwnd,IDC_LED3);
				SendMessage(handle,STM_SETIMAGE,(WPARAM)IMAGE_BITMAP,(LPARAM) (HANDLE) bmpled);
			}

			if (LOWORD(wParam) == IDC_DTEMPO)
			{
				DialogBox(hInstance, (LPCTSTR)IDD_INPUTBOX, NULL, (DLGPROC)InputCallBack);

			}

			if (LOWORD(wParam) == IDC_CSTOPTEMPO)
			{
				if(DOORCLOSED==true && WINDOW1CLOSED==true && WINDOW2CLOSED==true)
				{
					PlaySound(NULL, GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC);
					KillTimer(hwnd,1);
				}
				else
				{
					PlaySound("alarme.wav", GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC|SND_LOOP );
					SetTimer(hwnd, 1, tempo, NULL);
				}

			}
			
			if (LOWORD(wParam) == IDR_SCHEMA)
			{
				DialogBox(hInstance, (LPCTSTR)IDD_SCHEMA, NULL, (DLGPROC)SchemaCallBack);
			}

			if (LOWORD(wParam) == IDC_QUIT)
			{
				PostQuitMessage(0);
			}
			
			if (LOWORD(wParam) == IDC_ABOUT)
			{
				MessageBox(hwnd,"Fait par MùPùF pour le TPE(2004/2005) de Bigou Karim, Palayret Fabien et Peres Martin.\nCe programme (écris avec vc++ 6) montre le fonctionnement de notre systeme d'alarme gungrave.","About",MB_OK);
			}
			break;

		case WM_TIMER:
			PlaySound(NULL, GetModuleHandle(NULL), SND_FILENAME|SND_ASYNC);
			KillTimer(hwnd,1);
			break;

		case WM_CLOSE:
			EndDialog(hwnd, LOWORD(wParam));
			break;

		case WM_DESTROY:
			PostQuitMessage(0);;
			break;

		return DefWindowProc (hwnd, message, wParam, lParam);
	}
	return 0;
}

//CallBack du dialog InputBox
LRESULT CALLBACK InputCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	//Variables
	int res;
	HWND handle;
	char* Ctempo;

	switch (message)
	{
		case WM_INITDIALOG:	
			handle = GetDlgItem(hwnd,IDC_RES);
			SetWindowText(handle,itoa(tempo,"",10));
			break;

		case WM_COMMAND:
			if (LOWORD(wParam) == IDOK)
			{
				handle = GetDlgItem(hwnd,IDC_RES);
				Ctempo=new char[7];
				GetWindowText(handle,Ctempo,6);
				tempo=atoi(Ctempo);
				delete Ctempo;
				EndDialog(hwnd, LOWORD(wParam));
			}
			if (LOWORD(wParam) == IDC_ANNULER)
			{
				res=MessageBox(hwnd,"Attention, vous allez annuler le changement de Temporisation\nEtes vous sur de vouloir ?","Attention", MB_YESNO+MB_ICONWARNING);
				if(res==IDYES)
					EndDialog(hwnd, LOWORD(wParam));
			}
			
			break;

		case WM_CLOSE:
			EndDialog(hwnd, LOWORD(wParam));
			break;

		return DefWindowProc (hwnd, message, wParam, lParam);
	}
	return 0;
}
//CallBack du dialog InputBox
LRESULT CALLBACK SchemaCallBack (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		case WM_COMMAND:
			if (LOWORD(wParam) == IDC_OK)
				EndDialog(hwnd, LOWORD(wParam));			
			break;

		case WM_CLOSE:
			EndDialog(hwnd, LOWORD(wParam));
			break;

		return DefWindowProc (hwnd, message, wParam, lParam);
	}
	return 0;
}

Conclusion :


Bon, ben içi je laisse la place a vos commentaires !
N'hesitez pas a incendier si ya un pet en travers, je suis ouvert a toutes critiques constructives.

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.