代码越少越好!
打印整个窗体吧
Visual Basic的Printer对象支持PaintPicture方法,可以支持打印位图。其语法为:
Printer.PaintPicture picture, x1, y1, width1, height1, x2, y2, width2, height2, opcode
参数Picture是必需的,指明要绘制到打印机上的图形的来源源,通常是对象的Picture或Image属性。
参数x1, y1也是必需的,均为单精度数值,指定参数picture所确定图形在打印机上绘制的坐标。其值的单位是由Printer的 ScaleMode 属性决定的。
参数Width1和Height1是可选的,都是单精度数值,指示图象的目标宽度和高度。如果目标宽度/高度比源宽度 (width2)/高度大或者小,将适当地拉伸或压缩图形。如果省略这两个参数,则使用图形的原始尺寸。
参数x2, y2、Width2和Height2是可选的。它们指示参数picture确定的图象内剪贴区的坐标和大小。利用这四个参数,我们可以打印图象的一部分。默认是打印整个图象。
参数Opcode是可选的,是长型数值。它用来定义在将图象绘制到打印机上时对图象执行的位操作。关于位操作符常数的完整列表,请参阅 Visual Basic帮助文件中的有关内容。对于打印机来说,这个参数较少使用;而在屏幕显示图象时往往利用这个参数实现一些特殊效果。
通过使用负的目标高度值 (height1)或目标宽度值 (width1) ,可以水平或垂直翻转位图。
下面是一个简单的例子:
Printer.PaintPicture Picture1.Image, 0, 0
Picture1.Print
使用api的绘图函数将图象绘制到vb的printer对象上,最后记得使用printer自己的方法画一点内容上去,不然会打印不出东西的.
Attribute VB_Name = "PrintModule"
***********************************************
API Declares
2002.11.14
Private Type RECT
Left As Long
As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
x As Long
y As Long
End Type
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
Private Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Private Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
***********************************************
draw a line from (X1,Y1) to (X2,Y2) on the hdc
2002.11.14
Private Sub P_DrawLine(DC As Long, X1 As Long, Y1 As Long, X2 As Long, Y2 As Long)
Dim PointTmp As POINTAPI
If MoveToEx(DC, X1, Y1, PointTmp) = 0 Then
Debug.Print "Error when moveto point!!"
End If
If LineTo(DC, X2, Y2) = 0 Then
Debug.Print "Error when draw line!!"
End If
End Sub
***********************************************
draw the text in rect(X1,Y1,X1+Wid,Y1+Hei) on the hdc
2002.11.14
Private Sub P_DrawText(DC As Long, Text As String, X1 As Long, Y1 As Long, Wid As Long, Hei As Long)
Dim TextRect As RECT
TextRect.Left = X1
TextRect. = Y1
TextRect.Right = X1 + Wid
TextRect.Bottom = Y1 + Hei
If DrawText(DC, Text, Len(Text), TextRect, 0) = 0 Then
Debug.Print "Error when draw text!!"
End If
End Sub
***********************************************
print listview on the pictures canvas or printers canvas
if the canvas is a picturebox, only the fist page will be displayed
2002.11.14
Public Function PrintListview(LView As ListView, Canvas As Object, Wid As Integer, Hei As Integer) As Boolean
On Error GoTo ErrorHandler
Dim PageRect As RECT
Dim TextHeight As Long
Dim ColumnWidth As Long
Dim CurLine As Long, CurColumn As Long
Dim I As Integer, J As Integer
PrintListview = True
If LView.ColumnHeaders.Count = 0 Then
Exit Function
End If
If (Canvas Is Printer) Then
Canvas.Print "." !!use printer objects original method to print something,else nothing will be printed!!
End If
***********************************************
set the initialize values
TextHeight = Canvas.TextHeight("H")
PageRect.Left = Wid * 0.05
PageRect. = Hei * 0.05
PageRect.Right = Wid * 0.95
PageRect.Bottom = Hei * 0.9
ColumnWidth = (PageRect.Right - PageRect.Left) / LView.ColumnHeaders.Count
CurLine = PageRect.
CurColumn = PageRect.Left
***********************************************
print column header
Call P_DrawLine(Canvas.hdc, PageRect.Left, CurLine, PageRect.Right, CurLine)
CurLine = CurLine + Hei * 0.005
For J = 1 To LView.ColumnHeaders.Count
Call P_DrawText(Canvas.hdc, Space(2) & LView.ColumnHeaders.Item(J).Text, CurColumn, CurLine, ColumnWidth, TextHeight)
CurColumn = CurColumn + ColumnWidth
Next
CurLine = CurLine + TextHeight + Hei * 0.005
Call P_DrawLine(Canvas.hdc, PageRect.Left, CurLine, PageRect.Right, CurLine)
CurLine = CurLine + Hei * 0.005
***********************************************
print items
For I = 1 To LView.ListItems.Count
Set ListViewItem = LView.ListItems(I)
CurColumn = PageRect.Left
Call P_DrawText(Canvas.hdc, Space(2) & LView.ListItems(I).Text, CurColumn, CurLine, ColumnWidth, TextHeight)
CurColumn = CurColumn + ColumnWidth
For J = 1 To LView.ColumnHeaders.Count - 1
Call P_DrawText(Canvas.hdc, Space(2) & ListViewItem.SubItems(J), CurColumn, CurLine, ColumnWidth, TextHeight)
CurColumn = CurColumn + ColumnWidth
Next
CurLine = CurLine + TextHeight + Hei * 0.005
Call P_DrawLine(Canvas.hdc, PageRect.Left, CurLine, PageRect.Right, CurLine)
CurLine = CurLine + Hei * 0.005
***********************************************
finish current page,start next page
If (CurLine >= PageRect.Bottom) And (I <> LView.ListItems.Count) Then
***********************************************
draw column line
CurColumn = PageRect.Left
CurLine = CurLine - Hei * 0.005
For J = 1 To LView.ColumnHeaders.Count + 1
Call P_DrawLine(Canvas.hdc, CurColumn, PageRect., CurColumn, CurLine)
CurColumn = CurColumn + ColumnWidth
Next
***********************************************
if neednt to draw next page
If Not (Canvas Is Printer) Then
Exit Function
Else
Call P_DrawText(Canvas.hdc, "Page:" & Canvas.Page, PageRect.Left, PageRect.Bottom + 2 * TextHeight, PageRect.Right, PageRect.Bottom + 3 * TextHeight)
Canvas.NewPage
Canvas.Print "." !!!!!!!!!!!!!
End If
***********************************************
reset the initialize values
CurLine = PageRect.
CurColumn = PageRect.Left
***********************************************
print column header of next page
Call P_DrawLine(Canvas.hdc, PageRect.Left, CurLine, PageRect.Right, CurLine)
CurLine = CurLine + Hei * 0.005
For J = 1 To LView.ColumnHeaders.Count
Call P_DrawText(Canvas.hdc, Space(2) & LView.ColumnHeaders.Item(J).Text, CurColumn, CurLine, ColumnWidth, TextHeight)
CurColumn = CurColumn + ColumnWidth
Next
CurLine = CurLine + TextHeight + Hei * 0.005
Call P_DrawLine(Canvas.hdc, PageRect.Left, CurLine, PageRect.Right, CurLine)
CurLine = CurLine + Hei * 0.005
End If
Next
***********************************************
draw column line
CurColumn = PageRect.Left
CurLine = CurLine - Hei * 0.005
For J = 1 To LView.ColumnHeaders.Count + 1
Call P_DrawLine(Canvas.hdc, CurColumn, PageRect., CurColumn, CurLine)
CurColumn = CurColumn + ColumnWidth
Next
***********************************************
end printing
If (Canvas Is Printer) Then
Call P_DrawText(Canvas.hdc, "Page:" & Canvas.Page, PageRect.Left, PageRect.Bottom + 2 * TextHeight, PageRect.Right, PageRect.Bottom + 3 * TextHeight)
Canvas.EndDoc
End If
Exit Function
ErrorHandler:
PrintListview = False
MsgBox Err.Number & ":" & Err.Description
If Canvas Is Printer Then
Canvas.KillDoc
End If
End Function