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

Raymond Chen

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.

4 comments

Discussion is closed. Login to edit/delete existing comments.

  • Mystery Man 0

    “DC”? What does that stand for? (Data center, domain controller, device context, direct connection? All of these seem wrong.)

    • Erik Fjeldstrom 0

      “Device context” is the only one that makes sense in this article, and is mentioned as such in the linked article.

    • Ivan K 0

      Seemed obvious based on the blurb text about something something “locked in” : the interior design of the penitentiary in Washington DC. I was hoping for a an article about someone’s colorful past.

    • Antonio Rodríguez 0

      Given that the article speaks about pens, brushes and colors, “DC” can refer either to a painter’s canvas or to some computer graphics related concept. The article also says pens and brushes can be destroyed, which discards the canvas thing (unless the artist is in the middle of a performance which involves destroying the tools used to create the artwork, in a try to expose the uniqueness of the creative process or some nonsense like that…).

Feedback usabilla icon