Skip to content

Latest commit

 

History

History
129 lines (106 loc) · 5.53 KB

File metadata and controls

129 lines (106 loc) · 5.53 KB
title Graphic Objects | Microsoft Docs
ms.custom
ms.date 11/04/2016
ms.reviewer
ms.suite
ms.technology
cpp-windows
ms.tgt_pltfrm
ms.topic article
f1_keywords
HRGN
HFONT
HBITMAP
dev_langs
C++
helpviewer_keywords
CRgn class, HRGN handle type
HPEN
objects [C++], graphic
palettes, creating in device context
pens, creating in device context
bitmaps [C++], creating in device contexts
palette objects
memory [C++], display contexts
MFC, graphic objects
regions, creating in device context
CPen class, HPEN handle type
GDI objects [C++]
HRGN
graphic objects
GDI objects [C++], graphic-object classes
CFont class, HFONT handle type
HFONT and class CFont
HBITMAP and class CBitmap
fonts [C++], creating in device context
images [C++], graphic objects
CBitmap class, HBITMAP handle type
HPALETTE and class CPalette
CBrush class, HBRUSH handle type
objects [C++], graphic objects
drawing, in device contexts
device contexts, graphic objects
brushes, creating in device context
region objects
pen objects
GDI [C++], graphic-object classes
graphic objects, creating in device context
HBRUSH and class CBrush
painting and device context
CPalette class, HPALETTE handle type
ms.assetid 41963b25-34b7-4343-8446-34ba516b83ca
caps.latest.revision 11
author mikeblome
ms.author mblome
manager ghogen
translation.priority.ht
cs-cz
de-de
es-es
fr-fr
it-it
ja-jp
ko-kr
pl-pl
pt-br
ru-ru
tr-tr
zh-cn
zh-tw

Graphic Objects

Windows provides a variety of drawing tools to use in device contexts. It provides pens to draw lines, brushes to fill interiors, and fonts to draw text. MFC provides graphic-object classes equivalent to the drawing tools in Windows. The table below shows the available classes and the equivalent Windows graphics device interface (GDI) handle types.

Note

GDI+ is included with Windows XP and is available as a redistributable for Windows NT 4.0 SP6, Windows 2000, Windows 98, and Windows Me. To download the latest redistributable, see http://www.microsoft.com/msdownload/platformsdk/sdkupdate/psdkredist.htm. For more information, see the GDI+ SDK documentation at: http://msdn.microsoft.com/library/default.aspurl=/library/gdicpp/GDIPlus/GDIPlus.asp.

This article explains the use of these graphic-object classes:

Classes for Windows GDI Objects

Class Windows handle type
CPen HPEN
CBrush HBRUSH
CFont HFONT
CBitmap HBITMAP
CPalette HPALETTE
CRgn HRGN

Note

The class CImage provides enhanced bitmap support.

Each graphic-object class in the class library has a constructor that allows you to create graphic objects of that class, which you must then initialize with the appropriate create function, such as CreatePen.

Each graphic-object class in the class library has a cast operator that will cast an MFC object to the associated Windows handle. The resulting handle is valid until the associated object detaches it. Use the object's Detach member function to detach the handle.

The following code casts a CPen object to a Windows handle:

[!code-cppNVC_MFCDocViewSDI#5]

To create a graphic object in a device context

  1. Define a graphic object on the stack frame. Initialize the object with the type-specific create function, such as CreatePen. Alternatively, initialize the object in the constructor. See the discussion of one-stage and two-stage creation, which provides example code.

  2. Select the object into the current device context, saving the old graphic object that was selected before.

  3. When done with the current graphic object, select the old graphic object back into the device context to restore its state.

  4. Allow the frame-allocated graphic object to be deleted automatically when the scope is exited.

Note

If you will be using a graphic object repeatedly, you can allocate it once and select it into a device context each time it is needed. Be sure to delete such an object when you no longer need it.

What do you want to know more about

See Also

Window Objects