Using FillRect() in C++

shubhank srivastava picture shubhank srivastava · Apr 19, 2013 · Viewed 14.7k times · Source

I am new to using Graphics in Visual C++. I am just trying to make a rectangle filled with a color. Need help to correct this...

RECT rect;
HDC hdc;
hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
rect.left=30;
rect.right=100;
rect.top=50;
rect.bottom=200;
FillRect(hdc,&rect,(HBRUSH)(RGB(40,151,151)));

The error is:

ERROR: The variable 'rect' is being used without being initialized.

Answer

Jerry Coffin picture Jerry Coffin · Apr 19, 2013

This will normally be a warning, not an error. In this case, it also appears to be spurious.

It might work better if you initialize it something like:

HDC hdc=CreateDC(TEXT("DISPLAY"),NULL,NULL,NULL);
RECT rect = {30, 50, 100, 200};
HBRUSH brush = CreateSolidBrush(RGB(50, 151, 151));

FillRect(hdc, &rect, brush);

DeleteObject(brush);

Do note the use of CreateSolidBrush -- casting a color to an HBRUSH seems unlikely to work.