0% ont trouvé ce document utile (0 vote)
21 vues7 pages

Code

Le document présente un code en C pour une application de gestion d'étudiants utilisant une interface graphique Windows. Il permet d'ajouter, modifier, supprimer et afficher des informations sur les étudiants, ainsi que de les sauvegarder et de les charger à partir d'un fichier. Le programme inclut également une fenêtre de connexion pour l'accès à l'application.

Transféré par

glappiermimi
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
21 vues7 pages

Code

Le document présente un code en C pour une application de gestion d'étudiants utilisant une interface graphique Windows. Il permet d'ajouter, modifier, supprimer et afficher des informations sur les étudiants, ainsi que de les sauvegarder et de les charger à partir d'un fichier. Le programme inclut également une fenêtre de connexion pour l'accès à l'application.

Transféré par

glappiermimi
Copyright
© © All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats TXT, PDF, TXT ou lisez en ligne sur Scribd

Nom utilisateur : Hernest

Mot de passe : user

Code complet

Main.c

#include <windows.h>
#include "ressources.h"
#include <stdio.h>
#include <string.h>

#define MAX_ETUDIANTS 2000


#define MAX_CHAR 50

typedef struct {
char nom[MAX_CHAR];
char prenom[MAX_CHAR];
int age;
int absence;
double note1;
double note2;
double note3;
double moyenne;
} Etudiant;

Etudiant etudiants[MAX_ETUDIANTS];
int nbEtudiants = 0;

HWND hNom, hPrenom, hAge, hAbs, hN1, hN2, hN3, hResultat;

void SauvegarderEtudiants() {
FILE* f = fopen("etudiants.dat", "wb");
if (f) {
fwrite(&nbEtudiants, sizeof(int), 1, f);
fwrite(etudiants, sizeof(Etudiant), nbEtudiants, f);
fclose(f);
}
}

void ChargerEtudiants() {
FILE* f = fopen("etudiants.dat", "rb");
if (f) {
fread(&nbEtudiants, sizeof(int), 1, f);
fread(etudiants, sizeof(Etudiant), nbEtudiants, f);
fclose(f);
}
}

void AjouterEtudiant(HWND hwnd) {


if (nbEtudiants >= MAX_ETUDIANTS) {
MessageBox(hwnd, "Nombre maximum atteint", "Erreur", MB_OK);
return;
}

char nom[MAX_CHAR], prenom[MAX_CHAR], buf[20];


int age, absence;
double n1, n2, n3;

GetWindowText(hNom, nom, MAX_CHAR);


GetWindowText(hPrenom, prenom, MAX_CHAR);
GetWindowText(hAge, buf, 20); age = atoi(buf);
GetWindowText(hAbs, buf, 20); absence = atoi(buf);
GetWindowText(hN1, buf, 20); n1 = atof(buf);
GetWindowText(hN2, buf, 20); n2 = atof(buf);
GetWindowText(hN3, buf, 20); n3 = atof(buf);

strcpy(etudiants[nbEtudiants].nom, nom);
strcpy(etudiants[nbEtudiants].prenom, prenom);
etudiants[nbEtudiants].age = age;
etudiants[nbEtudiants].absence = absence;
etudiants[nbEtudiants].note1 = n1;
etudiants[nbEtudiants].note2 = n2;
etudiants[nbEtudiants].note3 = n3;
etudiants[nbEtudiants].moyenne = (n1 + n2 + n3) / 3.0;
nbEtudiants++;

SauvegarderEtudiants();
MessageBox(hwnd, "Etudiant ajouté", "Succès", MB_OK);
}

void AfficherEtudiants(HWND hwnd) {


char buffer[10000] = "";
int i;
for (i = 0; i < nbEtudiants; i++) {
char ligne[512];
sprintf(ligne, "%s %s | Age: %d | Abs: %d | Moy: %.2f\r\n",
etudiants[i].nom, etudiants[i].prenom, etudiants[i].age,
etudiants[i].absence, etudiants[i].moyenne);
strcat(buffer, ligne);
}
SetWindowText(hResultat, buffer);
}

void RechercherEtudiant(HWND hwnd) {


char nom[MAX_CHAR], res[512];
GetWindowText(hNom, nom, MAX_CHAR);
int i;
for (i = 0; i < nbEtudiants; i++) {
if (strcmp(etudiants[i].nom, nom) == 0) {
sprintf(res, "Trouvé: %s %s, Moyenne: %.2f", etudiants[i].nom,
etudiants[i].prenom, etudiants[i].moyenne);
SetWindowText(hResultat, res);
return;
}
}
MessageBox(hwnd, "Etudiant non trouvé", "Info", MB_OK);
}

void ModifierEtudiant(HWND hwnd) {


char nom[MAX_CHAR], buf[20];
GetWindowText(hNom, nom, MAX_CHAR);
int i;
for (i = 0; i < nbEtudiants; i++) {
if (strcmp(etudiants[i].nom, nom) == 0) {
char prenom[MAX_CHAR];
int age, abs;
double n1, n2, n3;
GetWindowText(hPrenom, prenom, MAX_CHAR);
GetWindowText(hAge, buf, 20); age = atoi(buf);
GetWindowText(hAbs, buf, 20); abs = atoi(buf);
GetWindowText(hN1, buf, 20); n1 = atof(buf);
GetWindowText(hN2, buf, 20); n2 = atof(buf);
GetWindowText(hN3, buf, 20); n3 = atof(buf);

strcpy(etudiants[i].prenom, prenom);
etudiants[i].age = age;
etudiants[i].absence = abs;
etudiants[i].note1 = n1;
etudiants[i].note2 = n2;
etudiants[i].note3 = n3;
etudiants[i].moyenne = (n1 + n2 + n3) / 3.0;

SauvegarderEtudiants();
MessageBox(hwnd, "Modifié avec succès", "Succès", MB_OK);
return;
}
}
MessageBox(hwnd, "Etudiant non trouvé", "Erreur", MB_OK);
}

void SupprimerEtudiant(HWND hwnd) {


char nom[MAX_CHAR];
int i, j;
GetWindowText(hNom, nom, MAX_CHAR);
for (i = 0; i < nbEtudiants; i++) {
if (strcmp(etudiants[i].nom, nom) == 0) {
for (j = i; j < nbEtudiants - 1; j++) {
etudiants[j] = etudiants[j + 1];
}
nbEtudiants--;
SauvegarderEtudiants();
MessageBox(hwnd, "Etudiant supprimé", "Succès", MB_OK);
return;
}
}
MessageBox(hwnd, "Etudiant non trouvé", "Erreur", MB_OK);
}

// Couleurs personnalisées
COLORREF bleuClair = RGB(173, 216, 230);
COLORREF grisClair = RGB(211, 211, 211);

// Gestion du dessin de fond de la fenêtre principale


BOOL CALLBACK MainDlgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG:
hNom = GetDlgItem(hwnd, IDC_NOM);
hPrenom = GetDlgItem(hwnd, IDC_PRENOM);
hAge = GetDlgItem(hwnd, IDC_AGE);
hAbs = GetDlgItem(hwnd, IDC_ABS);
hN1 = GetDlgItem(hwnd, IDC_NOTE1);
hN2 = GetDlgItem(hwnd, IDC_NOTE2);
hN3 = GetDlgItem(hwnd, IDC_NOTE3);
hResultat = GetDlgItem(hwnd, IDC_RESULTAT);
ChargerEtudiants();

// Mettre les zones de texte en gris clair


SetBkColor(GetDC(hNom), grisClair);
SetBkColor(GetDC(hPrenom), grisClair);
SetBkColor(GetDC(hAge), grisClair);
SetBkColor(GetDC(hAbs), grisClair);
SetBkColor(GetDC(hN1), grisClair);
SetBkColor(GetDC(hN2), grisClair);
SetBkColor(GetDC(hN3), grisClair);
SetBkColor(GetDC(hResultat), grisClair);
return TRUE;

case WM_CTLCOLORSTATIC:
case WM_CTLCOLOREDIT:
{
HDC hdc = (HDC)wParam;
HWND hCtl = (HWND)lParam;

if (hCtl == hResultat || hCtl == hNom || hCtl == hPrenom || hCtl == hAge ||


hCtl == hAbs || hCtl == hN1 || hCtl == hN2 || hCtl == hN3) {
SetBkColor(hdc, grisClair);
static HBRUSH hBrush = NULL;
if (!hBrush) hBrush = CreateSolidBrush(grisClair);
return (INT_PTR)hBrush;
}
return FALSE;
}

case WM_CTLCOLORBTN:
{
HDC hdc = (HDC)wParam;
SetBkMode(hdc, TRANSPARENT);
return FALSE;
}

case WM_CTLCOLORDLG:
{
HDC hdc = (HDC)wParam;
SetBkColor(hdc, bleuClair);
static HBRUSH hBrush = NULL;
if (!hBrush) hBrush = CreateSolidBrush(bleuClair);
return (INT_PTR)hBrush;
}

case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDC_INSCRIRE: AjouterEtudiant(hwnd); break;
case IDC_AFFICHER: AfficherEtudiants(hwnd); break;
case IDC_RECHERCHER: RechercherEtudiant(hwnd); break;
case IDC_MODIFIER: ModifierEtudiant(hwnd); break;
case IDC_SUPPRIMER: SupprimerEtudiant(hwnd); break;
case IDC_QUITTER: EndDialog(hwnd, 0); break;
}
return TRUE;

}
return FALSE;
}
// Fenêtre de connexion (login)
BOOL CALLBACK LoginProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_INITDIALOG:
return TRUE;

case WM_CTLCOLORDLG:
{
HDC hdc = (HDC)wParam;
SetBkColor(hdc, bleuClair);
static HBRUSH hBrush = NULL;
if (!hBrush) hBrush = CreateSolidBrush(bleuClair);
return (INT_PTR)hBrush;
}
case WM_CTLCOLOREDIT:
case WM_CTLCOLORSTATIC:
{
HDC hdc = (HDC)wParam;
SetBkColor(hdc, grisClair);
static HBRUSH hBrush = NULL;
if (!hBrush) hBrush = CreateSolidBrush(grisClair);
return (INT_PTR)hBrush;
}

case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
{
char user[30], pass[30];
GetDlgItemText(hwnd, IDC_USERNAME, user, 30);
GetDlgItemText(hwnd, IDC_PASSWORD, pass, 30);
if (strcmp(user, "Hernest") == 0 && strcmp(pass, "user") == 0) {
EndDialog(hwnd, 1);
}
else {
MessageBox(hwnd, "Nom ou mot de passe incorrect", "Erreur", MB_OK |
MB_ICONERROR);
}
break;
}
case IDCANCEL:
EndDialog(hwnd, 0);
break;
}
return TRUE;
}
return FALSE;
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpszCmdLine, int


nCmdShow) {
if (DialogBox(hInst, MAKEINTRESOURCE(IDD_LOGIN_DIALOG), NULL, LoginProc) == 1)
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_MAIN_DIALOG), NULL, MainDlgProc);
}
return 0;
}
Ressource.h

#define IDD_LOGIN_DIALOG 101


#define IDC_USERNAME 1001
#define IDC_PASSWORD 1002

#define IDD_MAIN_DIALOG 102


#define IDC_NOM 2001
#define IDC_PRENOM 2002
#define IDC_AGE 2003
#define IDC_ABS 2004
#define IDC_NOTE1 2005
#define IDC_NOTE2 2006
#define IDC_NOTE3 2007
#define IDC_RESULTAT 2008

#define IDC_INSCRIRE 3001


#define IDC_AFFICHER 3002
#define IDC_RECHERCHER 3003
#define IDC_MODIFIER 3004
#define IDC_SUPPRIMER 3005
#define IDC_QUITTER 3006

Ressource.rc

#include "ressources.h"
#include <windows.h>

IDD_LOGIN_DIALOG DIALOGEX 0, 0, 250, 120


STYLE DS_SETFONT | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Connexion"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Nom d'utilisateur :", -1, 10, 20, 90, 10
EDITTEXT IDC_USERNAME, 110, 18, 120, 14, ES_AUTOHSCROLL

LTEXT "Mot de passe :", -1, 10, 50, 90, 10


EDITTEXT IDC_PASSWORD, 110, 48, 120, 14, ES_PASSWORD | ES_AUTOHSCROLL

DEFPUSHBUTTON "Connexion", IDOK, 50, 90, 70, 20


PUSHBUTTON "Annuler", IDCANCEL, 130, 90, 70, 20
END

IDD_MAIN_DIALOG DIALOGEX 0, 0, 380, 400


STYLE DS_SETFONT | WS_OVERLAPPEDWINDOW | WS_CAPTION | WS_SYSMENU
CAPTION "Gestion Etudiants"
FONT 8, "MS Shell Dlg"
BEGIN
LTEXT "Nom :", -1, 20, 20, 50, 10
EDITTEXT IDC_NOM, 80, 18, 120, 14, ES_AUTOHSCROLL
LTEXT "Prénom :", -1, 220, 20, 50, 10
EDITTEXT IDC_PRENOM, 280, 18, 80, 14, ES_AUTOHSCROLL

LTEXT "Âge :", -1, 20, 50, 50, 10


EDITTEXT IDC_AGE, 80, 48, 120, 14, ES_AUTOHSCROLL

LTEXT "Absences :", -1, 220, 50, 50, 10


EDITTEXT IDC_ABS, 280, 48, 80, 14, ES_AUTOHSCROLL

LTEXT "Note 1 :", -1, 20, 80, 50, 10


EDITTEXT IDC_NOTE1, 80, 78, 120, 14, ES_AUTOHSCROLL

LTEXT "Note 2 :", -1, 220, 80, 50, 10


EDITTEXT IDC_NOTE2, 280, 78, 80, 14, ES_AUTOHSCROLL

LTEXT "Note 3 :", -1, 20, 110, 50, 10


EDITTEXT IDC_NOTE3, 80, 108, 120, 14, ES_AUTOHSCROLL

LTEXT "Etudiants enregistrés :", -1, 20, 140, 150, 10


EDITTEXT IDC_RESULTAT, 20, 160, 340, 180, ES_MULTILINE | ES_AUTOVSCROLL |
ES_READONLY | WS_VSCROLL | WS_BORDER

PUSHBUTTON "Inscrire", IDC_INSCRIRE, 20, 350, 60, 20


PUSHBUTTON "Afficher", IDC_AFFICHER, 90, 350, 60, 20
PUSHBUTTON "Rechercher", IDC_RECHERCHER, 160, 350, 70, 20
PUSHBUTTON "Modifier", IDC_MODIFIER, 240, 350, 60, 20
PUSHBUTTON "Supprimer", IDC_SUPPRIMER, 310, 350, 60, 20

PUSHBUTTON "Quitter", IDC_QUITTER, 20, 380, 340, 20


END

Merci.

Vous aimerez peut-être aussi