Recuperer la notification EN_CHANGE

KinNoShishi Messages postés 1 Date d'inscription mercredi 6 janvier 2010 Statut Membre Dernière intervention 11 janvier 2010 - 11 janv. 2010 à 15:33
BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019 - 11 janv. 2010 à 18:41
Bonjour à tous,

Je suis dans impasse depuis plusieurs heures maintenant... Je n'arrive pas à récupérer le message EN_CHANGE qui me permettrait de pouvoir, avant de creer une nouvelle feuille dans mon Richedit, sauver le contenu si il y eu modification du texte.

J'ai pourtant bien parametrer le EM_SETEVENTMASK => SendMessage(hWndEdit,EM_SETEVENTMASK,0,ENM_CHANGE).

La partie en rouge étant le probleme, pourriez-vous m'aider?

Voici le code:

/ ToolBarEdit.c
// Une fenêtre d'édition (RichEdit) avec dialogue "Chercher" non-modal

#include "ToolBarEdit.h" // les ID du menu et autres
#include <richedit.h> // UTILITAIRES POUR LE RICHEDIT

#define TAILLENOMFICH 256 // pour nom (et chemin) de fichier

// Charge la fenêtre avec le contenu d'un fichier choisi par l'utilisateur
// Utilise une boîte de dialogue standard
void ChargeFenetre(HWND hWnd);
void SaveAsFenetre(HWND hWnd);
BOOL Save(HWND hWnd);

void Chercher(HWND hWnd); // ouvrir le dialogue chercher

PSTR szProgName = "ToolBarEdit"; // le nom du programme
extern HWND hWndToolbar;
static HWND hWndEdit; // sous-fenêtre (garde sa valeur)
extern int affichage; // ETAT D'AFFICHAGE DE LA TOOLBAR
char szNomFichier[TAILLENOMFICH]; // NOM ET CHEMIN DU FICHIER A SAUVER

LRESULT CALLBACK WndProc(HWND hWnd, // handle de notre fenêtre
UINT nMsg, // numéro du message
WPARAM wParam, // 1ère info supplémentaire
LPARAM lParam) { // 2ème info supplémentaire
HINSTANCE hInst; // processus propriétaire
// entre les appels de WndProc
RECT TRect;
int hEdit;

switch(nMsg) { // quel message ?
case WM_CREATE : // on va créer une sous-fenêtre
// le processus propriétaire
hInst = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
LoadLibrary("RICHED32.DLL"); // librairie contenant RichEdit
hWndEdit = CreateWindowEx(
0, // style étendu (rien de particulier)
"RICHEDIT", // nom de la classe
NULL, // pas de titre
WS_CHILD | // fenêtre fille
WS_VISIBLE | // visible
WS_VSCROLL | // avec défilement vertical
ES_NOHIDESEL| // texte sélectionné tj. visible
ES_MULTILINE, // édition sur plusieurs lignes
0, 0, 0, 0, // dimension spécifiée ailleurs
hWnd, // handle fenêtre parente
NULL, // pas de menu
hInst, // processus propriétaire
NULL); // pas de data supplémentaires
SendMessage(hWndEdit,EM_SETEVENTMASK,0,ENM_CHANGE);
return 0; // message traité
case WM_SETFOCUS : // on reçoit la main
SetFocus(hWndEdit); // la passer à la fenêtre fille
return 0; // message traité
case WM_SIZE : // dimension de fenêtre ajustée
// redimensionner fenêtre fille

GetWindowRect(hWndToolbar,&TRect);
hEdit = TRect.bottom - TRect.top;
MoveWindow(hWndEdit, // handle fenêtre fille
0, // bord gauche
0, // BORD SUPERIEURE = LA HAUTEUR DE LA TOOLBAR
LOWORD(lParam), // largeur
HIWORD(lParam), // - hEdit, // HAUTEUR - LA HAUTEUR DE LA TOOLBAR (COMMENCEMENT DE LA TOOLBAR)
TRUE); // repeint la fenêtre fille
return 0; // message traité
case WM_COMMAND : // notification ou menu
switch (LOWORD(wParam)) { // SI UN DES SOUS MENUS A ETE SELECTIONNE
case ID_FICHIER_NOUVEAU : // CREATION D'UN NOUVEAU FICHIER
if (HIWORD(wParam) == EN_CHANGE) {
if (MessageBox(hWndEdit,
"Votre fichier a été modifié. Voulez-vous le sauver?",
szProgName,
MB_ICONINFORMATION | MB_YESNOCANCEL) == IDYES) {
SaveAsFenetre(hWndEdit);
SendMessage(hWndEdit, WM_SETTEXT, 0, 0);
}
}
break;
case ID_FICHIER_QUITTER :
if (!*szNomFichier) // (HIWORD(wParam)) == EN_CHANGE)
switch (MessageBox(hWndEdit,
"Votre fichier a été modifié. Voulez-vous le sauver?",
szProgName,
MB_ICONINFORMATION | MB_YESNOCANCEL)) {
case IDYES :
SaveAsFenetre(hWndEdit);
break;
case IDNO :
DestroyWindow(hWnd);
}
break;
} if(HIWORD(wParam) == 0) { // entrée du menu sélectionnée
switch(LOWORD(wParam)) { // laquelle ?
/*case ID_FICHIER_NOUVEAU : // CREATION D'UN NOUVEAU FICHIER
SendMessage(hWndEdit,ID_FICHIER_ENREGISTRER_SOUS,0,0);
SendMessage(hWndEdit, WM_SETTEXT, 0, 0);
break;*/
case ID_FICHIER_OUVRIR : // utilise GetOpenFileName
ChargeFenetre(hWndEdit); // pour charger la fenêtre
break;
case ID_FICHIER_QUITTER : // fermer la fenêtre
PostMessage(hWnd, WM_CLOSE,0, 0); // envoyer message WM_CLOSE
break;
case ID_EDITER_COUPER : // envoie WM_CUT
SendMessage(hWndEdit, WM_CUT, 0, 0);
break;
case ID_EDITER_COPIER : // envoie WM_COPY
SendMessage(hWndEdit, WM_COPY, 0, 0);
break;
case ID_EDITER_COLLER : // envoie WM_PASTE
SendMessage(hWndEdit, WM_PASTE, 0, 0);
break;
case ID_EDITER_EFFACER : // envoie WM_CLEAR
SendMessage(hWndEdit, WM_CLEAR, 0, 0);
break;
case ID_CHERCHER_REMPLACER : // ouvre le dialogue Chercher
Chercher(hWndEdit);
break;
case ID_FICHIER_ENREGISTRER_SOUS : // ouvre le dlgBox Enregistrer sous
SaveAsFenetre(hWndEdit);
break;
case ID_EDITER_SELECTIONNER : // Selectionne tout le texte dans l'edit
SendMessage(hWndEdit,EM_SETSEL,0,-1);
break;
case ID_MONTRER_TOOLBAR : // Montre ou cache la toolbar
if (affichage == SW_HIDE) {
ShowWindow(hWndToolbar,SW_SHOW);
affichage = SW_SHOW;
GetWindowRect(hWndToolbar,&TRect);
hEdit = TRect.bottom - TRect.top;
MoveWindow(hWndEdit, // handle fenêtre fille
0, // bord gauche
hEdit, // BORD SUPERIEURE = LA HAUTEUR DE LA TOOLBAR
LOWORD(lParam), // largeur
HIWORD(lParam) - hEdit, // HAUTEUR - LA HAUTEUR DE LA TOOLBAR (COMMENCEMENT DE LA TOOLBAR)
TRUE);
} else {
ShowWindow(hWndToolbar,SW_HIDE);
affichage = SW_HIDE;
GetWindowRect(hWndToolbar,&TRect);
MoveWindow(hWndEdit, // handle fenêtre fille
0, // bord gauche
0, // BORD SUPERIEURE = LA HAUTEUR DE LA TOOLBAR
LOWORD(lParam), // largeur
HIWORD(lParam), // HAUTEUR - LA HAUTEUR DE LA TOOLBAR (COMMENCEMENT DE LA TOOLBAR)
TRUE);
}
break;
case ID_FICHIER_ENREGISTRER : // MESSAGE QUI ENVOIE TRAITE LE CAS DE SAUVER
Save(hWndEdit);
break;
}
return 0; // message traité
}
break; // traitement par défaut
case WM_CLOSE : // on veut fermer ?
if (!*szNomFichier) {
if (SendMessage(hWnd,EM_GETEVENTMASK,0,0) == ENM_CHANGE) { // TESTE SI IL YA EU MODIFICATION DU RICHEDIT
if (MessageBox(hWnd,
"Vous avez apportez des modifications. Etes-vous sûr de vouloir quitter?",
szProgName,
MB_ICONINFORMATION | MB_YESNOCANCEL) == IDYES)
SaveAsFenetre(hWndEdit);
else if (MessageBox == IDCANCEL)
ShowWindow(hWnd,SW_SHOW);
}
}
else
// Save(hWndEdit);
DestroyWindow(hWnd);
break;
/*if(MessageBox(hWnd, // demander confirmation
"Sûr de vouloir quitter ?",
szProgName,
MB_ICONQUESTION |
MB_OKCANCEL
) == IDOK) // si l'utilisateur confirme
DestroyWindow(hWnd); // détruire la fenêtre principale */
return 0; // message traité
case WM_DESTROY : // fenêtre en passe d'être détruite
PostQuitMessage(0); // envoie WM_QUIT
return 0; // message traité
}
// tous les messages non pris en charge
return( DefWindowProc(hWnd, nMsg, wParam, lParam) );
}

void ChargeFenetre(HWND hWnd) {
OPENFILENAME ofn; // structure pour GetOpenFileName
char szNomFichier[TAILLENOMFICH]; // nom et chemin du fichier
char *szTexte; // texte lu dans le fichier
HANDLE hFile; // handle du fichier
DWORD taille, // taille du fichier
cbLus; // nombre d'octets lus

*szNomFichier = '\0'; // nom initial vide
memset(&ofn, 0, sizeof(ofn)); // initialise la structure à 0
ofn.lStructSize = sizeof(ofn); // taille de la structure
ofn.hwndOwner = hWnd; // proprio : fenêtre principale
ofn.lpstrFile = szNomFichier; // buffer pour nom + chemin
ofn.nMaxFile = TAILLENOMFICH; // taille du buffer
ofn.Flags = OFN_PATHMUSTEXIST | // exige un chemin valide
OFN_FILEMUSTEXIST; // ainsi qu'un nom valide
ofn.lpstrFilter = "Tout type de fichier (*.*)\0*.*\0\0"; // OUVRE DES FICHIERS DE TOUT TYPE
ofn.lpstrDefExt = ".*"; // MASQUE D'EXTENSION OUVRANT TOUT FICHIER
if(GetOpenFileName(&ofn)) // boite de dialogue Open
// ouvre le fichier
if((hFile = CreateFile(szNomFichier, // nom du fichier
GENERIC_READ, // mode lecture
FILE_SHARE_READ, // d'autres processus peuvent lire
NULL, // attributs de sécurité (pour NT)
OPEN_EXISTING, // le fichier doit exister
0, // attributs du fichier
NULL)) // handle de modèle d'attributs
!= INVALID_HANDLE_VALUE) { // pas d'erreur d'ouverture
taille = GetFileSize(hFile, NULL); // taille du fichier (max 4Gb)
szTexte = malloc(taille + 1); // alloue le buffer
if(ReadFile(hFile, szTexte, // lit le fichier dans szTexte
taille, // nombre max. d'octets à lire
&cbLus, // nombre d'octets lus
NULL)) { // pas d'opérations asynchrones
szTexte[cbLus] = '\0'; // termine le string
SetWindowText(hWnd, szTexte); // charge la fenêtre
free(szTexte); // libère le buffer
}
CloseHandle(hFile); // ferme le fichier
}
}

void SaveAsFenetre(HWND hWnd) {
OPENFILENAME lpofn; // STRUCTURE POUR GETSAVEFILENAME
char szNomFichier[TAILLENOMFICH]; // nom et chemin du fichier
char *szTexte; // texte lut dans le fichier
HANDLE hFile; // handle du fichier
DWORD taille, // taille du fichier
cbLus; // nombre d'octets lus

*szNomFichier = '\0'; // nom initial vide
memset(&lpofn, 0, sizeof(lpofn)); // initialise la structure à 0
lpofn.lStructSize = sizeof(lpofn); // taille de la structure
lpofn.hwndOwner = hWnd; // proprio : fenêtre principale
lpofn.lpstrFile = szNomFichier; // buffer pour nom + chemin
lpofn.nMaxFile = TAILLENOMFICH; // taille du buffer
lpofn.Flags = OFN_PATHMUSTEXIST | // exige un chemin valide
OFN_OVERWRITEPROMPT; // DDE CONFIRMATION D'ENREGISTREMENT SI LE FICHIER EXISTE DEJA
lpofn.lpstrFilter = "Fichier RTF (*.rtf)\0*.rtf\0\0"; // SAUVE LE NOM DU FICHIER EN .RTF
lpofn.lpstrDefExt = "rtf"; // MASQUE D'EXTENSION A ".RTF"

if(GetSaveFileName(&lpofn)) // BOITE DE DIALOGUE Sauver sous... ?
// OUVERTURE DU FICHIER POUR Y ECRIRE
if((hFile = CreateFile(szNomFichier, // nom du fichier
GENERIC_WRITE, // MODE ECRITURE
0, // AUCUN PROCESSUS NE PEUT ECRIRE
NULL, // attributs de sécurité (pour NT)
CREATE_ALWAYS, // ECRITURE QUE LE FICHIER EXISTE OU NON
0, // attributs du fichier
NULL)) // handle de modèle d'attributs
!= INVALID_HANDLE_VALUE) { // pas d'erreur d'ouverture
taille = GetWindowTextLength; // ENREGISTREMENT DE LA TAILLE DES CARACTERE (max 4Gb)
szTexte = malloc(taille + 1); // alloue le buffer
GetWindowText(hWndEdit, szTexte, taille); // RECUPERE LE TEXTE CONTENU DANS L'EDIT
if(WriteFile(hFile, szTexte, // ECRIT DANS LE FICHIER szTexte
taille, // NB MAX. D'OCTETS A ECRIRE
&cbLus, // NB OCTETS LUS
NULL)) { // pas d'opérations asynchrones
szTexte[cbLus] = '\0'; // termine le string

free(szTexte); // libère le buffer
CloseHandle(hFile);
}
// CloseHandle(hFile); // ferme le fichier
} else
(MessageBox(hWnd,"Impossible de sauver le fichier","Message",MB_OK));
}

BOOL Save(HWND hWnd) {
char *szTexte; // texte lut dans le fichier
HANDLE hFile; // HANDLE DU FICHIER DANS LEQUEL ON ECRIT
EDITSTREAM es; // INFOS QUI ENTRENT/SORTENT DU FICHIER
// *szNomFichier = '\0'; // NOM DU FICHIER VIDE
DWORD taille, // TAILLE DU FICHIER
cbLus; // NOMBRE D'OCTETS LUS

// SAUVE LE FICHIER EN "ENREGISTRER SOUS..." SI LE FICHIER N'AS PAS ENCORE DE NOM
if (!*szNomFichier) { //
SaveAsFenetre(hWnd);
return TRUE ;
}

// CREATION DU FICHIER
if((hFile = CreateFile(szNomFichier, // NOM DU FICHIER
GENERIC_WRITE, // MODE ECRITURE
0, // AUCUN AUTRE PROCESSUS NE PEUT ECRIRE
NULL, // attributs de sécurité (pour NT)
CREATE_ALWAYS, // ECRITURE QUE LE FICHIER EXISTE OU NON
0, // attributs du fichier
NULL)) // handle de modèle d'attributs
!= INVALID_HANDLE_VALUE) { // pas d'erreur d'ouverture
taille = GetWindowLong;
szTexte = malloc(taille + 1); // alloue le buffer
GetWindowText(hWndEdit, szTexte, taille); // RECUPERE LE TEXTE CONTENU DANS L'EDIT
es.dwCookie = (DWORD)hFile;
es.pfnCallback = WriteFile(hFile, szTexte, // ECRIT DANS LE FICHIER szTexte
taille, // NB MAX. D'OCTETS A ECRIRE
&cbLus, // NB OCTETS LUS
NULL); // PAS D'OPERATIONS ASYNCHRONES
es.dwError = 0; // PAS D'ERREUR DANS L'ECRITURE DE L'EDIT STREAM
SendMessage(hWndEdit, EM_STREAMOUT, SF_RTF, (LPARAM)&es);
}
CloseHandle(hFile);
return TRUE;
}



La difference entre la compréhension et la connaissance est ambigüe... Quelle est la réalité?

1 réponse

BruNews Messages postés 21040 Date d'inscription jeudi 23 janvier 2003 Statut Modérateur Dernière intervention 21 août 2019
11 janv. 2010 à 18:41
SendMessage(hWndEdit,EM_SETEVENTMASK,0,ENM_CHANGE | ENM_SELCHANGE);

ciao...
BruNews, MVP VC++
0
Rejoignez-nous