Jalaj P. Jha Technical & Miscellaneous Ramblings

1Mar/070




Getting Stock Brushes

If you carefully read the post title it says "Getting Stock Brushes" and not "Creating...", because these are the predefined brushes and you don't create more brushes but instead use one by getting the handle to the stock object.

The Graphics Device Interface maintains 7 pre-defined stock brushes namely Black, Light Gray, Dark Gray, Null, Gray, White and Hollow. The handles to these stock brushes and other stock objects as Pens, Fonts and Palettes can be obtained using the GetStockObject function. The function takes a single parameter, an index, which can be one of the constants as defined below. The return value is the handle to the Stock Object, which can be the Brush, Pen, Font or the Palette depending on the stock object queried for.

Private Declare Function GetStockObject Lib "gdi32" Alias "GetStockObject" ( _
    ByVal nIndex As Long) As Long

' Brushes
Const WHITE_BRUSH = 0
Const LTGRAY_BRUSH = 1
Const GRAY_BRUSH = 2
Const DKGRAY_BRUSH = 3
Const BLACK_BRUSH = 4
Const NULL_BRUSH = 5
Const HOLLOW_BRUSH = NULL_BRUSH

' Pens
Const WHITE_PEN = 6
Const BLACK_PEN = 7

' Fonts
Const ANSI_FIXED_FONT = 11
Const ANSI_VAR_FONT = 12
Const DEVICE_DEFAULT_FONT = 14
Const DEFAULT_GUI_FONT = 17
Const OEM_FIXED_FONT = 10
Const SYSTEM_FONT = 13
Const SYSTEM_FIXED_FONT = 16

' Palette
Const DEFAULT_PALETTE = 15

As already mentioned in previous post WinAPI: Using Brushes, 21 pre-defined stock brushes are maintained by the Window Management interface (User32.dll). These stock brushes can be utilized by getting their handle using the GetSysColorBrush function. The function takes a single parameter that can be one of the constants as defined below. The return value is the handle to the brush.

Private Declare Function GetSysColorBrush Lib "user32" Alias "GetSysColorBrush" ( _
    ByVal nIndex As Long) As Long

Const COLOR_SCROLLBAR = 0
Const COLOR_BACKGROUND = 1
Const COLOR_ACTIVECAPTION = 2
Const COLOR_INACTIVECAPTION = 3
Const COLOR_MENU = 4
Const COLOR_WINDOW = 5
Const COLOR_WINDOWFRAME = 6
Const COLOR_MENUTEXT = 7
Const COLOR_WINDOWTEXT = 8
Const COLOR_CAPTIONTEXT = 9
Const COLOR_ACTIVEBORDER = 10
Const COLOR_INACTIVEBORDER = 11
Const COLOR_APPWORKSPACE = 12
Const COLOR_HIGHLIGHT = 13
Const COLOR_HIGHLIGHTTEXT = 14
Const COLOR_BTNFACE = 15
Const COLOR_BTNSHADOW = 16
Const COLOR_GRAYTEXT = 17
Const COLOR_BTNTEXT = 18
Const COLOR_INACTIVECAPTIONTEXT = 19
Const COLOR_BTNHIGHLIGHT = 20

The stock brushes are maintained by the system and are used by all applications hence must not be destroyed.