List box background
-
What is the procedure to change the background of list box? NSS
-
What is the procedure to change the background of list box? NSS
The best way is to override the WM_CTLCOLORLISTBOX[^] message. You'll need something like
HBRUSH m_hBrush = NULL;
in your class definition and check you set it toNULL
in your classes constructor. Once done, override the DefWindowProc for your controls parent window and add to it...switch( message )
{
case WM_CTLCOLORLISTBOX:
{
if( m_hBrush == NULL )
{ // Fist time we're called so create the brush
LOGBRUSH lb;
ZeroMemory( & lb, sizeof( LOGBRUSH ) );
lb.lbStyle = BS_SOLID;
lb.lbColor = RGB( 0, 0, 255 );
m_hBrush = CreateBrushIndirect( & lb );
}SetBkColor( ( HDC ) wParam, RGB( 0, 0, 255 ) ); // Text background to Blue SetTextColor( ( HDC ) wParam, RGB( 255, 255, 255 ) ); // Text color to white return ( LRESULT ) m\_hBrush; }; break;
}
Obviously when the window / class is destroyed you'll need to destroy the brush you've alllocated so add something like
if( m_hBrush != NULL ) DestroyObject( m_hBrush );
aswell. This will currently set every list box on your window to the colour Blue, if you want to be more selective all you need to do is compare the list boxes handle to the lParam of theWM_CTLCOLORLISTBOX
message. Gavin Taylor w: http://www.gavspace.com -
The best way is to override the WM_CTLCOLORLISTBOX[^] message. You'll need something like
HBRUSH m_hBrush = NULL;
in your class definition and check you set it toNULL
in your classes constructor. Once done, override the DefWindowProc for your controls parent window and add to it...switch( message )
{
case WM_CTLCOLORLISTBOX:
{
if( m_hBrush == NULL )
{ // Fist time we're called so create the brush
LOGBRUSH lb;
ZeroMemory( & lb, sizeof( LOGBRUSH ) );
lb.lbStyle = BS_SOLID;
lb.lbColor = RGB( 0, 0, 255 );
m_hBrush = CreateBrushIndirect( & lb );
}SetBkColor( ( HDC ) wParam, RGB( 0, 0, 255 ) ); // Text background to Blue SetTextColor( ( HDC ) wParam, RGB( 255, 255, 255 ) ); // Text color to white return ( LRESULT ) m\_hBrush; }; break;
}
Obviously when the window / class is destroyed you'll need to destroy the brush you've alllocated so add something like
if( m_hBrush != NULL ) DestroyObject( m_hBrush );
aswell. This will currently set every list box on your window to the colour Blue, if you want to be more selective all you need to do is compare the list boxes handle to the lParam of theWM_CTLCOLORLISTBOX
message. Gavin Taylor w: http://www.gavspace.comThanks a lot for your answer..