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?

Recommended Answers

All 7 Replies

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
}
commented: thanks +0

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);
//
commented: thanks for all +2

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

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

That code looks fine :)

For SelectObject, refer to the MSDN article as to what objects combined with how they are created, need to have their original object value saved and consequently need to be replaced with the original object once the drawing has been completed.
http://msdn.microsoft.com/en-us/library/windows/desktop/dd162957

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? ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.