实现功能如下:设好打印边距打印方向后,根据vsflexgrid表格中的内容自动填满vsprinter可打印区域的宽度,而vsprinter各列宽度比例值保持和原表格中各列一样的比例。每一页都有表头标题。在from1中的有fg(vsflexgrid控件)有来显示数据,frmPrint中有一个vp(vsprinter控件)和一个vpfg(vsflexgrid控件).
打印预览时,先把fg中的数据内容,各列宽度,固定表头,固定表头合并单元格情况这些属性复制给vpfg.然后用vp.RenderControl = fg2.hwnd来实现打印。
一张报表从上到下为:
主标题,(用vp.Header)
副标题(用vp.Text,多行)
vpfg表格肉容''出问题的地方。
窜数显示(用vp.Footer)
-------------
有一个问题:就是打印多页的时候,如10多页以上,前一页和后一页总有总少两行内容,表现为前一面的最后两行在预时为空,
看起来象是合并的一个大的单元格一样,每页的最后都是这样。请高手帮看看是什么原因,如果好的 实现这个功能的语句,请贴出来或发给我
[email protected],谢谢了!
代码如下:Option Explicit
Public begPrint As Boolean
Public fg As VSFlexGrid
Public frm As Form
Private Sub Form_Load()
    Vp.MousePointer = mpArrowHourGlass
    gsubGetPrintSet vpPrt.frm '得到边距等打印设
    loadSomeThing vpPrt.fg ' 复制fg表格内容 和属性到vpfg
    mainPrint vpPrt.frm
    
    Vp.MousePointer = mpDefaultEnd Sub

解决方案 »

  1.   


    Public Sub loadSomeThing(fg As VSFlexGrid) 'fg in will print from.    Dim i As Integer
        Dim j As Integer    'MsgBox fg.Width & "," & vpFG.Width
        Dim remcol As Integer
        remcol = 999 '记录摘要字段所在列!
        
        '/////////
        begPrint = False
        With vpFG             'vp.vpfg<=printfrm.fg
            .FixedCols = fg.FixedCols
            .FixedRows = fg.FixedRows
            
            .Cols = fg.Cols
            .Rows = fg.Rows
            .FixedCols = fg.FixedCols
            .FixedRows = fg.FixedRows
            .Appearance = flexFlat
            .AutoResize = False
            .BackColor = vbWhite
            .BackColorBkg = vbWhite
            .BackColorFixed = vbWhite
            
            
            .MergeCells = fg.MergeCells
        
            For j = 0 To .Cols - 1
                If fg.TextMatrix(0, j) = "摘要" Then
                    remcol = j
                End If
            Next j
            
            For i = 0 To fg.Rows - 1
                For j = 0 To fg.Cols - 1
                    'If i = 0 And fg.ColHidden(j) = True Then MsgBox j
                    .TextMatrix(i, j) = fg.TextMatrix(i, j)
                    If j = remcol And remcol <> 0 And i > .FixedRows - 1 Then '设表格中摘要字段对齐方式!
                        .Cell(flexcpAlignment, i, j, i, j) = fg.Cell(flexcpAlignment, i, j, i, j)
                    End If
                Next j
                .MergeRow(i) = fg.MergeRow(i)
            Next i
            
            
            
            For j = 0 To .Cols - 1
                .FixedAlignment(j) = fg.FixedAlignment(j)
                .ColAlignment(j) = fg.ColAlignment(j)
                .MergeCol(j) = fg.MergeCol(j)
                
                
                If fg.ColHidden(j) <> True And fg.ColWidth(j) > 15 Then
                    .ColWidth(j) = fg.ColWidth(j)
                    'MsgBox fg.ColWidth(j) & "U"
                Else
                    .ColHidden(j) = True
                End If
            Next
            
            For j = fg.Rows - 1 To 0 Step 1
                If .RowHidden(j) <> True And fg.RowHeight(j) > 15 Then
                    .RowHeight(j) = fg.RowHeight(j)
                Else
                    .RemoveItem j
                End If
            Next
                   
        End With
        
    End Sub
    Public Sub mainPrint(frm As Form)
        
        '===============     
         Vp.MarginLeft = vpPrt.vpL '左边距
         Vp.MarginRight = vpPrt.vpR
         Vp.MarginBottom = vpPrt.vpB
         Vp.MarginTop = vpPrt.vpT
         Vp.Orientation = vpPrt.vpHZ'打倒方向     
        '=========
       
        Dim i As Integer
        Dim j As Integer
        
        Dim vpFgWidth As Long
        vpFgWidth = 0
            
        With vpFG    
            
            For i = 0 To .Cols - 1
                If .ColHidden(i) = False Then              
                    vpFgWidth = vpFgWidth + .ColWidth(i)
                End If
            Next i        
            
            Dim maxVpWidth As Long
            maxVpWidth = Vp.PageWidth - Vp.MarginLeft - Vp.MarginRight
                  
            Dim newMaxVpWidth As Long
            newMaxVpWidth = Int(Round(maxVpWidth) / 15) * 15 
            Do While newMaxVpWidth > maxVpWidth
                newMaxVpWidth = newMaxVpWidth - 15
            Loop
                    
            maxVpWidth = newMaxVpWidth
            
            Dim TempColWillWidth As Long
            For i = 0 To .Cols - 1
                If .ColHidden(i) = True Then
                    vpFG.ColWidth(i) = 0
                    vpFG.ColHidden(i) = True
                Else
                    TempColWillWidth = Int(Round(maxVpWidth * (vpFG.ColWidth(i) / vpFgWidth)) / 15) * 15
                    If newMaxVpWidth - TempColWillWidth >= 0 Then
                        .ColWidth(i) = TempColWillWidth
                    Else
                        .ColWidth(i) = 0
                    End If
                End If
            Next i
           
        End With
        
        begPrint = True    
        With Vp
            makeMHeader                       
            .StartDoc
             vpFG.FontSize = IIf(vpPrt.bdfontSize = 0, 9, vpPrt.bdfontSize)         
            .RenderControl = vpFG.hwnd
            .EndDoc
            
        End With
        
    '''///////////////////////////////////
    End Sub
    Private Sub makeMHeader()
        With Vp
            .HdrFontSize = IIf(vpPrt.hdfontSize = 0, 9, vpPrt.hdfontSize)
            .Header = vpPrt.mTitle
            
            .HdrFontSize = IIf(vpPrt.ftfontSize = 0, 5, vpPrt.ftfontSize)
            .Footer = "|第" & "%d" & "页|" '& "/共" & .PageCount & "|"   'vbLf &
            
            .FontSize = IIf(vpPrt.mhdfontSize = 0, 5, vpPrt.mhdfontSize)
            
            If Trim(vpPrt.m11 & vpPrt.m12 & vpPrt.m13) <> "" Then
                .TextAlign = taLeftMiddle
                .Text = vpPrt.m11
                .TextAlign = taCenterMiddle
                .Text = vpPrt.m12
                .TextAlign = taRightMiddle
                .Text = vpPrt.m13
            End If
            If Trim(vpPrt.m21 & vpPrt.m22 & vpPrt.m23) <> "" Then
                .TextAlign = taLeftBottom
                .Paragraph = ""
                .TextAlign = taLeftMiddle
                .Text = vpPrt.m21
                .TextAlign = taCenterMiddle
                .Text = vpPrt.m22
                .TextAlign = taRightMiddle
                .Text = vpPrt.m23
            End If
            .TextAlign = taLeftMiddle
            .Paragraph = ""
        End With
    End Sub
    Private Sub Form_Resize()
        Vp.Top = 0
        Vp.Left = 0
        Vp.Width = Me.ScaleWidth
        Vp.Height = Me.ScaleHeight
        
    End SubPrivate Sub Vp_NewPage()
        Call makeMHeader
    End Sub
    ----
    我认为打印不好做的就是动态打印。