[英]How to change a brush's color
好吧,假设我有一把刷子,
HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0));
我想改变它的颜色。 不要一遍又一遍地调用CreateSolidBrush
和DeleteObject
。
就像在这个例子中一样,
#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);
}
如上所示,我不想一次又一次地使用CreateSolidBrush
和DeleteObject
的原因是它很慢,我需要能够快速更改画笔的颜色。
我找到SetDCBrushColor
。 哪个可以改变所选画笔的颜色? 但即使在将它选择到上下文之后,似乎也没有改变我的画笔。
这就是为什么我想知道是否有任何替代SetDCBrushColor
。 这样我就可以在FillRect
中使用我的画笔。
任何帮助是极大的赞赏。 提前致谢。
事实上,我很抱歉问这个问题。 我找到了答案。
这里是:
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.
在上面的片段中;
调用GetStockObject(DC_BRUSH)
返回 DC 画笔。
收到刷子后,我可以用上面提到的改变它的颜色。 SetDCBrushColor
我还建议保存颜色,例如,
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, randomColor);
SetDCBrushColor(hdc, holdPreviousBrushColor);
所以现在问题中的代码片段看起来像,
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.