Attribute VB_Name = "modPrint"
Option Explicit

Public Const defFontName As String = "Arial"
Public Const defFontSize As Single = 8.25

Public CInteraction As New clsInteraction

Public PrinterEx As clsPrint
Public CQueue As clsQueue

Public colPage() As Collection

Public sngLineWidthCoefficient As Single
Public sngLabelExCoefficient As Single

Private Const DT_WORDBREAK& = &H10

Private Const LF_FACESIZE& = 32
Private Const DEFAULT_QUALITY& = 0
Private Const DEFAULT_PITCH& = 0
Private Const OUT_DEFAULT_PRECIS& = 0
Private Const FF_DONTCARE& = 0
Private Const DEFAULT_CHARSET& = 1

Private Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As Byte
    lfUnderline As Byte
    lfStrikeOut As Byte
    lfCharSet As Byte
    lfOutPrecision As Byte
    lfClipPrecision As Byte
    lfQuality As Byte
    lfPitchAndFamily As Byte
    lfFaceName(LF_FACESIZE - 1) As Byte
End Type

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

Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hDC As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long


Public Sub NewReport()
    Set PrinterEx = Nothing
    Set PrinterEx = New clsPrint
End Sub

Public Sub SendPage(OutputObj As Object, _
                                        ByVal Page As Integer, _
                                        Optional ByVal Zoom As Single = 1, _
                                        Optional ByVal Indent_X As Long, _
                                        Optional ByVal Indent_Y As Long)
                                        
Dim CPointA As clsPointAttributes
Dim CLineA As clsLineAttributes
Dim CLabelA As clsLabelAttributes
Dim CRectangleA As clsRectangleAttributes
Dim CLabelExA As clsLabelExAttributes
Dim CImageA As clsImageAttributes
Dim CTableA As clsTableAttributes

Dim lngDC As Long
Dim strItem As String
Dim x1_ As Single, y1_ As Single, x2_ As Single, y2_ As Single
Dim i As Long, j As Long, k As Long, Align As Long
Dim sngLeftMargin As Single, sngRightMargin As Single, sngPW As Single, sngPH As Single
Dim IndentX As Single, IndentY As Single
Dim sngLineWidthCoefficient_ As Single, sngLabelExCoefficient_ As Single

Dim TextArray() As String, sngTableTop As Single, FirstRowIsTitle As Boolean
Dim sngCellSpacing As Single, intBorderWidth As Integer, sngColumnSpacing As Single
Dim sngTitleHeight As Single, sngBodyHeight As Single
Dim lngTotalColumns As Long, lngColumnsCount As Long
Dim lngBorderColor As Long, sngColsWidth() As Single, sngTableWidth As Single
Dim Col_ As Long
Dim strTableLex() As String, lngAryIndex As Long
Dim lngRowsOnPage As Long, lngTotalRows As Long
Dim lngStartRow As Long, lngEndRow As Long
Dim ly As Long, uy As Long
Dim x1__  As Single, y1__ As Single

Dim lf As LOGFONT
Dim FontToUse As Long, oldFont As Long
Dim TempByteArray() As Byte

Dim PointArray() As Single

    OutputObj.Print ""

    IndentX = Indent_X * Zoom
    IndentY = Indent_Y * Zoom

    sngLabelExCoefficient_ = sngLabelExCoefficient * Zoom
    sngLineWidthCoefficient_ = sngLineWidthCoefficient * Zoom

    lngDC = OutputObj.hDC
    With PrinterEx
        sngPW = .PrintableWidth * Zoom
        sngPH = .PrintableHeight * Zoom
        sngLeftMargin = .LeftMargin * Zoom + IndentX
        sngRightMargin = .RightMargin * Zoom
    End With

    OutputObj.DrawStyle = vbSolid
    For i = 1 To PrinterEx.Repeats.Count
        Select Case TypeName(PrinterEx.Repeats(i))
            Case "clsLabelAttributes"
                Set CLabelA = PrinterEx.Repeats(i)
                With CLabelA
                    OutputObj.FontBold = .FontBold
                    OutputObj.FontItalic = .FontItalic
                    OutputObj.FontName = .FontName
                    OutputObj.FontStrikethru = .FontStrikethru
                    OutputObj.FontUnderline = .FontUnderline
                    OutputObj.FontSize = .FontSize * Zoom
                    OutputObj.ForeColor = .ForeColor
                    Align = .Align
                    sngCellSpacing = .CellSpacing * Zoom
                    x1_ = .Left * Zoom + sngLeftMargin
                    y1_ = .Top * Zoom + IndentY
                    x2_ = .Right * Zoom + sngLeftMargin
                    y2_ = .Bottom * Zoom + IndentY
                    If .WordWrap Then Align = Align Or DT_WORDBREAK
                    intBorderWidth = Abs(Int(-(.BorderWidth / 10) * sngLineWidthCoefficient_))
                    If intBorderWidth > 0 Then
                        OutputObj.DrawWidth = intBorderWidth
                        OutputObj.FillStyle = .FillStyle
                        OutputObj.FillColor = .FillColor
                        OutputObj.Line (x1_, y1_)-(x2_, y2_), .BorderColor, B
                    End If

                    Draw_Text lngDC, Replace(.Caption, "&[Page]", Page), _
                                        CLng(OutputObj.ScaleX(x1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleX(x2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        Align
                End With
            Case "clsLabelExAttributes"
                Set CLabelExA = PrinterEx.Repeats(i)
                With CLabelExA
                    OutputObj.ForeColor = .ForeColor
                    Align = .Align
                    sngCellSpacing = .CellSpacing * Zoom
                    x1_ = .Left * Zoom + sngLeftMargin
                    y1_ = .Top * Zoom + IndentY
                    x2_ = .Right * Zoom + sngLeftMargin
                    y2_ = .Bottom * Zoom + IndentY
                    If .WordWrap Then Align = Align Or DT_WORDBREAK
                    lf.lfHeight = .CharHeight * sngLabelExCoefficient_
                    lf.lfWidth = .CharWidth * sngLabelExCoefficient_
                    lf.lfEscapement = .Angle
                    lf.lfWeight = .CharWeight
                    lf.lfItalic = Abs(CLng(.FontItalic))
                    lf.lfUnderline = Abs(CLng(.FontUnderline))
                    lf.lfStrikeOut = Abs(CLng(.FontStrikeOut))
                    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
                    lf.lfClipPrecision = OUT_DEFAULT_PRECIS
                    lf.lfQuality = DEFAULT_QUALITY
                    lf.lfPitchAndFamily = DEFAULT_PITCH Or FF_DONTCARE
                    lf.lfCharSet = DEFAULT_CHARSET
                    TempByteArray = StrConv(.FontName & vbNullChar, vbFromUnicode)
                    For j = 0 To UBound(TempByteArray)
                        lf.lfFaceName(j) = TempByteArray(j)
                    Next j
                    FontToUse = CreateFontIndirect(lf)
                    intBorderWidth = Abs(Int(-(.BorderWidth / 10) * sngLineWidthCoefficient_))
                    If intBorderWidth > 0 Then
                        OutputObj.DrawWidth = intBorderWidth
                        OutputObj.FillStyle = .FillStyle
                        OutputObj.FillColor = .FillColor
                        OutputObj.Line (x1_, y1_)-(x2_, y2_), .BorderColor, B
                    End If
                    oldFont = SelectObject(lngDC, FontToUse)
                    Draw_Text lngDC, Replace(.Caption, "&[Page]", Page), _
                                        CLng(OutputObj.ScaleX(x1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleX(x2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        Align
                    If oldFont <> 0 Then
                        Call SelectObject(lngDC, oldFont)
                        Call DeleteObject(FontToUse)
                        FontToUse = 0
                        oldFont = 0
                    End If
                End With
            Case "clsLineAttributes"
                Set CLineA = PrinterEx.Repeats(i)
                With CLineA
                    OutputObj.DrawWidth = Abs(Int(-(.Size / 10) * sngLineWidthCoefficient_))
                    OutputObj.Line (.X1 * Zoom + sngLeftMargin, .Y1 * Zoom + IndentY)- _
                                                (.X2 * Zoom + sngLeftMargin, .Y2 * Zoom + IndentY), .Color
                End With
            Case "clsImageAttributes"
                Set CImageA = PrinterEx.Repeats(i)
                With CImageA
                    If IsMissing(.Height) Then
                        y1_ = .Image.Height / 100
                    Else
                        y1_ = CSng(.Height)
                    End If

                    If IsMissing(.Width) Then
                        x1_ = .Image.Width / 100
                    Else
                        x1_ = CSng(.Width)
                    End If

                    OutputObj.PaintPicture .Image, .Left * Zoom + sngLeftMargin, .Top * Zoom + IndentY, x1_ * Zoom, y1_ * Zoom
                End With
            Case "clsRectangleAttributes"
                Set CRectangleA = PrinterEx.Repeats(i)
                With CRectangleA
                    OutputObj.DrawWidth = Abs(Int(-(.BorderWidth / 10) * sngLineWidthCoefficient_))
                    OutputObj.FillStyle = .FillStyle
                    OutputObj.FillColor = .FillColor
                    OutputObj.Line (.Left * Zoom + sngLeftMargin, .Top * Zoom + IndentY)- _
                                                (.Right * Zoom + sngLeftMargin, .Bottom * Zoom + IndentY), .BorderColor, B
                End With
            Case "clsPointAttributes"
                Set CPointA = PrinterEx.Repeats(i)
                With CPointA
                    OutputObj.DrawWidth = Abs(Int(-(.Size / 10) * sngLineWidthCoefficient_))
                    PointArray = .PointArray
                    lngBorderColor = .Color
                    For j = 1 To UBound(PointArray, 2)
                        OutputObj.PSet (PointArray(1, j) * Zoom + sngLeftMargin, PointArray(2, j) * Zoom + IndentY), lngBorderColor
                    Next j
                End With
        End Select
    Next i

    'Report Body
    For i = 1 To colPage(Page).Count
        strItem = TypeName(colPage(Page)(i))
        Select Case strItem
            Case "clsPointAttributes"
                Set CPointA = colPage(Page)(i)
                With CPointA
                    OutputObj.DrawStyle = vbSolid
                    OutputObj.DrawWidth = Abs(Int(-.Size * Zoom))
                    lngBorderColor = .Color
                    PointArray = .PointArray
                    For j = 1 To UBound(PointArray, 2)
                        OutputObj.PSet (PointArray(1, j) * Zoom + IndentX, PointArray(2, j) * Zoom + IndentY), lngBorderColor
                    Next j
                End With
            Case "clsLineAttributes"
                Set CLineA = colPage(Page)(i)
                With CLineA
                    OutputObj.DrawStyle = vbSolid
                    OutputObj.DrawWidth = Abs(Int(-.Size * Zoom))
                    OutputObj.Line (.X1 * Zoom + IndentX, .Y1 * Zoom + IndentY)- _
                                                (.X2 * Zoom + IndentX, .Y2 * Zoom + IndentY), .Color
                End With
            Case "clsRectangleAttributes"
                Set CRectangleA = colPage(Page)(i)
                With CRectangleA
                    OutputObj.DrawStyle = vbSolid
                    OutputObj.DrawWidth = Abs(Int(-.BorderWidth * Zoom))
                    OutputObj.FillStyle = .FillStyle
                    OutputObj.FillColor = .FillColor
                    OutputObj.Line (.Left * Zoom + IndentX, .Top * Zoom + IndentY)- _
                                                (.Right * Zoom + IndentX, .Bottom * Zoom + IndentY), .BorderColor, B
                End With
            Case "clsLabelAttributes"
                Set CLabelA = colPage(Page)(i)
                With CLabelA
                    x1_ = .Left * Zoom + IndentX
                    y1_ = .Top * Zoom + IndentY
                    x2_ = .Right * Zoom + IndentX
                    y2_ = .Bottom * Zoom + IndentY

                    sngCellSpacing = .CellSpacing * Zoom
                    If .BorderWidth > 0 Then
                        OutputObj.DrawStyle = vbSolid
                        OutputObj.DrawWidth = Abs(Int(-.BorderWidth * Zoom))
                        OutputObj.FillColor = .FillColor
                        OutputObj.FillStyle = .FillStyle
                        OutputObj.Line (x1_, y1_)-(x2_, y2_), .BorderColor, B
                    End If

                    OutputObj.FontBold = .FontBold
                    OutputObj.FontItalic = .FontItalic
                    OutputObj.FontName = .FontName
                    OutputObj.FontSize = .FontSize * Zoom
                    OutputObj.FontStrikethru = .FontStrikethru
                    OutputObj.FontUnderline = .FontUnderline
                    OutputObj.ForeColor = .ForeColor
                    Draw_Text lngDC, .Caption, _
                                        CLng(OutputObj.ScaleX(x1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleX(x2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        .Align
                End With
            Case "clsLabelExAttributes"
                Set CLabelExA = colPage(Page)(i)
                With CLabelExA
                    x1_ = .Left * Zoom + IndentX
                    y1_ = .Top * Zoom + IndentY
                    x2_ = .Right * Zoom + IndentX
                    y2_ = .Bottom * Zoom + IndentY

                    sngCellSpacing = .CellSpacing * Zoom
                    If .BorderWidth > 0 Then
                        OutputObj.DrawStyle = vbSolid
                        OutputObj.DrawWidth = Abs(Int(-.BorderWidth * Zoom))
                        OutputObj.FillColor = .FillColor
                        OutputObj.FillStyle = .FillStyle
                        OutputObj.Line (x1_, y1_)-(x2_, y2_), .BorderColor, B
                    End If

                    lf.lfHeight = .CharHeight * Zoom
                    lf.lfWidth = .CharWidth * Zoom
                    lf.lfEscapement = .Angle
                    lf.lfWeight = .CharWeight
                    lf.lfItalic = Abs(CLng(.FontItalic))
                    lf.lfUnderline = Abs(CLng(.FontUnderline))
                    lf.lfStrikeOut = Abs(CLng(.FontStrikeOut))
                    lf.lfOutPrecision = OUT_DEFAULT_PRECIS
                    lf.lfClipPrecision = OUT_DEFAULT_PRECIS
                    lf.lfQuality = DEFAULT_QUALITY
                    lf.lfPitchAndFamily = DEFAULT_PITCH Or FF_DONTCARE
                    lf.lfCharSet = DEFAULT_CHARSET
                    TempByteArray = StrConv(.FontName & vbNullChar, vbFromUnicode)
                    For j = 0 To UBound(TempByteArray)
                        lf.lfFaceName(j) = TempByteArray(j)
                    Next j
                    FontToUse = CreateFontIndirect(lf)

                    OutputObj.ForeColor = .ForeColor
                    oldFont = SelectObject(lngDC, FontToUse)
                    Draw_Text lngDC, .Caption, _
                                        CLng(OutputObj.ScaleX(x1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y1_ + sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleX(x2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        CLng(OutputObj.ScaleY(y2_ - sngCellSpacing, vbMillimeters, vbPixels)), _
                                        .Align

                    If oldFont <> 0 Then
                        Call SelectObject(lngDC, oldFont)
                        Call DeleteObject(FontToUse)
                        oldFont = 0
                        FontToUse = 0
                    End If
                End With
            Case "clsImageAttributes"
                Set CImageA = colPage(Page)(i)
                With CImageA
                    If IsMissing(.Height) Then
                        y1_ = .Image.Height / 100
                    Else
                        y1_ = CSng(.Height)
                    End If

                    If IsMissing(.Width) Then
                        x1_ = .Image.Width / 100
                    Else
                        x1_ = CSng(.Width)
                    End If

                    OutputObj.PaintPicture .Image, .Left * Zoom + IndentX, .Top * Zoom + IndentY, x1_ * Zoom, y1_ * Zoom
                End With
            Case "clsTableAttributes"
                Set CTableA = colPage(Page)(i)
                With CTableA
                    TextArray() = .TextArray
                    ly = LBound(TextArray, 2): uy = UBound(TextArray, 2)
                    FirstRowIsTitle = .FirstRowIsTitle
                    lngTotalRows = uy - ly + 1
                    lngStartRow = 0: lngEndRow = ly - 1
                    If FirstRowIsTitle Then
                        lngTotalRows = lngTotalRows - 1
                        lngEndRow = lngEndRow + 1
                    End If
                    sngCellSpacing = .CellSpacing * Zoom
                    sngColumnSpacing = .ColumnSpacing * Zoom
                    sngTitleHeight = .TitleHeight * Zoom
                    sngBodyHeight = .BodyHeight * Zoom
                    lngTotalColumns = .TotalColumns
                    lngColumnsCount = .Columns.Count - 1
                    lngBorderColor = .BorderColor
                    intBorderWidth = Abs(Int(-.BorderWidth * Zoom))
                    sngTableTop = .Top * Zoom + IndentY

                    ReDim sngColsWidth(1 To lngColumnsCount)
                    sngTableWidth = 0
                    For j = 1 To lngColumnsCount
                        sngColsWidth(j) = .Columns(j).ColumnWidth * Zoom
                        sngTableWidth = sngTableWidth + sngColsWidth(j)
                    Next j

                    If intBorderWidth > 0 Then
                        OutputObj.DrawStyle = vbSolid
                        OutputObj.DrawWidth = intBorderWidth
                    Else
                        OutputObj.DrawStyle = vbInvisible
                    End If

                    y1_ = 0
                    x2_ = .Left * Zoom + IndentX - sngCellSpacing
'                    If lngTotalColumns = 1 Then x2_ = x2_ - sngCellSpacing
                    x1__ = x2_ + sngCellSpacing
                    y1__ = sngTableTop
                    y2_ = sngTableTop - sngCellSpacing
                    If FirstRowIsTitle Then
                        y1_ = sngTableTop + sngCellSpacing
                        y2_ = y2_ + sngTitleHeight
                        y1__ = y1_ - sngCellSpacing

                        For j = 1 To lngTotalColumns
                            OutputObj.Line (x2_ + sngCellSpacing, y1__)-(x2_ + sngCellSpacing + sngTableWidth, y1__), lngBorderColor

                            strTableLex = Split(TextArray(lngColumnsCount + 1, 1), ",")

                            For Col_ = 1 To lngColumnsCount
                                x1_ = x2_ + 2 * sngCellSpacing
                                x2_ = x2_ + sngColsWidth(Col_)
                                Align = .Columns(Col_).TitleAlign
                                If .TitleWordWrap Then Align = Align Or DT_WORDBREAK
                                OutputObj.FontBold = .TitleFontBold
                                OutputObj.FontItalic = .TitleFontItalic
                                OutputObj.FontName = .TitleFontName
                                OutputObj.FontSize = .TitleFontSize * Zoom
                                OutputObj.FontStrikethru = .TitleFontStrikethru
                                OutputObj.FontUnderline = .TitleFontUnderline
                                OutputObj.ForeColor = .TitleForeColor
'Exit Sub
                                lngAryIndex = 4 * Col_ - 4
                                If UBound(strTableLex) >= lngAryIndex Then
                                    If Val(strTableLex(lngAryIndex)) = Col_ Then
                                        OutputObj.FillStyle = Val(strTableLex(lngAryIndex + 1))
                                        OutputObj.FillColor = Val(strTableLex(lngAryIndex + 2))
                                        OutputObj.Line (x1_ - sngCellSpacing, y1_ - sngCellSpacing)-(x2_ + sngCellSpacing, y2_ + sngCellSpacing), Val(strTableLex(lngAryIndex + 3)), B
                                    End If
                                End If

                                Draw_Text lngDC, TextArray(Col_, 1), _
                                                    CLng(OutputObj.ScaleX(x1_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleY(y1_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleX(x2_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleY(y2_, vbMillimeters, vbPixels)), _
                                                    Align
                            Next Col_
                            x2_ = x2_ + sngColumnSpacing
                        Next j
                    End If

                    lngRowsOnPage = Abs(Int(-lngTotalRows / lngTotalColumns))

                    x2_ = .Left * Zoom + IndentX - sngCellSpacing
                    For j = 1 To lngTotalColumns
                        lngStartRow = lngEndRow + 1
                        If lngStartRow > uy Then Exit For
                        lngEndRow = lngEndRow + lngRowsOnPage
                        If lngEndRow > uy Then
                            lngEndRow = uy
                        End If

                        For Col_ = 1 To lngColumnsCount
                            x1_ = x2_ + 2 * sngCellSpacing
                            x2_ = x2_ + sngColsWidth(Col_)

                            Align = .Columns(Col_).BodyAlign
                            If .BodyWordWrap Then Align = Align Or DT_WORDBREAK
                            OutputObj.FontBold = .BodyFontBold
                            OutputObj.FontItalic = .BodyFontItalic
                            OutputObj.FontName = .BodyFontName
                            OutputObj.FontSize = .BodyFontSize * Zoom
                            OutputObj.FontStrikethru = .BodyFontStrikethru
                            OutputObj.FontUnderline = .BodyFontUnderline
                            OutputObj.ForeColor = .BodyForeColor

                            For k = lngStartRow To lngEndRow
                                y1_ = y2_ + 2 * sngCellSpacing
                                y2_ = y2_ + sngBodyHeight

                                strTableLex = Split(TextArray(lngColumnsCount + 1, k), ",")
                                
                                lngAryIndex = 4 * Col_ - 4
                                If UBound(strTableLex) >= lngAryIndex Then
                                    If Val(strTableLex(lngAryIndex)) = Col_ Then
                                        OutputObj.FillStyle = Val(strTableLex(lngAryIndex + 1))
                                        OutputObj.FillColor = Val(strTableLex(lngAryIndex + 2))
                                        OutputObj.Line (x1_ - sngCellSpacing, y1_ - sngCellSpacing)-(x2_ + sngCellSpacing, y2_ + sngCellSpacing), Val(strTableLex(lngAryIndex + 3)), B
                                    End If
                                End If

                                Draw_Text lngDC, TextArray(Col_, k), _
                                                    CLng(OutputObj.ScaleX(x1_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleY(y1_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleX(x2_, vbMillimeters, vbPixels)), _
                                                    CLng(OutputObj.ScaleY(y2_, vbMillimeters, vbPixels)), _
                                                    Align

                                If Col_ = 1 Then
                                    OutputObj.Line (x1_ - sngCellSpacing, y1_ - sngCellSpacing)-(x1_ - sngCellSpacing + sngTableWidth, y1_ - sngCellSpacing), lngBorderColor
                                End If
                            Next k

                            If Col_ = 1 Then
                                OutputObj.Line (x1_ - sngCellSpacing, y2_ + sngCellSpacing)-(x1_ - sngCellSpacing + sngTableWidth, y2_ + sngCellSpacing), lngBorderColor
                            End If
                            OutputObj.Line (x1_ - sngCellSpacing, y1__)-(x1_ - sngCellSpacing, y2_ + sngCellSpacing), lngBorderColor
                            If Col_ = lngColumnsCount Then
                                OutputObj.Line (x2_ + sngCellSpacing, y1__)-(x2_ + sngCellSpacing, y2_ + sngCellSpacing), lngBorderColor
                            End If
                            y2_ = sngTableTop - sngCellSpacing
                            If FirstRowIsTitle Then y2_ = y2_ + sngTitleHeight
                        Next Col_
                        x2_ = x2_ + sngColumnSpacing
                    Next j
                End With
        End Select
    Next i

    OutputObj.DrawStyle = vbInvisible
    OutputObj.Line (-1, -1)-(sngLeftMargin, sngPH + IndentY), vbWhite, BF
    OutputObj.Line (sngPW - sngRightMargin + IndentX, -1)-(sngPW + 2 * IndentX, sngPH), vbWhite, BF
End Sub

Private Sub Draw_Text(hDC As Long, _
                                           Text As String, _
                                           Left As Long, _
                                           Top As Long, _
                                           Right As Long, _
                                           Bottom As Long, _
                                           Optional Align As TextAlignConstants = DT_LEFT)

Dim OutputRect As RECT

    With OutputRect
        .Top = Top
        .Bottom = Bottom
        .Left = Left
        .Right = Right
    End With
    Call DrawText(hDC, Text, Len(Text), OutputRect, Align)
End Sub

Public Sub SelectText(TxtBox As TextBox)
    On Error Resume Next
    With TxtBox
        .SelStart = 0
        .SelLength = Len(.Text)
    End With
End Sub
