Many drawing
functions that operate on device contexts require that you
select a GDI object into the device context first. To
select a GDI object into a device context, use the
CDC::SelectObject member function. This member function
has several overloaded versions that enable you to select
an object of type CPen, CBrush, CFont, CBitmap, or CRgn
into the device context.
Selecting a
pen into a device context is required for functions that
draw lines. These include simple line drawing functions
(such as Line, Arc) as well as functions that
draw shapes,
as the contour of shapes is drawn using the current pen.
Selecting a
brush is required when a shape (for example, Ellipse,
Rectangle) is drawn. The brush will be used to fill the
interior of the shape.
Select an
object of type CFont into a device context if you wish to
draw text using a specific font.
To use
memory device contexts, you must select a CBitmap object
into them. The CBitmap object must represent either a
monochrome bitmap or a bitmap that is compatible with the
device context.
Selecting a
CRgn object into a device context sets the clipping region
of the device context to the specified region. Doing this
is equivalent to calling the CDC::SelectClipRgn member
function.
In many
situations, it is expected that when you are finished
using a device context, you restore its previous state
including any previous GDI object selections. There are
two ways of doing this. You can save the return value of
SelectObject (which is usually a pointer to a CPen, CBrush,
CFont, or CBitmap object representing the previous
selection) and use it in a subsequent call to SelectObject.
Alternatively, you can use the SaveDC and
RestoreDC member
functions. In either case, it is your responsibility to
delete any GDI objects you created once they are no longer
in use.
A variant of
SelectObject is SelectStockObject; this
CDC member
function enables you to select a GDI stock object into the
device context.
MFC
Example
Note:
UCanCode Advance E-XD++
CAD Drawing and Printing Solution
Source Code Solution for C/C++, .NET V2024 is released!
Contact UCanCode Software
To buy the source code or learn more about with: