vbAccelerator - Contents of code file: cMemDC.cls

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "cMemDC"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

Private Type RECT
   Left As Long
   TOp As Long
   Right As Long
   Bottom As Long
End Type

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As
 Long
Private Declare Function SelectObjectAPI Lib "gdi32" Alias "SelectObject"
 (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function CreateDCAsNull Lib "gdi32" Alias "CreateDCA" ( _
    ByVal lpDriverName As String, lpDeviceName As Any, _
   lpOutput As Any, lpInitData As Any) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function OleTranslateColor Lib "OLEPRO32.DLL" (ByVal OLE_COLOR
 As Long, ByVal HPALETTE As Long, pccolorref As Long) As Long
Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long)
 As Long
Private Declare Function FillRect Lib "user32" (ByVal hDC As Long, lpRect As
 RECT, ByVal hBrush As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As
 Long

Private m_hBmpOld As Long
Private m_hDC As Long
Private m_cBmp As cBmp

Public Sub Create()
Dim lhDC As Long
   Dispose
   lhDC = CreateDCAsNull("DISPLAY", ByVal 0&, ByVal 0&, ByVal 0&)
   m_hDC = CreateCompatibleDC(lhDC)
   DeleteDC lhDC
End Sub

Public Sub Dispose()
   UnselectObject
   If Not (m_hDC = 0) Then
      DeleteDC m_hDC
      m_hDC = 0
   End If
End Sub

Public Sub SelectObject(cB As cBmp)
   UnselectObject
   m_hBmpOld = SelectObjectAPI(m_hDC, cB.hBmp)
   Set m_cBmp = cB
End Sub

Public Sub UnselectObject()
   If Not (m_hBmpOld = 0) Then
      SelectObjectAPI m_hDC, m_hBmpOld
      m_hBmpOld = 0
      Set m_cBmp = Nothing
   End If
End Sub

Public Sub Fill(ByVal oColor As OLE_COLOR)
   If Not m_cBmp Is Nothing Then
      Dim tR As RECT
      tR.Right = m_cBmp.Width
      tR.Bottom = m_cBmp.Height
      Dim hBr As Long
      hBr = CreateSolidBrush(TranslateColor(oColor))
      FillRect m_hDC, tR, hBr
      DeleteObject hBr
   Else
      Err.Raise 7, App.EXEName & ".cMemDC", "Cannot fill DC as no bitmap is
       selected."
   End If
End Sub

Public Property Get hDC() As Long
   hDC = m_hDC
End Property

Private Function TranslateColor(ByVal oClr As OLE_COLOR, _
                        Optional hPal As Long = 0) As Long
    ' Convert Automation color to Windows color
    If OleTranslateColor(oClr, hPal, TranslateColor) Then
        TranslateColor = -1 'CLR_INVALID
    End If
End Function
Private Sub Class_Terminate()
   Dispose
End Sub