GDI
Biblioteca GDI32.DLL
GDI = permite manipularea elementelor grafice independente de dispozitiv, este un sistem de afisare static, ce permite numai animatii simple;
Dipozitive grafice de iesire:
Sistem de coordonate virtual, sistem de coordonate al dispozitivului;
W95 foloseste coordonate in numere intregi pe 16 biti, W_NT foloseste coordonate pe 32 biti;
Din pdv al programatorului, interfata GDI este formata din citeva sute de rutine si unele tipuri de date, macroinstructiuni si structuri de date;
Tipuri de apeluri de functii
Primitive GDI
Alte aspecte ale GDI:
Contextul de dispozitiv (DC):
Modalitati de obtinere a variabilei handle a DC:
PAINTSTRUCT ps;
hdc = BeginPaint (hwnd, &ps);
...
EndPaint(hwnd, &ps);
structura PAINTSTRUCT contine o structura de tip RECT rcPaint; care defineste dreptunghiul ce cuprinde regiunea invalida a zonei client a ferestrei; se valideaza regiunea invalida.
hdc = GetDC(hwnd);
...
ReleaseDC(hwnd, hdc);
hdc = GetWindowDC(hwnd);
...
ReleaseDC(hwnd, hdc);
Pentru folosirea acestei functii trebuie interceptat mesajul WM_NCPAINT ( non client paint).
hdc = CreateDC(pszDriver, pszDevice, pszOutput, pData);
...
DeleteDC(hdc);
Pentru a obtine o variabila handle a DC pt. spatiul de afisare:
hdc = CreateDC(“DISPLAY”, NULL, NULL, NULL);
hdc = CreateIC(pszDriver, pszDevice, pszOutput, pData);
...
DeleteDC(hdc);
hdcmem = CreateCompatibleDC (hdc)
...
DeleteDC(hdcMem);
hdcMeta = CreateMetaFile(pszFileName);
...
hmf = CloseMetaFile(hdcMeta);
Obtinere informatii despre contextul de dispozitiv: dimensiuni ecran (pixeli, fizice) posibilitati de folosire a culorilor, etc. cu functia GetDeviceCaps (get device capabilities);
ivalue = GetDeviceCaps(hdc, iIndex);
Index Meaning
DRIVERVERSION The device driver version.
TECHNOLOGY Device technology. It can be any one of the following values:
Value Meaning
DT_PLOTTER Vector plotter
DT_RASDISPLAY Raster display
DT_RASPRINTER Raster printer
DT_RASCAMERA Raster camera
DT_CHARSTREAM Character stream
DT_METAFILE Metafile
DT_DISPFILE Display file
If the hdc parameter identifies the device context of an enhanced metafile, the device technology is that of the referenced device as given to the CreateEnhMetaFile function. To determine whether it is an enhanced metafile device context, use the GetObjectType function.
HORZSIZE Width, in millimeters, of the physical screen.
VERTSIZE Height, in millimeters, of the physical screen. Diemensiuni communicate de placa video. Valorile sint aceleasi ca cele returnate de GetSystemMetrics.
HORZRES Width, in pixels, of the screen.
VERTRES Height, in raster lines, of the screen.
LOGPIXELSX Number of pixels per logical inch along the screen width.
LOGPIXELSY Number of pixels per logical inch along the screen height.
BITSPIXEL Number of adjacent color bits for each pixel.
PLANES Number of color planes. Se obtin informatii despre culori
NUMBRUSHES Number of device-specific brushes.
NUMPENS Number of device-specific pens.
NUMFONTS Number of device-specific fonts.
NUMCOLORS Number of entries in the device’s color table, if the device has a color depth of no more than 8 bits per pixel. For devices with greater color depths, -1 is returned.
ASPECTX Relative width of a device pixel used for line drawing.
ASPECTY Relative height of a device pixel used for line drawing.
ASPECTXY Diagonal width of the device pixel used for line drawing.
PDEVICESIZE Reserved.
CLIPCAPS Flag that indicates the clipping capabilities of the device. If the device can clip to a rectangle, it is 1. Otherwise, it is 0.
SIZEPALETTE Number of entries in the system palette. This index is valid only if the device driver sets the RC_PALETTE bit in the RASTERCAPS index and is available only if the driver is compatible with Windows version 3.0 or later.
NUMRESERVED Number of reserved entries in the system palette. This index is valid only if the device driver sets the RC_PALETTE bit in the RASTERCAPS index and is available only if the driver is compatible with Windows version 3.0 or later.
COLORRES Actual color resolution of the device, in bits per pixel. This index is valid only if the device driver sets the RC_PALETTE bit in the RASTERCAPS index and is available only if the driver is compatible with Windows version 3.0 or later.
PHYSICALWIDTH For printing devices: the width of the physical page, in device units. For example, a printer set to print at 600 dpi on 8.5"x11" paper has a physical width value of 5100 device units. Note that the physical page is almost always greater than the printable area of the page, and never smaller.
PHYSICALHEIGHT For printing devices: the height of the physical page, in device units. For example, a printer set to print at 600 dpi on 8.5"x11" paper has a physical height value of 6600 device units. Note that the physical page is almost always greater than the printable area of the page, and never smaller.
PHYSICALOFFSETX For printing devices: the distance from the left edge of the physical page to the left edge of the printable area, in device units. For example, a printer set to print at 600 dpi on 8.5"x11" paper, that cannot print on the leftmost 0.25" of paper, has a horizontal physical offset of 150 device units.
PHYSICALOFFSETY For printing devices: the distance from the top edge of the physical page to the top edge of the printable area, in device units. For example, a printer set to print at 600 dpi on 8.5"x11" paper, that cannot print on the topmost 0.5" of paper, has a vertical physical offset of 300 device units.
VREFRESH Windows NT only: For display devices: the current vertical refresh rate of the device, in cycles per second (Hz).
A vertical refresh rate value of 0 or 1 represents the display hardware’s default refresh rate. This default rate is typically set by switches on a display card or computer motherboard, or by a configuration program that does not use Win32 display functions such as ChangeDisplaySettings.
DESKTOPHORZRES Windows NT only: Width, in pixels, of the virtual desktop. This value may be larger than HORZRES if the device supports a virtual desktop or multiple displays.
DESKTOPVERTRES Windows NT only: Height, in pixels, of the virtual desktop. This value may be larger than VERTRES if the device supports a virtual desktop or multiple displays.
BLTALIGNMENT Windows NT only: Preferred horizontal drawing alignment, expressed as a multiple of pixels. For best drawing performance, windows should be horizontally aligned to a multiple of this value. A value of zero indicates that the device is accelerated, and any alignment may be used.
RASTERCAPS Value that indicates the raster capabilities of the device, as shown in the following table:
Capability Meaning
RC_BANDING Requires banding support.
RC_BITBLT Capable of transferring bitmaps.
RC_BITMAP64 Capable of supporting bitmaps larger than 64K.
RC_DI_BITMAP Capable of supporting the SetDIBits and GetDIBits functions.
RC_DIBTODEV Capable of supporting the SetDIBitsToDevice function.
RC_FLOODFILL Capable of performing flood fills.
RC_GDI20_OUTPUT Capable of supporting features of Windows 2.0.
RC_PALETTE Specifies a palette-based device.
RC_SCALING Capable of scaling.
RC_STRETCHBLT Capable of performing the StretchBlt function.
RC_STRETCHDIB Capable of performing the StretchDIBits function.
CURVECAPS Value that indicates the curve capabilities of the device, as shown in the following table:
Value Meaning
CC_NONE Device does not support curves.
CC_CIRCLES Device can draw circles.
CC_PIE Device can draw pie wedges.
CC_CHORD Device can draw chord arcs.
CC_ELLIPSES Device can draw ellipses.
CC_WIDE Device can draw wide borders.
CC_STYLED Device can draw styled borders.
CC_WIDESTYLED Device can draw borders that are wide and styled.
CC_INTERIORS Device can draw interiors.
CC_ROUNDRECT Device can draw rounded rectangles.
LINECAPS Value that indicates the line capabilities of the device, as shown in the following table:
Value Meaning
LC_NONE Device does not support lines.
LC_POLYLINE Device can draw a polyline.
LC_MARKER Device can draw a marker.
LC_POLYMARKER Device can draw multiple markers.
LC_WIDE Device can draw wide lines.
LC_STYLED Device can draw styled lines.
LC_WIDESTYLED Device can draw lines that are wide and styled.
LC_INTERIORS Device can draw interiors.
POLYGONALCAPS Value that indicates the polygon capabilities of the device, as shown in the following table:
Value Meaning
PC_NONE Device does not support polygons.
PC_POLYGON Device can draw alternate-fill polygons.
PC_RECTANGLE Device can draw rectangles.
PC_WINDPOLYGON Device can draw winding-fill polygons.
PC_SCANLINE Device can draw a single scanline.
PC_WIDE Device can draw wide borders.
PC_STYLED Device can draw styled borders.
PC_WIDESTYLED Device can draw borders that are wide and styled.
PC_INTERIORS Device can draw interiors.
TEXTCAPS Value that indicates the text capabilities of the device, as shown in the following table:
Bit Meaning
TC_OP_CHARACTER Device is capable of character output precision.
TC_OP_STROKE Device is capable of stroke output precision.
TC_CP_STROKE Device is capable of stroke clip precision.
TC_CR_90 Device is capable of 90-degree character rotation.
TC_CR_ANY Device is capable of any character rotation.
TC_SF_X_YINDEP Device can scale independently in the x- and y-directions.
TC_SA_DOUBLE Device is capable of doubled character for scaling.
TC_SA_INTEGER Device uses integer multiples only for character scaling.
TC_SA_CONTIN Device uses any multiples for exact character scaling.
TC_EA_DOUBLE Device can draw double-weight characters.
TC_IA_ABLE Device can italicize.
TC_UA_ABLE Device can underline.
TC_SO_ABLE Device can draw strikeouts.
TC_RA_ABLE Device can draw raster fonts.
TC_VA_ABLE Device can draw vector fonts.
TC_RESERVED Reserved; must be zero.
TC_SCROLLBLT Device cannot scroll using a bit-block transfer. Note that this meaning may be the opposite of what you expect.
Obtinere informatii despre culori:
Numarul de culori care pot firedate de o placa video este:
iPlanes = GetDicecaps(hdc, PLANES);
iBitsPixel = GetDeviceCaps)hdc, BITSPIXEL); // nr. biti de culoare folositi pt. fiecare pixel
iColors = 1 << (iPlanes * iBitsPixel); // 2 la puterea iPlanes * iBitsPixel
GetDeviceCaps(hdc, NUMCOLORS) => nr. de culori rezervate de Windows. Celelalte 236 de culori pot fi satbilite de program folosind un manager de palete. Pentru a reprezenta o culoare se foloseste un intreg pe 32 biti, iar tipul de data este COLORREF. Fiecare culoare este reprezentata pe 8 biti; octetul cel mai semnificativ are valoarea zero (0).
#define RGB (r,g,b) ((COLORREF)(((BYTE) (r) | \
((WORD) (g) <<8)) | \
(((DWORD)(BYTE) (b) << 16)))
Macroinstructiunile GetRvalue, GetGValue si GetBValue extrag valorile pentru culorile primare sub forma unor octeti fara semn.
Atributele contextului de dispozitiv:
Atribut DC |
Valaore implicita |
FCT ce modifica |
Fct ce obtine attr. |
Mod de mapare |
MM_TEX |
SetMapMode |
GetMapMode |
Originea ferestrei |
(0,0) |
SetWindowOrgEx OffsetWindowOrgEx |
GetWindowOrgEx |
Originea vizorului |
(0,0) |
SetViewportOrgEx OffsetViewportOrgEx |
GetViewportOrgEx |
Extensia ferestrei |
(1,1) |
SetWindowExtEx SetMapMode ScaleWindowExtEx |
GetWindowExtEx |
Extensia vizorului |
(1,1) |
SetViewportExtEx SetMapMode ScaleViewportExtEx |
GetViewportExtEx |
Penita |
BLACK_PEN |
SelectObject |
SelectObject |
Pensula |
WHITE_BRUSH |
SelectObject |
SelectObject |
Font |
SYSTEM_FONT |
SelectObject |
SelectObject |
Bitmap |
nu exista |
SelectObject |
SelectObject |
Pozitia curenta a penitei |
(0,0) |
MoveToEx LineTo PolylineTo PolyBezierTo |
GetCurrentPositionEx |
Modul de afisare al fondului |
OPAQUE |
SetBkMode |
GetBkMode |
Culoarea fondului |
aalb |
SetBkColor |
GetBkColor |
Culoarea textului |
Negru |
SetTextColor |
GetTextColor |
Mod de desenare |
R2_COPYPEN |
SetROP2 |
GetROP2 |
Mod de intindere |
BLACKONWHITE |
SetStretchBltMode |
GetStretchBltMode |
Mod de umplere a poligoanelor |
ALTERNATE |
SetPolyFillMode |
GetPolyFillMode |
Spatiu intre caractere |
0 |
SetTextCharacterExtra |
GetTextCharacterExtra |
Originea pensulei |
(0,0) in coord. ecran |
SetBrushOrgEx |
GetBrushOrgEx |
Regiune de decupare |
nu exista |
SelectObject SelectClipRgn IntersectClipRect OffsetClipRgn ExcludeClipRect SelectClipRect |
GetClipBox |
Salvarea contextului de dispozitiv se face prin includerea flag-ului CS_OWNDC in clasa fereastra atunci cind aceasta se inregistreaza. Sint afectate numai contextele de dispozitiv obtinutre cu GetDc si BeginPaint (ocupa aprox. 800 octeti memorie). De asemenea exista functiile SaveDC si RestoreDC.
Modificarea stilului ferestrei dupa ce aceasta a fost inregistrata:
WNDCLASSEX stil;
GetWindowLong(hwnd, GWL_STYLE);
...
SetWindowLong(....);
Desenarea liniilor:
LineTo, Polyline, PolylineTo, PolyPolyline, Arc, PolyBezier, PolyBezierTo.
Pentru suprafete pline: Rectangle, Ellipse, RoundRect, Chord, Pie => coloreaza zona inchisa, cu pensula curenta de colorare a suprafetelor.
Aspectul liniilor poate fi influentat de atributele contextului de dispozitiv:
Pozitia curenta se modifica cu
POINT pt; // struct POINT { LONG x; LONG y;}
MoveToEx(hdc, xStart, yStart, &pt); // pct. anterior este stocat in pt. Coordonatele au valori intre -32768 si 32767 (deci 16 biti).
Pozitia curenta se obtine cu GetCurrentPosition(hdc, &pt);
Dreptunghiul de incadrare
Folosirea penitelor
Penite de stoc:
HPEN hPen;
hPen = GetStockObject(WHITE_PEN);
SelectObject(hdc, hPen);
sau varianta comasata: SelectObject (hdc, GetStockObject(WHITE_PEN));
Obiectele de stoc sint:
Specifies the type of stock object. This parameter can be any one of the following values:
Value Meaning
BLACK_BRUSH Black brush.
DKGRAY_BRUSH Dark gray brush.
GRAY_BRUSH Gray brush.
HOLLOW_BRUSH Hollow brush (equivalent to NULL_BRUSH).
LTGRAY_BRUSH Light gray brush.
NULL_BRUSH Null brush (equivalent to HOLLOW_BRUSH).
WHITE_BRUSH White brush.
BLACK_PEN Black pen.
NULL_PEN Null pen.
WHITE_PEN White pen.
ANSI_FIXED_FONT Windows fixed-pitch (monospace) system font.
ANSI_VAR_FONT Windows variable-pitch (proportional space) system font.
DEVICE_DEFAULT_FONT Windows NT only: Device-dependent font.
DEFAULT_GUI_FONT Windows 95 only: Default font for user interface objects such as menus and dialog boxes.
OEM_FIXED_FONT Original equipment manufacturer (OEM) dependent fixed-pitch (monospace) font.
SYSTEM_FONT System font. By default, Windows uses the system font to draw menus, dialog box controls, and text. In Windows versions 3.0 and later, the system font is a proportionally spaced font; earlier versions of Windows used a monospace system font.
SYSTEM_FIXED_FONT Fixed-pitch (monospace) system font used in Windows versions earlier than 3.0. This stock object is provided for compatibility with earlier versions of Windows.
DEFAULT_PALETTE Default palette. This palette consists of the static colors in the system palette.
Return Values If the function succeeds, the return value identifies the logical object requested.
If the function fails, the return value is NULL.
Crearea, selectarea si stergerea penitelor
Procedura generala de creare a unei penite:
CreatePen
hPen = CreatePen(iPenStyle, iWidth, rgbColor);
iPenStyle poate lua una din urmatoarele valori:
PS_SOLID linie continua; ________
PS_DASH linie intrerupta; - - -
PS_DOT linie punctata; . . . .
PS_DASHDOT linie intrerupta si puncte interioare - . - . -
PS_NULL
PS_INSIDEFRAME = stil care poate folosi culori amestecate si are grosimea liniei mai mare de un pixel. Daca iWidth este mai mare ca 1 se va desena linie continua, iWidth = 0 => grosime linie de un pixel.
Crearea unei penite indirect cu ajutorul structurii LOGPEN.
The LOGPEN structure has the following form:
typedef struct tagLOGPEN { /* lgpn */
UINT lopnStyle;
POINT lopnWidth;
COLORREF lopnColor;
} LOGPEN;
The LOGPEN structure defines the style, width, and color of a pen, a drawing object used to draw lines and borders. The CPen::CreatePenIndirect function uses the LOGPEN structure.
Members
lopnStyle Specifies the pen type. This member can be one of the following values:
PS_SOLID Creates a solid pen.
PS_DASH Creates a dashed pen. (Valid only when the pen width is 1.)
PS_DOT Creates a dotted pen. (Valid only when the pen width is 1.)
PS_DASHDOT Creates a pen with alternating dashes and dots. (Valid only when the pen width is 1.)
PS_DASHDOTDOT Creates a pen with alternating dashes and double dots. (Valid only when the pen width is 1.)
PS_NULL Creates a null pen.
PS_INSIDEFRAME Creates a pen that draws a line inside the frame of closed shapes produced by GDI output functions that specify a bounding rectangle (for example, the Ellipse, Rectangle, RoundRect, Pie, and Chord member functions). When this style is used with GDI output functions that do not specify a bounding rectangle (for example, the LineTo member function), the drawing area of the pen is not limited by a frame.
If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID pen style cannot be used to create a pen with a dithered color. The PS_INSIDEFRAME style is identical to PS_SOLID if the pen width is less than or equal to 1.
When the PS_INSIDEFRAME style is used with GDI objects produced by functions other than Ellipse, Rectangle, and RoundRect, the line may not be completely inside the specified frame.
lopnWidth Specifies the pen width, in logical units. If the lopnWidth member is 0, the pen is 1 pixel wide on raster devices regardless of the current mapping mode.
lopnColor Specifies the pen color.
Comments
The y value in the POINT structure for the lopnWidth member is not used.
LOGPEN logpen;
hPen = CreatePenIndirect(&logpen);
Obtinerea de informatii despre o penita existenta:
GetObject( hPen, sizeof(LOGPEN), (LPVOID) &logpen);
Exemplu de creare, selectare si stergere:
static HPEN hPen1, hPen2, hPen3;
In timpul tratarii mesajului WM_CREATE:
hPen1 = CreatePen (PS_SOLID, 1, 0);
hPen2 = CreatePen (PS_SOLID, 3, RGB(255, 0, 0));
hPen3 = CreatePen (PS_DOT, 0, 0);
In timpul tratarii mesajului WM_PAINT:
SelectObject(hdc, hPen2);
... desenare
SelectObject(hdc, hPen1);
... desenare
In timpul tratarii mesajului WM_DESTROY:
DeleteObject(hPen1);
DeleteObject(hPen2);
DeleteObject(hPen3);
Altfel:
SelectObject(hdc, CreatePen(PS_DASH, 0, RGB(255,0,78)));
iar stergerea:
DeleteObject( SelectObject( hdc, GetStockObject(BLACK_PEN))) ;
Schimbarea culorii fondului (culorii de umplere):
SetBkColor(hdc, rgbColor);
iar obtinerea culorii de dispozitiv se face cu:
GetBkColor(hdc);
Moduri de desenare:
CPen Class Members
Construction
CPen Constructs a CPen object.
Initialization
CreatePen Creates a logical cosmetic or geometric pen with the specified style, width, and brush attributes, and attaches it to the CPen object.
CreatePenIndirect Creates a pen with the style, width, and color given in a LOGPEN structure, and attaches it to the CPen object.
Operations
FromHandle Returns a pointer to a CPen object when given a Windows HPEN.
Attributes
operator HPEN Returns the Windows handle attached to the CPen object.
GetLogPen Gets a LOGPEN underlying structure.
GetExtLogPen Gets an EXTLOGPEN underlying structure.
///////////////////////////////////////////////////////
CPen::CPen
CPen( );
CPen( int nPenStyle, int nWidth, COLORREF crColor );
throw( CResourceException );
CPen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL );
throw( CResourceException );
Parameters
nPenStyle Specifies the pen style. This parameter in the first version of the constructor can be one of the following values:
PS_SOLID Creates a solid pen.
PS_DASH Creates a dashed pen. Valid only when the pen width is 1 or less, in device units.
PS_DOT Creates a dotted pen. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOT Creates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOTDOT Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units.
PS_NULL Creates a null pen.
PS_INSIDEFRAME Creates a pen that draws a line inside the frame of closed shapes produced by the Windows GDI output functions that specify a bounding rectangle (for example, the Ellipse, Rectangle, RoundRect, Pie, and Chord member functions). When this style is used with Windows GDI output functions that do not specify a bounding rectangle (for example, the LineTo member function), the drawing area of the pen is not limited by a frame.
The second version of the CPen constructor specifies a combination of type, style, end cap, and join attributes. The values from each category should be combined by using the bitwise OR operator (|). The pen type can be one of the following values:
PS_GEOMETRIC Creates a geometric pen.
PS_COSMETIC Creates a cosmetic pen.
The second version of the CPen constructor adds the following pen styles for nPenStyle:
PS_ALTERNATE Creates a pen that sets every other pixel. (This style is applicable only for cosmetic pens.)
PS_USERSTYLE Creates a pen that uses a styling array supplied by the user.
The end cap can be one of the following values:
PS_ENDCAP_ROUND End caps are round.
PS_ENDCAP_SQUARE End caps are square.
PS_ENDCAP_FLAT End caps are flat.
The join can be one of the following values:
PS_JOIN_BEVEL Joins are beveled.
PS_JOIN_MITER Joins are mitered when they are within the current limit set by the ::SetMiterLimit function. If the join exceeds this limit, it is beveled.
PS_JOIN_ROUND Joins are round.
nWidth Specifies the width of the pen.
For the first version of the constructor, if this value is 0, the width in device units is always 1 pixel, regardless of the mapping mode.
For the second version of the constructor, if nPenStyle is PS_GEOMETRIC, the width is given in logical units. If nPenStyle is PS_COSMETIC, the width must be set to 1.
crColor Contains an RGB color for the pen.
pLogBrush Points to a LOGBRUSH structure. If nPenStyle is PS_COSMETIC, the lbColor member of the LOGBRUSH structure specifies the color of the pen and the lbStyle member of the LOGBRUSH structure must be set to BS_SOLID. If nPenStyle is PS_GEOMETRIC, all members must be used to specify the brush attributes of the pen.
nStyleCount Specifies the length, in doubleword units, of the lpStyle array. This value must be zero if nPenStyle is not PS_USERSTYLE.
lpStyle Points to an array of doubleword values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL if nPenStyle is not PS_USERSTYLE.
Remarks
If you use the constructor with no arguments, you must initialize the resulting CPen object with the CreatePen, CreatePenIndirect, or CreateStockObject member functions. If you use the constructor that takes arguments, then no further initialization is necessary. The constructor with arguments can throw an exception if errors are encountered, while the constructor with no arguments will always succeed.
CPen::CreatePen
BOOL CreatePen( int nPenStyle, int nWidth, COLORREF crColor );
BOOL CreatePen( int nPenStyle, int nWidth, const LOGBRUSH* pLogBrush, int nStyleCount = 0, const DWORD* lpStyle = NULL );
Return Value Nonzero, or the handle of a logical pen, if successful; otherwise 0.
Parameters
nPenStyle Specifies the style for the pen. For a list of possible values, see the nPenStyle parameter in the CPen constructor.
nWidth Specifies the width of the pen.
For the first version of CreatePen, if this value is 0, the width in device units is always 1 pixel, regardless of the mapping mode.
For the second version of CreatePen, if nPenStyle is PS_GEOMETRIC, the width is given in logical units. If nPenStyle is PS_COSMETIC, the width must be set to 1.
crColor Contains an RGB color for the pen.
pLogBrush Points to a LOGBRUSH structure. If nPenStyle is PS_COSMETIC, the lbColor member of the LOGBRUSH structure specifies the color of the pen and the lbStyle member of the LOGBRUSH structure must be set to BS_SOLID. If nPenStyle is PS_GEOMETRIC, all members must be used to specify the brush attributes of the pen.
nStyleCount Specifies the length, in doubleword units, of the lpStyle array. This value must be zero if nPenStyle is not PS_USERSTYLE.
lpStyle Points to an array of doubleword values. The first value specifies the length of the first dash in a user-defined style, the second value specifies the length of the first space, and so on. This pointer must be NULL if nPenStyle is not PS_USERSTYLE.
Remarks
The first version of CreatePen initializes a pen with the specified style, width, and color. The pen can be subsequently selected as the current pen for any device context.
Pens that have a width greater than 1 pixel should always have either the PS_NULL, PS_SOLID, or PS_INSIDEFRAME style.
If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID pen style cannot be used to create a pen with a dithered color. The style PS_INSIDEFRAME is identical to PS_SOLID if the pen width is less than or equal to 1.
The second version of CreatePen initializes a logical cosmetic or geometric pen that has the specified style, width, and brush attributes. The width of a cosmetic pen is always 1; the width of a geometric pen is always specified in world units. After an application creates a logical pen, it can select that pen into a device context by calling the CDC::SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.
When an application no longer requires a given pen, it should call the CGdiObject::DeleteObject member function to delete the pen from the device context.
CPen::CreatePenIndirect
BOOL CreatePenIndirect( LPLOGPEN lpLogPen );
Return Value Nonzero if the function is successful; otherwise 0.
Parameters
lpLogPen Points to the Windows LOGPEN structure that contains information about the pen.
Remarks
Initializes a pen that has the style, width, and color given in the structure pointed to by lpLogPen.
Pens that have a width greater than 1 pixel should always have either the PS_NULL, PS_SOLID, or PS_INSIDEFRAME style.
If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_INSIDEFRAME style is identical to PS_SOLID if the pen width is less than or equal to 1.
CPen::FromHandle
static CPen* PASCAL FromHandle( HPEN hPen );
Return Value A pointer to a CPen object if successful; otherwise NULL.
Parameters
hPen HPEN handle to Windows GDI pen.
Remarks
Returns a pointer to a CPen object given a handle to a Windows GDI pen object. If a CPen object is not attached to the handle, a temporary CPen object is created and attached. This temporary CPen object is valid only until the next time the application has idle time in its event loop, at which time all temporary graphic objects are deleted. In other words, the temporary object is only valid during the processing of one window message.
CPen::GetLogPen
int GetLogPen( LOGPEN* pLogPen );
Return Value Nonzero if successful; otherwise 0.
Parameters
pLogPen Points to a LOGPEN structure to contain information about the pen.
Remarks
Call this member function to get a LOGPEN underlying structure. The LOGPEN structure defines the style, color, and pattern of a pen.
For example, call GetLogPen to match the particular style of pen.
See the following topics in the Win 32 SDK Programmer's Reference for information about pen attributes:
Example
The following code example demonstrates calling GetLogPen to retrieve a pen character, and then create a new, solid pen with the same color.
LOGPEN logpen;
penExisting.GetLogPen( &logpen );
CPen penOther( PS_SOLID, 0, logpen.lopnColor);
CBrush Class Members
Construction
CBrush Constructs a CBrush object.
Initialization
CreateSolidBrush Initializes a brush with the specified solid color.
CreateHatchBrush Initializes a brush with the specified hatched pattern and color.
CreateBrushIndirect Initializes a brush with the style, color, and pattern specified in a LOGBRUSH structure.
CreatePatternBrush Initializes a brush with a pattern specified by a bitmap.
CreateDIBPatternBrush Initializes a brush with a pattern specified by a device-independent bitmap (DIB).
CreateSysColorBrush Creates a brush that is the default system color.
Operations
FromHandle Returns a pointer to a CBrush object when given a handle to a Windows HBRUSH object.
Attributes
GetLogBrush Gets a LOGBRUSH structure.
operator HBRUSH Returns the Windows handle attached to the CBrush object.
CBrush::CBrush
CBrush( );
CBrush( COLORREF crColor );
throw( CResourceException );
CBrush( int nIndex, COLORREF crColor );
throw( CResourceException );
CBrush( CBitmap* pBitmap );
throw( CResourceException );
Parameters
crColor Specifies the foreground color of the brush as an RGB color. If the brush is hatched, this parameter specifies the color of the hatching.
nIndex Specifies the hatch style of the brush. It can be any one of the following values:
HS_BDIAGONAL Downward hatch (left to right) at 45 degrees
HS_CROSS Horizontal and vertical crosshatch
HS_DIAGCROSS Crosshatch at 45 degrees
HS_FDIAGONAL Upward hatch (left to right) at 45 degrees
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
pBitmap Points to a CBitmap object that specifies a bitmap with which the brush paints.
Remarks
Has four overloaded constructors. The constructor with no arguments constructs an uninitialized CBrush object that must be initialized before it can be used.
If you use the constructor with no arguments, you must initialize the resulting CBrush object with CreateSolidBrush, CreateHatchBrush, CreateBrushIndirect, CreatePatternBrush, or CreateDIBPatternBrush. If you use one of the constructors that takes arguments, then no further initialization is necessary. The constructors with arguments can throw an exception if errors are encountered, while the constructor with no arguments will always succeed.
The constructor with a single COLORREF parameter constructs a solid brush with the specified color. The color specifies an RGB value and can be constructed with the RGB macro in WINDOWS.H.
The constructor with two parameters constructs a hatch brush. The nIndex parameter specifies the index of a hatched pattern. The crColor parameter specifies the color.
The constructor with a CBitmap parameter constructs a patterned brush. The parameter identifies a bitmap. The bitmap is assumed to have been created by using CBitmap::CreateBitmap, CBitmap::CreateBitmapIndirect, CBitmap::LoadBitmap, or CBitmap::CreateCompatibleBitmap. The minimum size for a bitmap to be used in a fill pattern is 8 pixels by 8 pixels.
CBrush::CreateSolidBrush
BOOL CreateSolidBrush( COLORREF crColor );
Return Value Nonzero if successful; otherwise 0.
Parameters
crColor A COLORREF structure that specifies the color of the brush. The color specifies an RGB value and can be constructed with the RGB macro in WINDOWS.H.
Remarks
Initializes a brush with a specified solid color. The brush can subsequently be selected as the current brush for any device context.
When an application has finished using the brush created by CreateSolidBrush, it should select the brush out of the device context.
CBrush::CreateHatchBrush
BOOL CreateHatchBrush( int nIndex, COLORREF crColor );
Return Value Nonzero if successful; otherwise 0.
Parameters
nIndex Specifies the hatch style of the brush. It can be any one of the following values:
HS_BDIAGONAL Downward hatch (left to right) at 45 degrees
HS_CROSS Horizontal and vertical crosshatch
HS_DIAGCROSS Crosshatch at 45 degrees
HS_FDIAGONAL Upward hatch (left to right) at 45 degrees
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
crColor Specifies the foreground color of the brush as an RGB color (the color of the hatches). See COLORREF in the Win32 SDK documentation for more information.
Remarks
Initializes a brush with the specified hatched pattern and color. The brush can subsequently be selected as the current brush for any device context.
LOGBRUSH
The LOGBRUSH structure defines the style, color, and pattern of a physical brush. It is used by the CreateBrushIndirect and ExtCreatePen functions.
typedef struct tagLOGBRUSH { // lb
UINT lbStyle;
COLORREF lbColor;
LONG lbHatch;
} LOGBRUSH;
Members
lbStyle Specifies the brush style. The lbStyle member must be one of the following styles:
Value Meaning
BS_DIBPATTERN A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB.
Windows 95: Creating brushes from bitmaps or DIBs larger than 8x8 pixels is not supported. If a larger bitmap is given, only a portion of the bitmap is used.
BS_DIBPATTERN8X8 Same as BS_DIBPATTERN.
BS_DIBPATTERNPT A pattern brush defined by a device-independent bitmap (DIB) specification. If lbStyle is BS_DIBPATTERNPT, the lbHatch member contains a pointer to a packed DIB.
BS_HATCHED Hatched brush.
BS_HOLLOW Hollow brush.
BS_NULL Same as BS_HOLLOW.
BS_PATTERN Pattern brush defined by a memory bitmap.
BS_PATTERN8X8 Same as BS_PATTERN.
BS_SOLID Solid brush.
lbColor Specifies the color in which the brush is to be drawn. If lbStyle is the BS_HOLLOW or BS_PATTERN style, lbColor is ignored.
If lbStyle is BS_DIBPATTERN or BS_DIBPATTERNBT, the low-order word of lbColor specifies whether the bmiColors members of the BITMAPINFO structure contain explicit red, green, blue (RGB) values or indices into the currently realized logical palette. The lbColor member must be one of the following values:
Value Meaning
DIB_PAL_COLORS The color table consists of an array of 16-bit indices into the currently realized logical palette.
DIB_RGB_COLORS The color table contains literal RGB values.
lbHatch Specifies a hatch style. The meaning depends on the brush style defined by lbStyle.
If lbStyle is BS_DIBPATTERN, the lbHatch member contains a handle to a packed DIB. If lbStyle is BS_DIBPATTERNPT, the lbHatch member contains a pointer to a packed DIB.
If lbStyle is BS_HATCHED, the lbHatch member specifies the orientation of the lines used to create the hatch. It can be one of the following values:
Value Meaning
HS_BDIAGONAL A 45-degree upward, left-to-right hatch
HS_CROSS Horizontal and vertical cross-hatch
HS_DIAGCROSS 45-degree crosshatch
HS_FDIAGONAL A 45-degree downward, left-to-right hatch
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
If lbStyle is BS_PATTERN, lbHatch is a handle to the bitmap that defines the pattern. The bitmap cannot be a DIB section bitmap, which is created by the CreateDIBSection function.
If lbStyle is BS_SOLID or BS_HOLLOW, lbHatch is ignored.
Remarks
Although lbColor controls the foreground color of a hatch brush, the SetBkMode and SetBkColor functions control the background color.
See Also
BITMAPINFO, CreateBrushIndirect, CreateDIBSection, ExtCreatePen, SetBkColor, SetBkMode
CBitmap Class Members
Construction
CBitmap Constructs a CBitmap object.
Initialization
LoadBitmap Initializes the object by loading a named bitmap resource from the application's executable file and attaching the bitmap to the object.
LoadOEMBitmap Initializes the object by loading a predefined Windows bitmap and attaching the bitmap to the object.
LoadMappedBitmap Loads a bitmap and maps colors to current system colors.
CreateBitmap Initializes the object with a device-dependent memory bitmap that has a specified width, height, and bit pattern.
CreateBitmapIndirect Initializes the object with a bitmap with the width, height, and bit pattern (if one is specified) given in a BITMAP structure.
CreateCompatibleBitmap Initializes the object with a bitmap so that it is compatible with a specified device.
CreateDiscardableBitmap Initializes the object with a discardable bitmap that is compatible with a specified device.
Attributes
GetBitmap Fills a BITMAP structure with information about the bitmap.
operator HBITMAP Returns the Windows handle attached to the CBitmap object.
Operations
FromHandle Returns a pointer to a CBitmap object when given a handle to a Windows HBITMAP bitmap.
SetBitmapBits Sets the bits of a bitmap to the specified bit values.
GetBitmapBits Copies the bits of the specified bitmap into the specified buffer.
SetBitmapDimension Assigns a width and height to a bitmap in 0.1-millimeter units.
GetBitmapDimension Returns the width and height of the bitmap. The height and width are assumed to have been set previously by the SetBitmapDimension member function.
CRgn
The CRgn class encapsulates a Windows graphics device interface (GDI) region. A region is an elliptical or polygonal area within a window. To use regions, you use the member functions of class CRgn with the clipping functions defined as members of class CDC.
The member functions of CRgn create, alter, and retrieve information about the region object for which they are called.
For more information on using CRgn, see Graphic Objects in Visual C++ Programmer's Guide.
#include <afxwin.h>
CRgn Class Members
Construction
CRgn Constructs a CRgn object.
Initialization
CreateRectRgn Initializes a CRgn object with a rectangular region.
CreateRectRgnIndirect Initializes a CRgn object with a rectangular region defined by a RECT structure.
CreateEllipticRgn Initializes a CRgn object with an elliptical region.
CreateEllipticRgnIndirect Initializes a CRgn object with an elliptical region defined by a RECT structure.
CreatePolygonRgn Initializes a CRgn object with a polygonal region. The system closes the polygon automatically, if necessary, by drawing a line from the last vertex to the first.
CreatePolyPolygonRgn Initializes a CRgn object with a region consisting of a series of closed polygons. The polygons may be disjoint, or they may overlap.
CreateRoundRectRgn Initializes a CRgn object with a rectangular region with rounded corners.
CombineRgn Sets a CRgn object so that it is equivalent to the union of two specified CRgn objects.
CopyRgn Sets a CRgn object so that it is a copy of a specified CRgn object.
CreateFromPath Creates a region from the path that is selected into the given device context.
CreateFromData Creates a region from the given region and transformation data.
Operations
EqualRgn Checks two CRgn objects to determine whether they are equivalent.
FromHandle Returns a pointer to a CRgn object when given a handle to a Windows region.
GetRegionData Fills the specified buffer with data describing the given region.
GetRgnBox Retrieves the coordinates of the bounding rectangle of a CRgn object.
OffsetRgn Moves a CRgn object by the specified offsets.
PtInRegion Determines whether a specified point is in the region.
RectInRegion Determines whether any part of a specified rectangle is within the boundaries of the region.
SetRectRgn Sets the CRgn object to the specified rectangular region.
Operators
operator HRGN Returns the Windows handle contained in the CRgn object.
CPalette
The CPalette class encapsulates a Windows color palette. A palette provides an interface between an application and a color output device (such as a display device). The interface allows the application to take full advantage of the color capabilities of the output device without severely interfering with the colors displayed by other applications. Windows uses the application's logical palette (a list of needed colors) and the system palette (which defines available colors) to determine the colors used.
A CPalette object provides member functions for manipulating the palette referred to by the object. Construct a CPalette object and use its member functions to create the actual palette, a graphics device interface (GDI) object, and to manipulate its entries and other properties.
For more information on using CPalette, see Graphic Objects in Visual C++ Programmer's Guide.
#include <afxwin.h>
CPalette Class Members
Construction
CPalette Constructs a CPalette object with no attached Windows palette. You must initialize the CPalette object with one of the initialization member functions before it can be used.
Initialization
CreatePalette Creates a Windows color palette and attaches it to the CPalette object.
CreateHalftonePalette Creates a halftone palette for the device context and attaches it to the CPalette object.
Operations
FromHandle Returns a pointer to a CPalette object when given a handle to a Windows palette object.
AnimatePalette Replaces entries in the logical palette identified by the CPalette object. The application does not have to update its client area, because Windows maps the new entries into the system palette immediately.
GetNearestPaletteIndex Returns the index of the entry in the logical palette that most closely matches a color value.
ResizePalette Changes the size of the logical palette specified by the CPalette object to the specified number of entries.
Attributes
GetEntryCount Retrieves the number of palette entries in a logical palette.
GetPaletteEntries Retrieves a range of palette entries in a logical palette.
SetPaletteEntries Sets RGB color values and flags in a range of entries in a logical palette.
operator HPALETTE Returns the HPALETTE attached to the CPalette.
CFont
The CFont class encapsulates a Windows graphics device interface (GDI) font and provides member functions for manipulating the font. To use a CFont object, construct a CFont object and attach a Windows font to it with CreateFont, CreateFontIndirect, CreatePointFont, or CreatePointFontIndirect, and then use the object's member functions to manipulate the font.
The CreatePointFont and CreatePointFontIndirect functions are often easier to use than CreateFont or CreateFontIndirect since they do the conversion for the height of the font from a point size to logical units automatically.
For more information on CFont, see Graphic Objects in Visual C++ Programmer's Guide.
#include <afxwin.h>