Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. List box background

List box background

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sthalasayanam
    wrote on last edited by
    #1

    What is the procedure to change the background of list box? NSS

    G 1 Reply Last reply
    0
    • S sthalasayanam

      What is the procedure to change the background of list box? NSS

      G Offline
      G Offline
      Gavin Taylor
      wrote on last edited by
      #2

      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 to NULL 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 the WM_CTLCOLORLISTBOX message. Gavin Taylor w: http://www.gavspace.com

      S 1 Reply Last reply
      0
      • G Gavin Taylor

        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 to NULL 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 the WM_CTLCOLORLISTBOX message. Gavin Taylor w: http://www.gavspace.com

        S Offline
        S Offline
        sthalasayanam
        wrote on last edited by
        #3

        Thanks a lot for your answer..

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups