Thread: Window color(LISTBOX)? PLEASE HELP!

  1. #1
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question Window color(LISTBOX)? PLEASE HELP!

    I tried FillRect to fill the background color & text color of a listbox, but it didn't work. What do I do to set the background color of a listbox(Text color, too)? Please give example!
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    23
    Assuming your program is dialog base and you're using MFC, try this:

    declare first 3 variable like these:

    COLORREF m_crText;
    COLORREF m_crBackground;
    CBrush m_brush;

    in your OnInitDialog function, code these:

    m_crText = GetSysColor(COLOR_WINDOWTEXT);
    m_crBackground = GetSysColor(COLOR_WINDOW);
    m_brush.CreateSolidBrush (m_crBackground);

    and assuming you have a button to change the text color and background color, code the click function as follows:

    void CMyDialog::OnBackColor()
    {
    CColorDialog dlg;
    if(dlg.Domodal() == IDOK)
    {
    m_crBackground = dlg.GetColor();
    if(m_brush.Detach())
    m_brush.DeleteObject();
    m_brush.CreateSolidBrush(m_crBackground);

    //example m_list is the object of your ListBox
    m_list.Invalidate();
    }
    }

    void CMyDialog::OnTextColor()
    {
    CColorDialog dlg;
    if(dlg.Domodal() == IDOK)
    {
    m_crText = dlg.GetColor();
    m_list.Invalidate();
    }
    }

    and handle the WM_CTLCOLOR message as follows:

    HBRUSH CMyDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    if(nCtlColor == CTLCOLOR_LISTBOX) //CTLCOLOR_LISTBOX is define at WinUser.h
    {
    pDC->SetBkMode(TRANSPARENT); //experiment this if u want
    pDC->SetTextColor(m_crText);
    return (HBRUSH) m_brush.GetSafeHandle();
    }
    else
    return CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    }

    Thats it, I hope it helps...
    If they can, why can't I...

  3. #3
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Post That did not...

    I use Dev-C++ 4.01. I don't use dialog boxes, either. I need code for a regular LISTBOX.
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You can do it by trapping a WM_CTLCOLORLISTBOX message.
    zen

  5. #5
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Question And how...

    And HOW might I "trap" the message?
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  6. #6
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    All messages affecting your window will be sent to your wndproc. You can trap them or pass them on to the default wndproc. Whenever a window that contains a listbox as a child window is repainted it will be sent a WM_CTLCOLORLISTBOX message, you can then either deal with this message to alter the way in which some aspects of the listbox are drawn or let the default wndproc handle it, in which case you'll get the default behavior.
    zen

  7. #7
    Programming is fun, mkay?
    Join Date
    Oct 2001
    Posts
    490

    Exclamation LoL!

    Could you just give me an example? The listbox handle is listWin;
    Website(s): http://www16.brinkster.com/trifaze/

    E-mail: [email protected]

    ---------------------------------
    C++ Environment: MSVC++ 6.0; Dev-C++ 4.0/4.1
    DirectX Version: 9.0b
    DX SDK: DirectX 8.1 SDK

  8. #8
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Perhaps you might learn something by trying it to work it out for yourself (or if you used a search engine you could probably find some examples that would help).
    zen

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Or use owner draw listbox's and the WM_DRAWITEM msg.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C or C++
    By AcerN30 in forum Game Programming
    Replies: 41
    Last Post: 05-30-2008, 06:57 PM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM