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.
Your code fails because of this code:
You cannot cast an RGB color to an
HBRUSH
in any meaningful way. The only way to obtain anHBRUSH
is a ask the system to give you one.So, you need to create a real brush using one of the API functions for that purpose. For example,
CreateSolidBrush
.When you have finished with the brush, call
DeleteObject
to return resources to the system.As a general rule, every time you write a cast, regard the code as very suspicious. Always endeavour to write code without casts.
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:
Do note the use of
CreateSolidBrush
-- casting a color to an HBRUSH seems unlikely to work.