簡體   English   中英

如何更改畫筆的顏色

[英]How to change a brush's color

好吧,假設我有一把刷子,

HBRUSH brush = CreateSolidBrush(RGB(0, 0, 0));

我想改變它的顏色。 不要一遍又一遍地調用CreateSolidBrushDeleteObject

就像在這個例子中一樣,

#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);
}

如上所示,我不想一次又一次地使用CreateSolidBrushDeleteObject的原因是它很慢,我需要能夠快速更改畫筆的顏色。

我找到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM