fredsor
Messages postés
198
Date d'inscription
lundi 24 avril 2006
Statut
Membre
Dernière intervention
3 avril 2008
11 juin 2007 à 15:57
Toujours rien
pour tester J'ai donc créé un nouveau projet tres basique ne contenant q'une listbox, composée de 2 items...
Voici le code complet qui est donc court, mais qui ne colore pas les lignes comme souhaité...
J'ai hate de savoir ce que t'en pense, cette fois je crois avoir respecté tout ce que tu m'a di. Et pour assurer le coup, je gère le WM_DRAWITEM pour la fenetre principale, mais également en sous-classement :
#include "stdafx.h"
#include "stdlib.h"
#define ID_EDIT 102
HWND hWnd;
HWND hBtn;
LPDRAWITEMSTRUCT info;
HINSTANCE hinst;
WNDPROC ListBoxProcSousClassement;
int iCurrItem;
HBRUSH fondRouge = CreateSolidBrush(RGB(255,0,0));
HBRUSH fondBleu = CreateSolidBrush(RGB(0,0,255));
HBRUSH fondVert = CreateSolidBrush(RGB(0,255,0));
COLORREF rouge = RGB(255,0,0);
COLORREF bleu = RGB(0,0,255);
COLORREF vert = RGB(0,255,0);
WCHAR *GetText(HWND hWnd)
{
WCHAR *Text;
unsigned int TextLen = 0;
TextLen = GetWindowTextLength(hWnd);
if(TextLen){
Text = new WCHAR[(TextLen + 2)];
TextLen = GetWindowText(hWnd, Text, (TextLen + 1));
}
return Text;
}
LRESULT CALLBACK ListBoxProc(HWND hwnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
switch(Msg)
{
case WM_DRAWITEM:
{
RECT rt2;
HDC hdc2;
if(iCurrItem==0)
{
/* info = (LPDRAWITEMSTRUCT) lParam;
SetTextColor(info->hDC, bleu);
SetBkMode(info->hDC, TRANSPARENT);
FillRect(info->hDC, &info->rcItem, fondRouge);
DrawText(info->hDC, TEXT("ligneRouge"), 10, &info->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
*/
hdc2= BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt2);
SetTextColor(hdc2, bleu);
SetBkMode(hdc2, TRANSPARENT);
FillRect(hdc2, &rt2, fondRouge);
DrawText(hdc2, TEXT("ligneRouge"), 10, &rt2, DT_CENTER);
break;
}
if(iCurrItem==1)
{
/* info = (LPDRAWITEMSTRUCT) lParam;
SetTextColor(info->hDC, rouge);
SetBkMode(info->hDC, TRANSPARENT);
FillRect(info->hDC, &info->rcItem, fondBleu);
DrawText(info->hDC, TEXT("ligneBleue"), 10, &info->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
*/
hdc2= BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rt2);
SetTextColor(hdc2, rouge);
SetBkMode(hdc2, TRANSPARENT);
FillRect(hdc2, &rt2, fondBleu);
DrawText(hdc2, TEXT("ligneBleue"), 10, &rt2, DT_CENTER);
break;
}
}
}
//retour la fonction de gestion des édits
return CallWindowProc(ListBoxProcSousClassement, hwnd, Msg, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
switch (message)
{
case WM_CREATE:
hBtn = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("LISTBOX"), NULL, WS_VISIBLE|WS_CHILD|LBS_NOTIFY|WS_VSCROLL|ES_AUTOVSCROLL|BS_OWNERDRAW,
0, 40, 240, 100, hWnd, HMENU(ID_EDIT), hinst, NULL);
iCurrItem=SendMessage(GetDlgItem(hWnd, ID_EDIT), LB_ADDSTRING, 0, (LPARAM)TEXT("Ligne 1a"));
SendMessage (GetDlgItem(hWnd, ID_EDIT), LB_SETITEMDATA,(WPARAM) iCurrItem, (LPARAM) 0);
iCurrItem=SendMessage(GetDlgItem(hWnd, ID_EDIT), LB_ADDSTRING, 0, (LPARAM)TEXT("Ligne 2aa"));
SendMessage (GetDlgItem(hWnd, ID_EDIT), LB_SETITEMDATA,(WPARAM) iCurrItem, (LPARAM) 1);
ListBoxProcSousClassement = (WNDPROC)SetWindowLong(GetDlgItem(hWnd,ID_EDIT), GWL_WNDPROC,(LONG)ListBoxProc);
if (!hBtn)
{
return FALSE;
}
break;
case WM_DRAWITEM:
{
RECT rt2;
HDC hdc2;
if(iCurrItem==0)
{
/* info = (LPDRAWITEMSTRUCT) lParam;
SetTextColor(info->hDC, bleu);
SetBkMode(info->hDC, TRANSPARENT);
FillRect(info->hDC, &info->rcItem, fondRouge);
DrawText(info->hDC, TEXT("ligneRouge"), 10, &info->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
*/
hdc2= BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt2);
SetTextColor(hdc2, bleu);
SetBkMode(hdc2, TRANSPARENT);
FillRect(hdc2, &rt2, fondRouge);
DrawText(hdc2, TEXT("ligneRouge"), 10, &rt2, DT_CENTER);
break;
}
if(iCurrItem==1)
{
/* info = (LPDRAWITEMSTRUCT) lParam;
SetTextColor(info->hDC, rouge);
SetBkMode(info->hDC, TRANSPARENT);
FillRect(info->hDC, &info->rcItem, fondBleu);
DrawText(info->hDC, TEXT("ligneBleue"), 10, &info->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
*/
hdc2= BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt2);
SetTextColor(hdc2, rouge);
SetBkMode(hdc2, TRANSPARENT);
FillRect(hdc2, &rt2, fondBleu);
DrawText(hdc2, TEXT("ligneBleue"), 10, &rt2, DT_CENTER);
break;
}
}
case WM_CTLCOLORLISTBOX://Dessin du contrôle TAB -> passe bien ici
{
return (BOOL) fondVert;
}
case WM_PAINT:
RECT rt;
hdc = BeginPaint(hWnd, &ps);
GetClientRect(hWnd, &rt);
FillRect(hdc, &rt, (HBRUSH) (1));
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
DestroyWindow(hWnd);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
ATOM MyRegisterClass(HINSTANCE hInstance, LPTSTR szWindowClass){
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC) WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = 0;// LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wc.hCursor = 0;
wc.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = 0;
wc.lpszClassName = szWindowClass;
return RegisterClass(&wc);
}
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow){
MSG msg;
hinst=hInstance;
MyRegisterClass(hInstance, TEXT("szWindowClass"));
hWnd = CreateWindow(TEXT("szWindowClass"), TEXT("Exemple 4"), WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
J'essaie de gérer de deux maniere les drawitem, tu verra ce qui te semble le plus judicieux (peut etre aucun des deux ^^) mais je crois que ca ne passe pas dedans de toute facon...
Peut etre que les listbox ne permettent tout simplement pas cela..