Understanding device contexts

As a relative newcomer to MFC, I see Device Contexts (DCs) a lot. I vaguely understand that this is connected with drawing, but the features are not well explained anywhere that I can find. What does creating a “compatible device context” mean and why is it important? What does SelectObject do, and how do I first perform compatibility with DC?

+7
mfc device-context
source share
1 answer

The device context is just the place where the drawing takes place, so if you have two different DCs, you draw in two different places. A kind of file descriptor.

Device contexts may refer to real estate on the screen or to bitmaps that are just stored in memory, and possibly elsewhere, these are the ones I can think of at the moment.

Compatible contexts are those that have the same basic pixel organization, which means the number of bits per pixel, bytes per pixel, color organization, etc. In-memory bitmap contexts can have any organization you want, but your screen contexts will be connected (eventually) with buffers on your video card that (depending on mode, etc.) have a very specific pixel organization.

The presence of compatible contexts means that it is effective for transferring image data between them, since little or no data translation is required. On the other hand, if you have a 256 color palette, an 8-bit card, and you are trying to split it into a screen with 8 bits of each of RGBA per pixel, each last pixel will require significant massaging when copying it, and thus copying incompatible bitmaps images are much slower. According to the Win32 SDK documentation, at least BitBlt () and StretchBlt () "will convert the source color format to the destination format", so this can be done.

Explore CreateCompatibleDC () and CreateCompatibleBitmap () as starting points for creating drawing contexts that are compatible with existing ones.

SelectObject () determines which resources are currently active in the device context. The context has the current pen, brush, font, and bitmap. They make many other GDI calls easier, allowing you to specify fewer parameters. For example, you do not need to specify the font when using TextOut (), but if you want to change the font, then when SelectObject () comes in. If you provide a SelectObject () font descriptor, Value is the font descriptor that acted, and subsequent operations use the new font. The behavior is the same for other types of resources, pens, brushes, etc.

+11
source share

All Articles