
sprintjeffC
|
TU ES GRAND CA MARCHE !!! (faut donner toutes les instructions je vais pas les trouver tout seul ;-) En fait j'ai utilisé la fonction getstockobject pour balancer le même gris dans la fenêtre et les static !!!
merci 100Ox
Kdo => voici le code de mon petit crypteur quasi achevé grâce à toi :
// To do : // - icône dans fenêtre // - voir remarques brunews
// Rappel normes de nommage des variables // Dsl je suis VB-iste à la base ;-) // - 'g' si variable niveau global, 'm' si niveau module, rien si local // - sur 3 caratères : rappel type variable // - sur n caracteres : non de la variable // Ex : mhwndMaFenetre = handle pour window niveau module MaFenetre // Si nommage utilisé une seule fois le préfixe suffit (ex : mhwnd) // Tableaux : gtoi = global table of integers
#include <windows.h> // include pour programmation windows #include <string.h> // pour strcpy, ... #include <stdlib.h> // pour atoi, ...
#define ID_EDITBOX_1 1 // ID editbox #define ID_EDITBOX_2 2 #define ID_EDITBOX_3 3 #define ID_BUTTON_1 1 // ID boutons #define ID_BUTTON_2 2 #define ID_STATIC_1 1 // ID static #define ID_STATIC_2 2
HWND ghwnd; // déclaration handle fenêtre HWND ghedbEDB1; // déclaration handle editbox HWND ghedbEDB2; HWND ghedbEDB3;
// Astuce pour colorier les static HBRUSH hbr =(HBRUSH)GetStockObject(LTGRAY_BRUSH);
// Gestion des évènements sur la fenêtre LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); // Fonction de cryptage, décryptage char fCrypte (char chCryptage);
// La main avec windows.h int WINAPI WinMain(HINSTANCE FirstInstance, HINSTANCE PrevInstance, LPSTR CmdLine, int CmdShow) {
MSG msg; WNDCLASSEX wc;
// --------------------------------------------------------------------------------
// Enregistrement classe Window wc.hInstance = FirstInstance; // instance wc.lpszClassName = "Window"; // nom de la classe wc.lpfnWndProc = WinProc; // adresse de la procédure wc.style = CS_DBLCLKS; // style wc.cbSize = sizeof(WNDCLASSEX); // taille wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // grande icone wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); // petite icone wc.hCursor = LoadCursor(NULL, IDC_ARROW); // curseur wc.lpszMenuName = NULL; // menu wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = hbr; // couleur de fond if (!RegisterClassEx(&wc)) { MessageBox(NULL, "Erreur lors de l'initialisation de la classe Window.", "Erreur", MB_OK); return 0; }
// --------------------------------------------------------------------------------
// Création de la fenêtre ghwnd = CreateWindowEx(0, "Window", "Crypteur/décrypteur XOR", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 530, 260, HWND_DESKTOP, NULL, FirstInstance, NULL); if (ghwnd == NULL) { MessageBox(NULL, "Erreur lors de la création d'une fenêtre.", "Erreur", MB_OK); return 0; }
// Création des editbox (avec classe prédéfinie) ghedbEDB1 = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_VISIBLE|WS_CHILD|ES_AUTOHSCROLL|ES_NOHIDESEL, 10, 10, 500, 25, ghwnd, (HMENU)ID_EDITBOX_1, FirstInstance, NULL); ghedbEDB2 = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_VISIBLE|WS_CHILD|ES_AUTOHSCROLL|ES_NOHIDESEL, 10, 50, 500, 25, ghwnd, (HMENU)ID_EDITBOX_2, FirstInstance, NULL); ghedbEDB3 = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW, "EDIT", "", WS_VISIBLE|WS_CHILD|ES_AUTOHSCROLL|ES_NOHIDESEL, 340, 120, 130, 25, ghwnd, (HMENU)ID_EDITBOX_3, FirstInstance, NULL); if (ghedbEDB1 == NULL && ghedbEDB2 == NULL && ghedbEDB3 == NULL) { MessageBox(ghwnd, "Erreur lors de la création d'une editbox.", "Erreur", MB_OK); return 0; }
// Création des boutons (avec classe prédéfinie) HWND hbtnBTN1 = CreateWindowEx(0, "BUTTON", "", WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON|BS_NOTIFY|BS_TEXT, 40, 120, 100, 50, ghwnd, (HMENU)ID_BUTTON_1, FirstInstance, NULL); HWND hbtnBTN2 = CreateWindowEx(0, "BUTTON", "", WS_VISIBLE|WS_CHILD|BS_PUSHBUTTON|BS_NOTIFY|BS_TEXT, 160, 120, 100, 50, ghwnd, (HMENU)ID_BUTTON_2, FirstInstance, NULL); if (hbtnBTN1 == NULL && hbtnBTN2 == NULL) { MessageBox(ghwnd, "Erreur lors de la création d'un bouton.", "Erreur", MB_OK); return 0; }
// Création des static (avec classe prédéfinie) HWND hstaSTA1 = CreateWindow("STATIC", "", WS_CHILD | WS_VISIBLE | SS_LEFT, 290, 120, 50, 18, ghwnd, (HMENU)ID_STATIC_1, FirstInstance, NULL ); HWND hstaSTA2 = CreateWindow("STATIC", "", WS_CHILD | WS_VISIBLE | SS_LEFT, 358, 200, 150, 18, ghwnd, (HMENU)ID_STATIC_1, FirstInstance, NULL ); if (hstaSTA1 == NULL || hstaSTA2 == NULL) { MessageBox(ghwnd, "Erreur lors de la création d'un static.", "Erreur", MB_OK); return 0; }
// --------------------------------------------------------------------------------
// Envoi de texte sur les objets SetWindowText(ghedbEDB1, "Taper ici le texte à crypter/décrypter de 5000 caractères maximum ..."); UpdateWindow(ghedbEDB1); SetWindowText(ghedbEDB3, "int.int.int ..."); UpdateWindow(ghedbEDB3); SetWindowText(hbtnBTN1, "Crypter"); UpdateWindow(hbtnBTN1); SetWindowText(hbtnBTN2, "Décrypter"); UpdateWindow(hbtnBTN2); SetWindowText(hstaSTA1, " Clef : "); UpdateWindow(hstaSTA1); SetWindowText(hstaSTA2, " jerome.ferquel@free.fr "); UpdateWindow(hstaSTA2);
// Affichage de la fenêtre ShowWindow(ghwnd, SW_SHOW); UpdateWindow(ghwnd);
// Met le focus sur l'editbox 1 SetFocus(ghedbEDB1);
// --------------------------------------------------------------------------------
// Boucle qui permet au programme de tourner sans arrêt et de guetter les // évènement pour les envoyer à la procédure qui les gère (ci dessous) while (GetMessage(&msg, NULL, 0, 0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam;
}
// Evènements sur la fenêtre LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
char chRretour;
switch (msg) {
// Fermeture du programme case WM_DESTROY:
// Ferme le programme PostQuitMessage(0); break;
// Fermeture de la fenêtre via le menu système ou la croix(X) noir case WM_CLOSE:
// Décharge la fenêtre DestroyWindow(hwnd); break;
// Réception des évènements sur les objets case WM_COMMAND:
// editbox if (LOWORD(wParam) == ID_EDITBOX_1 || LOWORD(wParam) == ID_EDITBOX_2 ) { switch (HIWORD(wParam)) {
case EN_ERRSPACE:
MessageBox(ghwnd, "Mémoire insuffisante sur la station !!!", "Erreur", MB_OK); break;
case EN_MAXTEXT:
MessageBox(ghwnd, "Le nombre maximum de caractères dans une editbox a été atteint !!!", "Erreur", MB_OK); break;
case EN_CHANGE: //MessageBox(ghwnd, "Le contenu d'une editbox à changé.", "EN_CHANGE", MB_OK); break;
default:
break;
}
}
// Bouton Crypter // Vérifie que c'est bien le bouton et vérifie qu'on à cliqué dessus if ((LOWORD(wParam) == ID_BUTTON_1) && (HIWORD(wParam) == BN_CLICKED)) { if(fCrypte(1) != 1) { MessageBox(ghwnd, "Zut ... le cryptage a planté ???", "Crytpage", MB_OK); } break; }
// Bouton Décrypter // Vérifie que c'est bien le bouton et vérifie qu'on à cliqué dessus if ((LOWORD(wParam) == ID_BUTTON_2) && (HIWORD(wParam) == BN_CLICKED)) { if(fCrypte(2) != 1) { MessageBox(ghwnd, "Zut ... le décryptage a planté ???", "Décrytpage", MB_OK); } }
break;
case WM_CTLCOLORSTATIC : // Coloration des static SetBkMode((HDC) wParam, TRANSPARENT); return (BOOL) hbr; break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam); break;
}
return 0;
}
char fCrypte (char chCryptage) {
int i, iLongueur, iIndiceMax, iNbEltClef; int toiClef[50]; char sIn[5000] = " "; char sOut[5000] = " "; char sClef[500] = " "; char sTmpClef[500] = " "; char *sTmp1;
// Récupère la chaine à crypter GetWindowText(ghedbEDB1, sIn, 5000); // Récupère la clé de cryptage GetWindowText(ghedbEDB3, sTmpClef, 500);
// Contrôles if (strcmp(sTmpClef,"int.int.int ...") == 0) { MessageBox(ghwnd, "Il faut saisir une clef (ex : 2.456.8.7.78) ...", "M'enfin ???", MB_OK|MB_ICONINFORMATION); return 1; } if (strlen(sTmpClef) >= 500) { MessageBox(ghwnd, "500 caractères max dans la clef (séparateur compris) ... tronquée au delà !!!", "Warning ...", MB_OK|MB_ICONINFORMATION); }
// Chargement clé // On la charge à l'envers car strrchr analyse en commençant par la fin strcpy(sClef, "."); strcat(sClef, sTmpClef); iNbEltClef = 0; while (*sClef != 0 && iNbEltClef <= 50) { sTmp1 = strrchr(sClef, 46); // 46 = code ascii décimal de '.' sTmp1 = sTmp1+1; toiClef[iNbEltClef] = atoi(sTmp1); sTmp1 = sTmp1-1; *sTmp1 = 0; iNbEltClef++; } if (iNbEltClef > 50) { MessageBox(ghwnd, "50 eléments dans la clef ça me semblait pourtant suffisant !!!", "Overflow ...", MB_OK|MB_ICONINFORMATION); return 0; }
// Applique le XOR avec la clé iLongueur = strlen(sIn); iIndiceMax = iLongueur - 1; for(i=0; i<iLongueur; i++) { if (chCryptage == 1) { sOut[i]= sIn[iIndiceMax-i] ^ toiClef[i%iNbEltClef]; } else // =2, décryptage { sOut[iIndiceMax-i]= sIn[i] ^ toiClef[i%iNbEltClef]; } }
// Met le résultat dans l'editbox 2 SetWindowText(ghedbEDB2, sOut); UpdateWindow(ghedbEDB2);
return 1;
}
|