VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "dwBrush"
Attribute VB_Creatable = True
Attribute VB_Exposed = True
Option Explicit

' Class dwBrush
' Brush drawing object control and configuration class
' Copyright (c) 1996 by Desaware Inc.
' Part of the Desaware API Classes Library

' Brush objects always reference a valid handle, stock or created

#If Win32 Then
Private ihBrush As Long
#Else
Private ihBrush As Integer
#End If

Private bCreated As Boolean  ' Brush was created

#If Win32 Then
Private Declare Function apiGetStockObject& Lib "gdi32" Alias "GetStockObject" (ByVal nIndex As Long)
Private Declare Function apiDeleteObject& Lib "gdi32" Alias "DeleteObject" (ByVal hObject As Long)
Private Declare Function apiCreateBrushIndirect& Lib "gdi32" Alias "CreateBrushIndirect" (lpLogBrush As LOGBRUSH)
Private Declare Function apiCreateHatchBrush& Lib "gdi32" Alias "CreateHatchBrush" (ByVal nIndex As Long, ByVal crColor As Long)
Private Declare Function apiCreateSolidBrush& Lib "gdi32" Alias "CreateSolidBrush" (ByVal crColor As Long)
Private Declare Function apiGetObjectType Lib "gdi32" (ByVal hgdiobj As Long) As Long

#Else
Private Declare Function apiGetStockObject% Lib "gdi" Alias "GetStockObject" (ByVal nIndex As Integer)
Private Declare Function apiDeleteObject% Lib "gdi" Alias "DeleteObject" (ByVal hObject As Integer)
Private Declare Function apiCreateBrushIndirect% Lib "gdi" Alias "CreateBrushIndirect" (lpLogBrush As LOGBRUSH)
Private Declare Function apiCreateHatchBrush% Lib "gdi" Alias "CreateHatchBrush" (ByVal nIndex As Integer, ByVal crColor As Long)
Private Declare Function apiCreateSolidBrush% Lib "gdi" Alias "CreateSolidBrush" (ByVal crColor As Long)
#End If 'WIN32

Public Property Get hBrush() As Long
    hBrush = ihBrush
End Property

Public Property Let hBrush(vNewValue As Long)
    #If Win32 Then
        If apiGetObjectType(vNewValue) <> OBJ_BRUSH Then
            RaiseBrushError DWERR_INVALIDPARAMETER
        End If
    #End If
    
    Cleanup
    ihBrush = vNewValue
End Property

Public Property Get Created() As Boolean
    Created = bCreated
End Property

Public Property Let Created(vNewValue As Boolean)
    If vNewValue Then bCreated = True
End Property

Private Sub Class_Initialize()
    ihBrush = apiGetStockObject(WHITE_BRUSH)
End Sub

' Delete any allocated objects
Public Sub Cleanup()
    If bCreated Then
        Call apiDeleteObject(ihBrush)
        bCreated = False
        ihBrush = apiGetStockObject(WHITE_BRUSH)
    End If
End Sub

Private Sub Class_Terminate()
    Cleanup
End Sub

Public Property Get WHITE_BRUSH() As Integer
    WHITE_BRUSH = 0
End Property

Public Property Get LTGRAY_BRUSH() As Integer
    LTGRAY_BRUSH = 1
End Property

Public Property Get GRAY_BRUSH() As Integer
    GRAY_BRUSH = 2
End Property

Public Property Get DKGRAY_BRUSH() As Integer
    DKGRAY_BRUSH = 3
End Property

Public Property Get BLACK_BRUSH() As Integer
    BLACK_BRUSH = 4
End Property

Public Property Get NULL_BRUSH() As Integer
    NULL_BRUSH = 5
End Property

Public Sub GetStockObject(idx%)
Attribute GetStockObject.VB_HelpID = 2569
Attribute GetStockObject.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
    Cleanup
    If idx < WHITE_BRUSH Or idx > NULL_BRUSH Then
        RaiseBrushError DWERR_INVALIDPARAMETER
    Else
        ihBrush = apiGetStockObject(idx)
    End If
End Sub

Public Sub CreateSolidBrush(ByVal crColor As Long)
Attribute CreateSolidBrush.VB_HelpID = 2518
Attribute CreateSolidBrush.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
    Dim ret&
    ret& = apiCreateSolidBrush(crColor)
    If ret& = 0 Then RaiseBrushError
    Cleanup
    ihBrush = ret&
    bCreated = True
End Sub

Public Sub CreateHatchBrush(ByVal nIndex, crColor As Long)
Attribute CreateHatchBrush.VB_HelpID = 2505
Attribute CreateHatchBrush.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
    Dim ret&
    
    ret& = apiCreateHatchBrush(nIndex, crColor)
    If ret& = 0 Then RaiseBrushError
    Cleanup
    ihBrush = ret&
    bCreated = True
End Sub

Public Sub CreateBrushIndirect(lpLogBrush As dwLogBrush)
Attribute CreateBrushIndirect.VB_HelpID = 2492
Attribute CreateBrushIndirect.VB_Description = "Help available in Visual Basic Programmer's Guide to Win32 API CD ROM"
    Dim tempBrush As LOGBRUSH
    Dim ret&
    
    lpLogBrush.CopyToLOGBRUSH agGetAddressForObject(tempBrush)
    ret& = apiCreateBrushIndirect(tempBrush)
    If ret& = 0 Then RaiseBrushError
    Cleanup
    ihBrush = ret&
    bCreated = True
End Sub

Private Sub RaiseBrushError(Optional errval)
    Dim useerr%
    If IsMissing(errval) Then
        RaiseBrushError DWERR_APIRESULT
    Else
        RaiseError errval, "dwBrush"
    End If
End Sub


