Results 1 to 2 of 2

Thread: changing a static window's background color

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2001
    Location
    Sterling Heights, MI, USA
    Posts
    11

    changing a static window's background color

    I want to create a static control that functions as a color selector. I am not having any problem using the common dialog to select a color, but I would like the color of the static control to change to whatever was selected. I scoured the web and this forum for answers, but the only thing I came up with was incomplete examples for MFC. If anyone can tell me how I can change the background color of a static control without using MFC, I would really appreciate it.

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    This may help you:
    This can easily be accomplished by handling (NOT sending) the messages WM_CTLCOLOREDIT and WM_CTLCOLORSTATIC. Why the STATIC one? Well if the edit control is disabled then it sends WM_CTLCOLORSTATIC instead, so you need to be aware of which you are going to use or both.

    To change the background colour you return the handle to a brush, which windows will use to paint the background of the control. To change the text color, you can use SetTextColor() on the HDC which is passed to you in the wParam parameter. You may also want to use SetBkMode() to set the text drawing mode to TRANSPARENT, otherwise your background color won't show through if you are changing it.

    For example, to make the background black, you would do:

    case WM_CTLCOLOREDIT:
    return (LRESULT)GetStockObject(BLACK_BRUSH);

    If your edit control is disabled, substitute WM_CTLCOLORSTATIC for WM_CTLCOLOREDIT.

    But if your background is black, you probably don't want your text to be black. So we'll make it white:

    case WM_CTLCOLOREDIT:
    SetBkMode((HDC)wParam, TRANSPARENT);
    SetTextColor((HDC)wParam, RGB(255, 255, 255));
    return (LRESULT)GetStockObject(BLACK_BRUSH);

    If you only want to change the color for a single control, you can match the ID of the control you want to change to the ID of the hwnd you get in lParam:

    case WM_CTLCOLOREDIT:
    if(IDC_OF_THE_CONTROL == GetDlgCtrlID((HWND)lParam)){
    SetBkMode((HDC)wParam), TRANSPARENT);
    SetTextColor((HDC)wParam, RGB(255, 255, 255));
    return (LRESULT)GetStockObject(BLACK_BRUSH);
    }
    break;

    If you haven't guessed by now, you can also use this to change the colors of STATIC controls, and a few others. (look up the WM_CTLCOLOR* messages in MSDN).
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width