GetStockObject Function

Declare Function GetStockObject Lib "gdi32.dll" (ByVal nIndex As Long) As Long

Platforms: Win 32s, Win 95/98, Win NT

GetStockObject accesses one of Windows's stock pens, brushes, fonts, or palettes. This function provides fast access to these commonly used objects, instead of having to use more complicated functions. The function returns a handle to the pen, brush, font, or palette which the function accesses. Although the program isn't required to delete the handle using DeleteObject, doing so doesn't have any adverse effects.

nIndex
Exactly one of the following flags specifying which of the stock objects to create a handle to:
ANSI_FIXED_FONT = 11
The system's normal monospaced font.
ANSI_VAR_FONT = 12
The system's normal proportional-width font.
BLACK_BRUSH = 4
A solid black brush.
BLACK_PEN = 7
A solid black pen.
DEFAULT_GUI_FONT = 17
Win 95/98 only: The default font for user objects under Windows.
DEFAULT_PALETTE = 15
The default system palette.
DEVICE_DEFAULT_FONT = 14
Win NT only: a device-dependent font.
DKGRAY_BRUSH = 3
A solid dark gray brush.
GRAY_BRUSH = 2
A solid gray brush.
HOLLOW_BRUSH = 5
Same as NULL_BRUSH.
LTGRAY_BRUSH = 1
A solid light gray brush.
NULL_BRUSH = 5
A null brush; i.e., a brush that does not draw anything on the device.
NULL_PEN = 8
A null pen; i.e., a pen that does not draw anything on the device.
OEM_FIXED_FONT = 10
The Original Equipment Manufacturer's default monospaced font.
SYSTEM_FIXED_FONT = 16
The system monospaced font under pre-3.x versions of Windows.
SYSTEM_FONT = 13
The system font (used for most system objects under Windows).
WHITE_BRUSH = 0
A solid white brush.
WHITE_PEN = 6
A solid white pen.

Example:

' Draw a rectangle with a black border and light gray filled interior
' on window Form1.  Use stock pens and brushes to do this.
Dim hbrush As Long, holdbrush As Long  ' handles to stock brush & default brush
Dim hpen As Long, holdpen As Long  ' handles to stock pen & default pen
Dim retval As Long  ' return value

' Load the stock pen and brush needed for this operation
hpen = GetStockObject(BLACK_PEN)  ' load the black solid pen
hbrush = GetStockObject(LTGRAY_BRUSH)  ' load the light gray solid brush
' Select the two objects in Form1 and save the defaults
holdpen = SelectObject(Form1.hDC, hpen)  ' select the pen
holdbrush = SelectObject(Form1.hDC, hbrush)  ' select the brush
' Draw the rectangle using the pen and brush.  The rectangle has corners (20,25)-(200,175)
retval = Rectangle(Form1.hDC, 20, 25, 200, 175)
' Restore Form1's previously selected pen and brush
retval = SelectObject(Form1.hDC, holdpen)  ' reselect old pen
retval = SelectObject(Form1.hDC, holdbrush)  ' reselect old brush
' Note that it is not necessary to delete hpen and hbrush, but we could if we wanted.

Category: Devices

Go back to the alphabetical Function listing.
Go back to the Reference section index.


This page is copyright © 2000 Paul Kuliniewicz. Copyright Information.
Go back to the Windows API Guide home page.
E-mail: vbapi@vbapi.com Send Encrypted E-Mail
This page is at http://www.vbapi.com/ref/g/getstockobject.html