the

WindowClass.hbrBackground=(HBRUSH) CreateSolidBrush(RGB(0,255,0));

change the all windows backcolor, registed with these class(WindowClasss).
and for change the DC\window backcolor we need use WM_ERASEBKGND or other messages for do it(i don't remember how).
but the SetDCBrushColor() do the job, right? but seems be ignored :(
i'm using it in WM_PAINT messages.
can anyone advice me more for change backcolor?

Something like this?

hbrBackground = CreateSolidBrush(RGB(241, 245, 251));
//
case WM_PAINT:
{
    LPPAINTSTRUCT lpps = new PAINTSTRUCT;
    HDC hdc = BeginPaint(hWnd, lpps);
    FillRect(hdc, &lpps->rcPaint, hbrBackground);
    // other code
}
Comments
thanks

it's a nice thot ;)
but i belive the FillRect(), isn't the same of backcolor, right?

This may be what you're after.

LPPAINTSTRUCT lpps = new PAINTSTRUCT;
HDC hdc = BeginPaint(hWnd, lpps);
SelectObject(hdc, GetStockObject(DC_BRUSH));
SetDCBrushColor(hdc, RGB(180, 0, 0));
Rectangle(hdc, lpps->rcPaint.left, lpps->rcPaint.top, lpps->rcPaint.right, lpps->rcPaint.bottom);
//
Comments
thanks for all

thanks, but can you give me a nice link about SetDCBrushColor() and SetDCPenColor()?
anotherthing: if we use SelectObject(), we must DeleteObject(), right?

Edited 2 Years Ago by cambalinho

heres my actual code in WM_PAINT message:

case WM_PAINT:
            HDC hdc;
            RECT rect, textrect;
            PAINTSTRUCT a;

            HGDIOBJ original;

            GetClientRect(hwnd,&rect); //getting the actual client rectangle

            hdc=BeginPaint(hwnd,&a); //for getting DC for start using the GDI functions

            original=SelectObject(hdc,GetStockObject(DC_PEN)); //Getting the original pen(i don't know if includes the brush)

            SetBkMode(hdc,TRANSPARENT); //i must put it transparent, or the text will show me it's rectangle backcolor

            SelectObject(hdc, GetStockObject(DC_BRUSH)); //selecting the brush
            SetDCBrushColor(hdc, RGB(0, 0, 255)); //changing the brush color (in these case is the backcolor)

            SelectObject(hdc, GetStockObject(DC_PEN)); //selecting the pen
            SetDCPenColor(hdc,RGB(255,0,0)); // changing the pen color (remember these is the shapes lines and not text\foreground colors

            SetTextColor(hdc,RGB(0,0,255)); //changing the text color

            Rectangle(hdc,rect.left, rect.top,rect.right,rect.bottom); //show us a rectangle with green backcolor (the SetDCBrushColor()), with red line (the SetDCPenColor())

            DrawText(hdc,"hello world\nhi\thello mother\nhello my dog",-1, &textrect, DT_CALCRECT); // getting the text rectangle

            rect.top=rect.bottom/2-textrect.bottom/2; //change is rectangle for show us in vertical center too

            SetTextColor(hdc,RGB(0,255,0)); //changing the text color

            DrawText(hdc,"hello world\nhi\thello mother\nhello my dog",-1, &rect, DT_CENTER | DT_EXPANDTABS); //draw the text

            EndPaint(hwnd,&a); //the BeginPaint() must be ended with EndPaint()

            SelectObject(hdc,original); //i save the original PEN and now i select it

            DeleteObject(original); //the SelectObject() must be deleted with DeleteObject()

            return 0;
  • when we speak about brush is like speak about the background\backcolor;
  • when we speak about the pen is like speak about the shaped lines;
  • so when we use DrawText() the SetTextColor() change the text color, but the SetBkColor() change the the text backcolor and not windows backcolor, that's why:

    SetBkMode(hdc,TRANSPARENT);

for don't show us the text backcolor
i hope these tread help more readers ;)
(theres another way for change backcolors, but i belive these is the best and direct to DC)
thanks to all

Edited 2 Years Ago by pritaeas: Code change.

if the code is cool, try these functions:

COLORREF GetWindowBackColor(HDC WindowDC)
{
    return GetDCBrushColor (WindowDC);
}



void SetWindowBackColor(HDC WindowDC, COLORREF BackColor)
{
    HWND HandleWindow= WindowFromDC(WindowDC);
    RECT WindowClientRectangle;
    GetClientRect(HandleWindow,&WindowClientRectangle);
    SelectObject(WindowDC, GetStockObject(DC_BRUSH)); //selecting the brush
    SetDCBrushColor(WindowDC,BackColor);
    SetDCPenColor(WindowDC,BackColor);
    Rectangle(WindowDC,WindowClientRectangle.left, WindowClientRectangle.top,WindowClientRectangle.right,WindowClientRectangle.bottom);
}

what you think? ;)

This question has already been answered. Start a new discussion instead.