Hi
I am writing a program using the Windows API, the whole background of the window is white but when I put in static text the background of the text is the standard text.
How do I change the background of the window to the standard grey?
Thanks
Printable View
Hi
I am writing a program using the Windows API, the whole background of the window is white but when I put in static text the background of the text is the standard text.
How do I change the background of the window to the standard grey?
Thanks
Part I - Gray background
Well to change the background color you need to specify a new brush for your painting. This is done when you are registering your class. Change the "hbrBackground" member of the WNDCLASS or WNDCLASSEX struct to ...
That will return to you the standard gray brush. There are other things you can pass to the function but you said you wanted a gray brush.Code:(HBRUSH)GetStockObject( GRAY_BRUSH );
Part II - Transparent background text
If you just use TextOut now to draw your text you will notice that the background of the text is white! This is obviously not what you want! You want the background to be transparent. Don't worry it is a simple function call away.
The only valid modes are:Code:SetBkMode( HDC hDC, int iBkMode );
TRANSPARENT
OPAQUE
So your call might look like this..
That should do it.Code:SetBkMode( hDC, TRANSPARENT );
SetBkColor() will change the colour under the text.
>>How do I change the background of the window to the standard grey?
Did you register a window class? If so change the background brush value.
look up WNDCLASSEX
WndClass.hbrBackground =(HBRUSH) COLOR_WINDOWFRAME;
or change the default properties on the fly
Else simple and bad paint handler;Code:hdc=GetDC(hWnd);
//change teh default colour in the DC to gray
SetDCBrushColor(hdc, GetStockObject(GRAY_BRUSH);
ReleaseDC(hWnd,hdc);
GetClientRect(hWnd, &theRect);
//ask to redraw whole client area so we see new colour
InvalidateRect(hWnd,&theRect,TRUE);
UpdateWindow(hWnd);
Code:case WM_PAINT:
//find the area to paint
GetUpdateRect(hWnd, &theRect, 0);
if (IsRectEmpty(&theRect))
{
GetClientRect(hWnd, &theRect);
}
//create the GDI
hBrush=GetStockObject(GRAY_BRUSH);
BeginPaint(hWnd, &ps);
FillRect(ps.hdc,&theRect,hBrush);
EndPaint(hWnd, &ps);
DeleteObject(hBrush);//no 100% needed but is good practice to clean up GDI's