I know I can change the color of the DC pen, but what about the other attributes?

Some time ago, I called out the usefulness of the DC pen and DC brush: They always exist, each DC gets one, and you can change their colors dynamically. They are handy when you have some operation that requires a brush or pen, and you don't really want to create one, use it for the operation, and then destroy it.

In a way, the DC pen and DC brush let you pretend that there are no brushes or pens at all, just colors. If there's an operation that requires a brush, you can pretend that it takes a color: Set the DC brush to that color, and then perform the operation with the DC brush.

HBRUSH g_dcBrush = (HBRUSH)GetStockObject(DC_BRUSH);

int FillRect(HDC hdc, const RECT* rect, COLORREF color)
{
 color = SetDCBrushColor(hdc, color);
 auto result = FillRect(hdc, rect, g_dcBrush);
 SetDCBrushColor(hdc, color);
}

The DC brush is a solid color brush.

The DC pen is a solid pen of width zero, which means that it draws one device pixel. All other properties are at their defaults (round join, endcap round).

The only thing you can change about the DC brush and DC pen are their colors. The other attributes are locked. If you need a pen or brush with attributes different from the ones provided by the DC pen or brush, you'll have to create your own brush. Sorry.



Read more here: https://devblogs.microsoft.com/oldnewthing/20210721-00/?p=105467

Content Attribution

This content was originally published by Raymond Chen at Microsoft Developer Blogs, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: