Colores en CListView

06/09/2003 - 16:53 por duart | Informe spam
Tengo creada una clase vista de tipo CLIstView. Me
gustaria saber si se puede cambia el color de cada una de
las filas de esta vista y como se haria?
 

Leer las respuestas

#1 christian
06/09/2003 - 22:30 | Informe spam
Tengo creada una clase vista de tipo CLIstView. Me
gustaria saber si se puede cambia el color de cada una de
las filas de esta vista y como se haria?
.



primero declara el listview del tipo OWNERDRAW,
recibiras un mensaje por cada fila q vaya a ser dibujada,
te dan el hdc,rect y todo eso.

1-
debes especificar el estilo LVS_OWNERDRAWFIXED al crear
tu listview en la llamada a CreateWindow(...)

2-
debes atrapar el mensaje WM_DRAWITEM en el WndProc
callback que adueña al listview, este mensaje se envia
cada vez q se necesita dibujar, en el MSDN ecuentras la
informacion completa para este mensaje,

en el ejemplo mas abajo llamo a una funcion de mi clase
llamada OnDrawItemMessage(hwnd,message,wParam,lParam)

por ejemplo:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM
wParam, LPARAM lParam)
{

switch (message)
{
case WM_DRAWITEM:

recibes el mensaje aqui, esta callback es la
de la ventana q adueña a al control, aqui

por tanto llama a
tu_listview.OnDrawItemMessage
(hwnd,message,wParam,lParam);

break;
}

return ...
}



3-
sample code

mi clase TListView, con su funcion OnDrawItemMessage
llamada en el WndProc del control:
(copialo y pegalo en notead)

tips:
debes trabajar con visual C++ 6.0, el 5 no tiene definidas
muchas cosas que el 6 si trae. evita prblemas y usa el 6.


CUT THIS LINE:
-

void TListView::OnDrawItemMessage(HWND hwnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
LPDRAWITEMSTRUCT lpdis = (LPDRAWITEMSTRUCT)
lParam;

if ((message != WM_DRAWITEM) || (lpdis->CtlID !=
ListView::idres))
return;

HDC hdc =
lpdis->hDC;
LPRECT lpr = &lpdis-
rcItem;


LPITEMLV lpi;
LPSTR text;
int
totcolsize=lpr->left;
RECT rect;
HICON hiconselected=NULL;


if (lpi = (LPITEMLV)lpdis->itemData)
{

hiconselected = ListView::LoadIcon(lpi-
iconindex);



if (lpdis->itemState & ODS_FOCUS)
{
SetTextColor
(hdc,c_selectedtextcolor);
SelectObject(hdc,hbksel);
if(!PreProcesarHDCFila(hdc,lpr,1))
Rectangle(hdc,lpr-
left,lpr->top,lpr->right,lpr->bottom);


SelectObject(hdc,hbknor);
}
else
{

if (lpi->domark)
{
SetTextColor
(hdc,c_markrowtextcolor);
SelectObject(hdc,hbkrowmark);
if(!PreProcesarHDCFila(hdc,lpr,2))
Rectangle(hdc,lpr-
left,lpr->top,lpr->right,lpr->bottom);


SelectObject(hdc,hbknor);
}
else
{
SelectObject(hdc,hbknor);
SetTextColor
(hdc,c_normaltextcolor);
if(!PreProcesarHDCFila
(hdc,lpr,0))
Rectangle(hdc,lpr-
left-3,lpr->top-3,lpr->right+3,lpr->bottom+3);


}

}





for(int c=0;c<lpi->bcols;c++)
{
int csize = ListView_GetColumnWidth
(ListView::hwnd,c);
rect.top = lpr->top-1;
rect.bottom = lpr->bottom;

if(hiconselected)
{
if(c>0)
{
rect.left = totcolsize + 4;
rect.right = rect.left +
csize - 4;
}
else
{
rect.left = totcolsize +
16;
rect.right = rect.left +
csize - 16;
if(hiconselected)
DrawIconEx
(hdc,0,rect.top,hiconselected,16,16,0,0,DI_NORMAL);
}
}
else
{
rect.left = totcolsize + 4;
rect.right = rect.left +
csize - 4;
}


text = lpi->data[c];

DrawText(hdc,text,lstrlen
(text),&rect,DT_LEFT | DT_SINGLELINE | DT_VCENTER |
DT_WORD_ELLIPSIS);
totcolsize += csize;
}


if(hiconselected)
DestroyIcon(hiconselected);
}

}


-
CUT THIS LINE

Preguntas similares