好吧,假設我有一把刷子,
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.
任何幫助是極大的贊賞。提前致謝。
uj5u.com熱心網友回復:
事實上,我很抱歉問這個問題。我找到了答案。
這里是:
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)回傳直流電刷。
收到刷子后,我可以用上面提到的改變它的顏色。
SetDCBrushColor
我還建議保存顏色,例如,
COLORREF holdPreviousBrushColor = SetDCBrushColor(hdc, randomColor);
SetDCBrushColor(hdc, holdPreviousBrushColor);
這樣您就可以將DC 畫筆設定回原來的顏色。
所以現在問題中的代碼片段看起來像,
#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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/510147.html
標籤:C 温纳皮gdi
