Ok suppose I have a brush,
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0));
And I want to change it's color.
Not calling CreateSolidBrush
and DeleteObject
on it over and over again.
Like in this example,
#define INFINITY UINT64_MAX // You get the point. I am just calling it many times.
RECT rect = { 0 };
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0)); // Same brush as the one above.
for(uint64_t i = 0; i < INFINITY; i ){
SetRect(&rect, 0, i, i, i 1); // Right angle triangle btw.
// How would I change the color of the brush?
FillRect(hdc, &rect, brush);
}
As shown above, the reason I don't want to use CreateSolidBrush
and DeleteObject
again and again, is that it is slow and I need to be able to change the color of the brush quickly.
I have found SetDCBrushColor
. Which can change the color of the selected brush? But doesn't seem to change my brush even after selecting it to the context.
That's why I'm wondering if there is any alternative to SetDCBrushColor
.
So that I can use my brush in FillRect
.
Any help is greatly appreciated. Thanks in advance.
CodePudding user response:
Actually, I am so sorry for asking this question. I found the answer.
Here it is:
HBRUSH dcbrush = (HBRUSH)::GetStockObject(DC_BRUSH); // Returns the DC brush.
COLORREF randomColor = RGB(69, 69, 69);
SetDCBrushColor(hdc, randomColor); // Changing the DC brush's color.
In the above snippet;
Calling GetStockObject(DC_BRUSH)
returns the DC brush.
After receiving the brush, I can change it's color with the above mentioned.
SetDCBrushColor
I would also suggest saving the color like,
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, randomColor);
SetDCBrushColor(hdc, holdPreviousBrushColor);
So that you set the DC brush back to it's original color.
So now the code snippet in the question would look like,
#define INFINITY UINT64_MAX // You get the point. I am just calling it many times.
RECT rect = { 0 };
HBRUSH brush = (HBRUSH)::GetStockObject(DC_BRUSH);
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, RGB(0, 0, 0));
for(uint64_t i = 0; i < INFINITY; i ){
SetRect(&rect, 0, i, i, i 1); // Right angle triangle btw.
SetDCBrushColor(hdc, /* Any color you want. */);
FillRect(hdc, &rect, brush);
}
SetDCBrushColor(hdc, holdPreviousBrushColor); // Setting the DC brush's color back to its original color