Exécuter fonction en appuyant sur enter sur un rich edit

Résolu
Neomaster951 Messages postés 7 Date d'inscription mercredi 4 mai 2005 Statut Membre Dernière intervention 24 novembre 2007 - 14 sept. 2007 à 18:58
Neomaster951 Messages postés 7 Date d'inscription mercredi 4 mai 2005 Statut Membre Dernière intervention 24 novembre 2007 - 17 sept. 2007 à 23:24
Bonjour, j'aimerais que lorsqu'un utilisateur écrive dans une rich edit box, que dès qu'il appuye enter, ça exécute une fonction. J'ai essayé de mettre l'événement WM_KEYDOWN dans mon MainWndProc ci-dessous, mais ça ne fonctionne que lorsque l'utilisateur n'a pas cliqué sur le contrôle :x Un peu d'aide serait vraiment apprécié, merci d'avance ^^

HWND hTypeZone;

LRESULT CALLBACK MainWndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    LoadLibrary("RICHED20.DLL");
   
    switch (uMsg)
    {
        case WM_CREATE:
                 hTypeZone = CreateWindow("RichEdit20A", "", WS_CHILD | WS_VISIBLE | WS_BORDER | ES_WANTRETURN, 5, 410, 610, 24, hwnd, NULL, hinst, NULL);
    }
}

5 réponses

racpp Messages postés 1909 Date d'inscription vendredi 18 juin 2004 Statut Modérateur Dernière intervention 14 novembre 2014 17
17 sept. 2007 à 14:38
La deuxième solution s'appelle le sous-classement. Elle n'est utile que lorsque tu veux capturer des touches autres que Entrée. Voici un code complet que je viens de faire pour tester:

#include <windows.h>
#include <richedit.h>

void ProcessCommand(void)
{
    MessageBox(0,"La fonction est appelée.","Test",0);
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT msg,WPARAM wParam, LPARAM lParam)
{
    static HWND hTypeZone,hSendButton;
    switch(msg)
    {
    case WM_CREATE:
        {
            hTypeZone = CreateWindow("RichEdit20A","", WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 5, 400, 24, hwnd, NULL, 0, NULL);
            hSendButton = CreateWindow("BUTTON", "Envoyer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 420 , 5, 80, 24, hwnd, (HMENU)IDOK, 0, NULL);
            return 0;
        }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_COMMAND:
        if(LOWORD(wParam)==IDOK) ProcessCommand();
        break;
    default:
        break;
    }
    return DefWindowProc(hwnd,msg,wParam,lParam);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR cmd,int show)
{
    HINSTANCE hDll=LoadLibrary("riched20.dll");
    WNDCLASSEX wc;
    memset(&wc,0,sizeof(WNDCLASSEX));
    wc.cbSize=sizeof(WNDCLASSEX);
    wc.hInstance=hInst;
    wc.lpfnWndProc=WndProc;
    wc.hCursor=LoadCursor(0,IDC_ARROW);
    wc.hbrBackground=(HBRUSH)COLOR_WINDOW;
    wc.lpszClassName="test";
    RegisterClassEx(&wc);
    HWND hwnd=CreateWindow("test","test", WS_SYSMENU | WS_MINIMIZEBOX,0,0,520,70,0,0,0,0);
    ShowWindow(hwnd,1);
    UpdateWindow(hwnd);
    MSG Msg;
    while (GetMessage(&Msg, NULL, 0, 0))
    {
        if (!IsDialogMessage(hwnd, &Msg))
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }
    FreeLibrary(hDll);
    return 0;
}

J'ai testé et je n'ai remarqué aucune anomalie.
3
cs_juju12 Messages postés 966 Date d'inscription samedi 3 avril 2004 Statut Membre Dernière intervention 4 mars 2010 4
14 sept. 2007 à 19:50
En fait tu peux faire ca avec le style ES_WANTRETURN : quand tu fais enter il envoie un message WM_COMMAND en provenance du bouton de commande par défaut, donc si tu as ça sur ta fenêtre...
0
racpp Messages postés 1909 Date d'inscription vendredi 18 juin 2004 Statut Modérateur Dernière intervention 14 novembre 2014 17
14 sept. 2007 à 21:49
Salut,
Le style ES_WANTRETURN permet juste à un richedit, ayant aussi le style ES_MULTILINE,  de passer à une nouvelle ligne après appui sur la touche Entrée. Sinon, cet appui sera capturé par la boite de dialogue ou la fenêtre mère. Pour appeler une fonction à l'appui sur Entrée, on défini un bouton par défaut ayant BS_DEFPUSHBUTTON comme style et IDOK comme identificateur:
hOk=CreateWindow("button","Ok",WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,10,10,60,20,hwndparent,(HMENU)IDOK,0,0);
Si le parent est une boite de dialogue, cela marchera directement. si c'est une fenêtre, la boucle des messages doit ressembler à ceci:
    while (GetMessage(&Msg, 0, 0, 0))
    {
        if (!IsDialogMessage(hwnd, &Msg))
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
    }
L'appui sur Entré sera considéré comme un clic sur le bouton par défaut, et ce quelque soit le controle ayant le focus.
0
Neomaster951 Messages postés 7 Date d'inscription mercredi 4 mai 2005 Statut Membre Dernière intervention 24 novembre 2007
17 sept. 2007 à 12:32
J'ai essayé ça:

hSendButton = CreateWindow(

"BUTTON",
"Envoyer", WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON, 620, 430, 68, 23, hwnd, (HMENU)IDOK, hinst, NULL);

case

WM_COMMAND;

if(LOWORD(wParam) == IDOK)
   ProcessCommand();

Le hic: dès que je tape un caractère dans mon rich edit, il exécute ProcessCommand(); :x

Sinon, j'ai trouvé une autre solution, mais qui me cause également du trouble:

WNDPROC wpOrigEditProc;
hTypeZone = CreateWindow(
"RichEdit20A",
"", WS_CHILD | WS_VISIBLE | WS_BORDER, 5, 430, 610, 24, hwnd, NULL, hinst, NULL);

wpOrigEditProc = (WNDPROC)SetWindowLong(hTypeZone, GWL_WNDPROC, (LONG)EditProc);

LRESULT APIENTRY EditProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
   
if(uMsg WM_KEYDOWN && wParam VK_RETURN)
   {
      ProcessCommand();
     
return 0;
   }

return CallWindowProc(wpOrigEditProc, hwnd, uMsg, wParam, lParam);
}

Bon... la, si je lance mon application a partir de visual studio -> Start debugging, je tape mon texte et quand j'appuye sur enter ça exécute la commande sans problème. Le prob, c'est lorsque je part mon application hors de visual studio. Elle refuse d'exécuter la fonction quand j'appuye sur enter, mais bizarrement après quelques messages envoyés en appuyant sur le bouton, ça marche quand j'appuye sur enter...
 
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Neomaster951 Messages postés 7 Date d'inscription mercredi 4 mai 2005 Statut Membre Dernière intervention 24 novembre 2007
17 sept. 2007 à 23:24
Ah ouais, ça fonctionne super la :)

J'ai peut-être fait quelque chose de mauvais lors de ma première tentative...

Merci beaucoup ^^
0
Rejoignez-nous