Encore moi et mes editbox

Résolu
julienbj Messages postés 452 Date d'inscription jeudi 4 décembre 2003 Statut Membre Dernière intervention 19 décembre 2008 - 27 déc. 2004 à 12:21
julienbj Messages postés 452 Date d'inscription jeudi 4 décembre 2003 Statut Membre Dernière intervention 19 décembre 2008 - 27 déc. 2004 à 16:16
Bon, malgré les InvalidateRect d'hier, j'ai encore un problème (voir EDITBOX ET COULEUR dans le meme forum)

Voici tout d'abord mon code:
#include "edit.h"

HINSTANCE hInst;
t_edit edit;
char buffer[256];
HWND Dlg;
COLORREF background;
COLORREF texte;
static BOOL CHANGE = FALSE;

void EDITChangeColor(t_edit edit)
{
HDC hdcedit = GetDC(edit.handle);
background = edit.background;
texte = edit.texte;
CHANGE = TRUE;
SendMessage(Dlg, WM_CTLCOLOREDIT,(long) hdcedit, (long) edit.handle);
CHANGE = FALSE;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
//A la création de la fenetre
case WM_CREATE:
{unsigned long i 1, j 1;

edit.ID = 10;
EDITCreate(hwnd, &edit, 0x00FFFF00, 0x000000FF, 10, 10, 150, 40, EDITMULTILINE | EDITVISIBLE | EDITBORDER | EDITTABSTOP, TRUE);
EDITChangeColor(edit);
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
return TRUE;
}
case WM_CTLCOLOREDIT:
{
if (CHANGE == TRUE)
{
SetBkColor((HDC) wParam, background);
SetTextColor((HDC) wParam, texte);
}
else //if (CHANGE == FALSE);
{
background = GetBkColor((HDC) wParam);
texte = GetTextColor((HDC) wParam);
SetBkColor((HDC) wParam, background);
SetTextColor((HDC) wParam, texte);
}
return TRUE;
}
case WM_COMMAND:
{
if (LOWORD(wParam) == edit.ID)
{
return TRUE;
}
return FALSE;
}
//A la destruction de la fenetre
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}

}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
WNDCLASSEX config;

//Paramétrage de la fenêtre principale
config.cbSize = sizeof(WNDCLASSEX);
config.style = CS_HREDRAW | CS_VREDRAW;
config.lpfnWndProc = WndProc;
config.cbClsExtra = 0;
config.cbWndExtra = 0;
config.hInstance = hInstance;
config.hIcon = NULL;
config.hCursor = LoadCursor(NULL, IDC_ARROW);
config.hbrBackground = (HBRUSH) COLOR_APPWORKSPACE + 1;
config.lpszClassName = "test";
config.lpszMenuName = NULL;
config.hIconSm = NULL;
if (!RegisterClassEx(&config))
exit(1);

//On passe l'hinstance de fenetre en global
hInst = hInstance;

//Création de la fenêtre principale
Dlg = CreateWindowEx(WS_EX_WINDOWEDGE, config.lpszClassName, "test", WS_OVERLAPPEDWINDOW | WS_VSCROLL, 0, 0, 200, 200, NULL, NULL, hInstance, NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

et voici la structure t_edit:
typedef struct s_edit
{
HWND handle;
long ID;
COLORREF background;
COLORREF texte;
} t_edit;

Bon, en quelques mots mon problème: CA MARCHE PAS!
En fait je cherche à changer la couleur de mon editbox (ce n'est qu'un programme de test pour la fonction), mais rien ne se passe!
L'editbox reste tel que!
Si je met en commenatire la ligne CHANGE = FALSE dans la fonction EDITChangeCouleur(), la par contre ça fonctionne!
J'ai donc essayer de mettre le CHANGE = FALSE à la fin du case WM_CTLCOLOREDIT, mais toujours le meme problème, je change plus de couleur!
Si vous avez des idées...
Je sasi plus quoi tester!
Ah si BruNews, si jamais tu lis le post jusque la, j'ai mis aussi à la place du SendMessage un InvalidateRect comme tu me l'avais indiqué, mais ça change rien! (alors que ça marhait sur les boutons!!)
Merci d'avance

Vive le C
Tchao
[mailto:julienbj@hotmail.com Savon]

4 réponses

ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
27 déc. 2004 à 14:27
Le truc important c'est que c'est ta structure qui doit stocker les couleurs et c'est à partir de cette structure que tu doit modifier les param du HDC fournit par WM_CTLCOLOREDIT

un exemple qui crée un EDIT et change de couleur toutes les secondes aléatoirement

#include <windows.h>

#define ID_EDT	1000

struct edtparam
{
HWND		hEdt;
COLORREF	clrBackground;
COLORREF	clrText;
HBRUSH		hBrush;
};

HINSTANCE g_hAppInstance = NULL;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, int nCmdShow)
{
g_hAppInstance = hInstance;
char szWndClass[] = "TestEdit";
WNDCLASS wc = {0};
wc.style		= CS_HREDRAW|CS_VREDRAW;
wc.hInstance	= g_hAppInstance;
wc.hIcon		= LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor		= LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground= (HBRUSH)(COLOR_BTNFACE+1);
wc.lpfnWndProc	= WndProc;
wc.lpszClassName= szWndClass;
if(!RegisterClass(&wc))
return 0;

HWND hWnd = CreateWindowEx(0, szWndClass, "TestEdit", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, g_hAppInstance, NULL);

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_CREATE :
{
// création structuree, init couleurs et HBRUSH
edtparam* edt = new edtparam;
edt->clrBackground = RGB(0xff, 0xff, 0xff);
edt->clrText = RGB(0x00, 0x00, 0x00);
edt->hBrush = CreateSolidBrush(edt->clrBackground);

// création EDIT et association HWND<->structure
edt->hEdt = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "Hello World", WS_CHILD|WS_VISIBLE,
10, 10, 150, 50, hWnd, (HMENU)ID_EDT, g_hAppInstance, NULL);
SetWindowLong(edt->hEdt, GWL_USERDATA, (LONG)edt);

// timer
SetTimer(hWnd, 0x100, 1000, NULL);

return 0;

}
case WM_DESTROY :
{
// récupération param, destruction HBRUSH et structure
HWND hEdt = GetDlgItem(hWnd, ID_EDT);
edtparam* edt = (edtparam*)GetWindowLong(hEdt, GWL_USERDATA);
DeleteObject(edt->hBrush);
delete edt;
KillTimer(hWnd, 0x100);
PostQuitMessage(0);
break;
}

case WM_TIMER :
{
// récupération param
HWND hEdt = GetDlgItem(hWnd, ID_EDT);
edtparam* edt = (edtparam*)GetWindowLong(hEdt, GWL_USERDATA);

// destruction ancien pinceau, création noubeau avec nouvelles couleurs
DeleteObject(edt->hBrush);
edt->clrBackground = RGB(rand()&0xff, rand()&0xff,rand()&0xff);
edt->clrText = RGB(rand()&0xff, rand()&0xff,rand()&0xff);
edt->hBrush = CreateSolidBrush(edt->clrBackground);

// mise à jour
InvalidateRect(edt->hEdt, NULL, TRUE);
break;
}

case WM_CTLCOLOREDIT :
{
// si c'est notre Edit
HWND hEdt = GetDlgItem(hWnd, ID_EDT);
if(hEdt == (HWND)lParam)
{
// récupération param, affectation au HDC, retour HBRUSH du fond
edtparam* edt = (edtparam*)GetWindowLong(hEdt, GWL_USERDATA);
HDC hdc = (HDC)wParam;
SetBkColor(hdc, edt->clrBackground);
SetTextColor(hdc, edt->clrText);
return (LRESULT)edt->hBrush;
}
break;
}

default :
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
3
ymca2003 Messages postés 2070 Date d'inscription mardi 22 avril 2003 Statut Membre Dernière intervention 3 juillet 2006 7
27 déc. 2004 à 13:41
voici une piste :
crée ta structure t_edit dynamiquement (new, malloc...)
initialise les champs, et crée l' EDIT avec CreateWindow.
associe le t_edit au EDIT avec SetWindowLong ou sauvegarde l'association Id (ou handle) <-> t_edit dans un tableau ou var globale pour pouvoir récupérer un t_edit à parir d'un Id (ou d'un handle)
t_edit* edit = new t_edit;
...
SetWindowLong(hEdt, GWL_USERDATA, (LONG)edit);

lors du traitemment de WM_CTLCOLOREDI, récupère le t_edit à partir du handle par GetWindowLong, et affecte au DC fourni les paramètres de couleiurs mémorisé dans la structure.
t_edit* edit = (t_edit*)GetWindowLong(hEdt, GWL_USERDATA);

Dans ton code, si tu veux modifier les couleurs, modifie les champs de la stucture et appel InvalidateRect qui va se charger d'appeler WM_CTLCOLOREDIT qui va mettre à jours les couleurs avec les nouveaux champs de la structure.
0
julienbj Messages postés 452 Date d'inscription jeudi 4 décembre 2003 Statut Membre Dernière intervention 19 décembre 2008 15
27 déc. 2004 à 14:00
BOOL EDITCreate(HWND windowlocation, t_edit *edit, COLORREF background, COLORREF texte, unsigned int xpos, unsigned int ypos, unsigned int width, unsigned int heigth, unsigned long style, BOOL VIEWERROR)
{if ((edit->handle CreateWindow("EDIT", NULL, WS_CHILD | style, xpos, ypos, width, heigth, windowlocation, NULL, 0, NULL)) NULL)
{
if (VIEWERROR)
EDITVoirErreur(GetLastError());
return FALSE;
}
if (edit->ID > 0)
{
//On spécifie l'ID associé à l'editbox
//SetWindowLong renvoie l'ID d'avant la modification (si on cree l'editbox, cet ID est à 0) =>Test sur GetLastError
if (!SetWindowLong(edit->handle, GWL_ID, edit->ID))
{
if (VIEWERROR)
{
int error = GetLastError();
if (error)
{
EDITVoirErreur(error);
EDITDelete(*edit, VIEWERROR);
return FALSE;
}
}
}
}
edit->background = background;
edit->texte = texte;
//EDITChangeColor(edit);
return TRUE;
}

Voici ma fonction de création d'un edit!
J'ai tester avec un malloc mais ça change rien!
De toute façon je pense pas que ça vienne de ça (mais c'était pas impossible) puisque si je vire dans la fonction EDITChangeColor le CHANGE = FALSE, mon editbox change de couleur!
Je dirais plutôt qu'il doit effectuer la ligne CHANGE = FALSE avant que le message est été traité!
Mais même la encore, ça ne me convient qu'à moitié puisque si je met le CHANGE=FALSe non plus dans la fonction, mais dans le traitement du message, ça fait pareil! Alors que la il devrait avoir d'abord me changer l'arrière plan, puis mettre CHANGE à FALSE!
Et le COLORREF de mon arrièreplan de l'editbox devrait avoir changer ce qui n'est pas le cas!

Vive le C
Tchao
[mailto:julienbj@hotmail.com Savon]
0
julienbj Messages postés 452 Date d'inscription jeudi 4 décembre 2003 Statut Membre Dernière intervention 19 décembre 2008 15
27 déc. 2004 à 16:16
OK, c'est bon, j'ai compris pourkoi ca marchait pas!
Il manquait les HBRUSH et les CreateSolidObject pour peindre le fond!
Merci de ton aide!

Vive le C
Tchao
[mailto:julienbj@hotmail.com Savon]
0
Rejoignez-nous