ListView ,MSFlexGrid 直接输出到打印机的函数写法!问题解决人:laviewpbt(人一定要靠自己) ,mylzw(芃) 把ListView 里的内容做个参数为ListView 对象,使用VB的Printer 对象打印出来的函数!下面是打印MSFlexGrid 的代码,不知道怎么打印我说的ListView!以下是参考代码:Sub print_grid(Grd As MSFlexGrid, MainTitle As String, SecTitle As String, PageLine As Integer, DjCol As Integer, JeCol As Integer) Dim x0 As Single, y0 As Single Dim x As Single, y As Single Dim fs As String * 10 Dim zje As Currency, yje As Currency Dim HzNum As Integer Dim n As Integer, r As Integer Dim dx(0 To 20) As Integer Dim Cellf(0 To 20) As Single Dim str1 As String, str2 As String, str3 As String, str4 As String Dim I As Integer, j As Integer, k As Integer 'Load printer 'printer.Show If Grd.Rows <= 1 Then MsgBox "本表格没有数据,没有必要打印!", 48, "提示" Exit Sub End If x0 = 10 '打印边界 y0 = 20 With Grd '计算栏空 dx(i) .Row = 1 w = .Width For I = .Cols - 1 To 1 Step -1 .Col = I dx(I) = (.CellWidth + 72) / 56.7 / 4.233 w = w - (.CellWidth + 72) Next I dx(0) = w / 4.233 / 56.7 '计算 str1 到 str4 str1 = "┌" str2 = "├" str3 = "│" str4 = "└" HzNum = 1 '汉字总数 For I = 0 To .Cols - 1 Cellf(I) = HzNum * 4.233 '计算每栏起始打印位置 For j = 1 To dx(I) str1 = str1 + "─" str2 = str2 + "─" str3 = str3 + " " str4 = str4 + "─" Next j HzNum = HzNum + dx(I) + 1 If I < .Cols - 1 Then str1 = str1 + "┬" str2 = str2 + "┼" str3 = str3 + "│" str4 = str4 + "┴" Else str1 = str1 + "┐" str2 = str2 + "┤" str3 = str3 + "│" str4 = str4 + "┘" End If Next I Printer.ScaleMode = 6 '毫米为单位Printer.Width = 210 * 56.7 '窄行打印纸大小Printer.Height = 297 * 56.7k = 1n = 0 '当前页数zje = 0 '总计金额 r = Int((.Rows + PageLine - 2) / PageLine) '总页数While k <= .Rows - 1 yje = 0 '页小计金额' printer.FontName = "楷书" Printer.FontName = "宋体" Printer.FontBold = True Printer.FontSize = 18 x = x0 + (HzNum * 4.233 - Len(MainTitle) * 6.46) / 2 y = y0 Printer.CurrentX = x Printer.CurrentY = y Printer.Print MainTitle '打印主标题 Printer.FontBold = False Printer.FontName = "宋体" Printer.FontSize = 12 y = Printer.CurrentY + 3 x = x0 Printer.CurrentX = x Printer.CurrentY = y Printer.Print SecTitle '打印次标题 Printer.CurrentX = x + HzNum * 4.233 - 50 Printer.CurrentY = y Printer.Print Format$(Date, "yyyy-mm-dd") ''打印日期 y = Printer.CurrentY + 2 Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str1 '打印┌───┬──┬─┐ y = Printer.CurrentY Printer.CurrentX = x0 Printer.Print str3 '打印│ │ │ │ For j = 0 To .Cols - 1 '打印表头内容 '表头内容位于单元中间 Printer.CurrentX = x0 + Cellf(j) + (dx(j) - Len(.TextMatrix(0, j))) / 2 * 4.233 Printer.CurrentY = y Printer.Print .TextMatrix(0, j) Next j For I = 1 To PageLine If k = .Rows Then Exit For '表格结束 y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str2 '打印├───┼──┼─┤ y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str3 '打印│ │ │ │ For j = 0 To .Cols - 1 '打印一行的内容 Printer.CurrentX = x0 + Cellf(j) Printer.CurrentY = y If j = DjCol Then fs = " " Mid$(fs, 7 - Len(Format$(.TextMatrix(k, j), "##0.00")), Len(Format$(.TextMatrix(k, j), "##0.00"))) = Format$(.TextMatrix(k, j), "##0.00") Printer.Print Mid$(fs, 1, 6) ElseIf j = JeCol Then fs = " " Mid$(fs, 10 - Len(Format$(.TextMatrix(k, j), "####0.00")), Len(Format$(.TextMatrix(k, j), "####0.00"))) = Format$(.TextMatrix(k, j), "####0.00") Printer.Print fs yje = yje + Val(.TextMatrix(k, j)) zje = zje + Val(.TextMatrix(k, j)) Else Printer.Print .TextMatrix(k, j) End If Next j k = k + 1 Next I n = n + 1 y = Printer.CurrentY Printer.CurrentX = x0 Printer.CurrentY = y Printer.Print str4 '打印└──┴──┴──┘ If zje <> 0 Then Printer.CurrentX = x0 + 80 Printer.Print "本页小计金额:" + Format$(yje, "######0.00") + "元" End If Printer.CurrentX = x0 + 90 Printer.Print "第" + Str$(n) + "页,共" + Str$(r) + "页" If k < .Rows - 1 Then Printer.NewPage Else Printer.CurrentX = x0 + 80 Printer.Print "总计金额:" + Format$(zje, "######0.00") + "元" End IfWendPrinter.EndDocEnd WithEnd Sub 回复人: laviewpbt(人一定要靠自己) ( ) 信誉:100 2005-11-16 20:03:28 得分: 70 Function gPrintListView(ByRef pobjListView As ListView, pstrHeading As String) As Boolean Dim objCol As ColumnHeader Dim objLI As ListItem Dim objILS As ImageList Dim objPic As Picture Dim dblXScale As Double Dim dblYScale As Double Dim sngFontSize As Single Dim lngX As Long Dim lngY As Long Dim lngX1 As Long Dim lngY1 As Long Dim lngX2 As Long Dim lngRows As Long Dim lngLeft As Long Dim lngPageNo As Long Dim lngEOP As Long Dim lngEnd As Long Dim lngWidth As Long Dim intCols As Integer Dim lng As Long Dim intOffset As Integer Dim px As Integer Dim py As Integer Dim intRowHeight As Integer Dim strText As String Dim strTextTrun As String '-------------------------------------------------------------------------- 'Establish print & screen metrics '-------------------------------------------------------------------------- On Error GoTo Error_Handler Screen.MousePointer = vbHourglass For Each objCol In pobjListView.ColumnHeaders lngX = lngX + objCol.Width Next Set objILS = pobjListView.SmallIcons dblXScale = (Printer.Width * 0.9) / lngX dblYScale = Printer.Height / pobjListView.Height lngLeft = (Printer.Width - (Printer.Width * 0.95)) / 2 sngFontSize = Printer.Font.Size If pstrHeading <> "" Then Printer.Font.Size = 12 Printer.CurrentX = (Printer.Width / 2) - (Printer.TextWidth(pstrHeading) / 2) Printer.Font.Underline = True Printer.Print pstrHeading Printer.Font.Underline = False Printer.Font.Size = sngFontSize lng = Printer.CurrentY + Printer.CurrentY End If intRowHeight = (Screen.TwipsPerPixelY * 17) lngEOP = Printer.Height - (intRowHeight * 3) lngX = lngLeft lngY = lngTop lngY1 = lng + (Screen.TwipsPerPixelY * 17) Printer.CurrentY = lngY Printer.Font.Bold = True Printer.DrawMode = vbCopyPen px = Screen.TwipsPerPixelX py = Screen.TwipsPerPixelY '-------------------------------------------------------------------------- 'Print column headers with slight 3D effect '-------------------------------------------------------------------------- For Each objCol In pobjListView.ColumnHeaders lngX1 = lngX + (objCol.Width * dblXScale) Printer.Line (lngX, lngY)-(lngX1, lngY1), vbButtonShadow, BF Printer.Line (lngX, lngY)-(lngX1 - px, lngY1), RGB(245, 245, 245), BF Printer.Line (lngX + px, lngY + py)-(lngX1, lngY1), vbButtonShadow, BF Printer.Line (lngX + px, lngY + py)-(lngX1 - px, lngY1 - py), vbButtonFace, BF Printer.CurrentY = lngY + ((intRowHeight - Printer.TextHeight(objCol.Text)) / 2) + py Select Case objCol.Alignment Case ListColumnAlignmentConstants.lvwColumnCenter Printer.CurrentX = lngX + (((objCol.Width * dblXScale) - Printer.TextWidth(objCol.Text)) / 2) Case ListColumnAlignmentConstants.lvwColumnLeft Printer.CurrentX = lngX + (px * 5) Case ListColumnAlignmentConstants.lvwColumnRight Printer.CurrentX = lngX + ((objCol.Width * dblXScale) - Printer.TextWidth(objCol.Text)) - (px * 5) End Select Printer.Print objCol.Text lngX = lngX1 Next lngEnd = lngX1 + px Printer.Font.Bold = False '-------------------------------------------------------------------------- 'Print list item data '-------------------------------------------------------------------------- For Each objLI In pobjListView.ListItems If lngY1 > lngEOP - intRowHeight - intRowHeight Then '------------------------------------------------------------------ 'Print page number '------------------------------------------------------------------ lngPageNo = lngPageNo + 1 Printer.CurrentX = (Printer.Width / 2) - (Printer.TextWidth("Page " & lngPageNo) / 2) Printer.CurrentY = lngEOP - intRowHeight Printer.Print "Page " & lngPageNo Printer.NewPage Printer.CurrentY = lngTop lngY = lngTop Else lngY = lngY + intRowHeight End If lngX = lngLeft lngY1 = lngY + intRowHeight For Each objCol In pobjListView.ColumnHeaders '------------------------------------------------------------------ 'Print the icon if on col 1 '------------------------------------------------------------------ If objCol.Index > 1 Then strText = objLI.SubItems(objCol.Index - 1) intOffset = 0 Else strText = objLI.Text If IsEmpty(objLI.SmallIcon) Then intOffset = 0 Else Set objPic = objILS.Overlay(objLI.SmallIcon, objLI.SmallIcon) Printer.PaintPicture objPic, lngX + px, lngY + (py / 2), 16 * px, 16 * py, , , , , vbSrcCopy intOffset = px * 16 End If End If '------------------------------------------------------------------ 'Make sure text fits '------------------------------------------------------------------ lngWidth = (objCol.Width * dblXScale) lngX1 = lngX + lngWidth strTextTrun = strText Do Until Printer.TextWidth(strTextTrun) < lngWidth - (px * 5) - intOffset Or strText = "" strText = Left$(strText, Len(strText) - 1) strTextTrun = strText & "..." Loop Printer.Line (lngX, lngY)-(lngX1, lngY1), 1, B Printer.CurrentY = lngY + ((intRowHeight - Printer.TextHeight(strTextTrun)) / 2) + py Select Case objCol.Alignment Case ListColumnAlignmentConstants.lvwColumnCenter Printer.CurrentX = lngX + intOffset + (((objCol.Width * dblXScale) - Printer.TextWidth(strTextTrun)) / 2) Case ListColumnAlignmentConstants.lvwColumnLeft Printer.CurrentX = lngX + intOffset + (px * 5) Case ListColumnAlignmentConstants.lvwColumnRight Printer.CurrentX = lngX + ((objCol.Width * dblXScale) - intOffset - Printer.TextWidth(strTextTrun)) - (px * 5) End Select '------------------------------------------------------------------ 'Print each colum '------------------------------------------------------------------ Printer.Print strTextTrun lngX = lngX1 Next Next '-------------------------------------------------------------------------- 'Print final page number '-------------------------------------------------------------------------- lngPageNo = lngPageNo + 1 Printer.CurrentX = (Printer.Width / 2) - (Printer.TextWidth("Page " & lngPageNo) / 2) Printer.CurrentY = lngEOP - intRowHeight Printer.Print "Page " & lngPageNo Printer.EndDoc gPrintListView = True Screen.MousePointer = vbDefault Set objCol = Nothing Set objILS = Nothing Set objLI = Nothing Set objPic = Nothing Exit Function Error_Handler: Set objCol = Nothing Set objILS = Nothing Set objLI = Nothing Set objPic = Nothing Screen.MousePointer = vbDefault '-------------------------------------------------------------------------- 'Simple error message reporting '-------------------------------------------------------------------------- MsgBox "gPrintListView() failed with the following error:-" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & "Description:" & Err.Description, vbExclamation End Functio 回复人: mylzw(芃) ( ) 信誉:96 2005-11-16 23:49:27 得分: 30 楼主有MSHFLEXGRID打印的代码,再来个LISTVIEW转换为MSHFLEXGRID的函数不得了? '转换ListView中数据至MSHFlexGrid的函数Public Function Lv_To_Fg(ListView As ListView, MSHFlexGrid As MSHFlexGrid) As Long If ListView.View <> lvwReport Then Lv_To_Fg = 0 '非报表结构,无法进行转换 Exit Function End If Dim I As Long Dim j As Long With MSHFlexGrid .Clear .FixedRows = 1 .FixedCols = 0 .Rows = ListView.ListItems.Count + 1 .cols = ListView.ColumnHeaders.Count '同步列宽 For I = 0 To .cols - 1 .colWidth(I) = ListView.ColumnHeaders(I + 1).width Next '开始转换 For I = 0 To .Rows - 1 For j = 0 To .cols - 1 If I = 0 Then '写表头 .TextMatrix(I, j) = ListView.ColumnHeaders(j + 1).Text Else '写内容 If j = 0 Then '写首列 .TextMatrix(I, j) = ListView.ListItems(I).Text Else '写其余列 .TextMatrix(I, j) = ListView.ListItems(I).SubItems(j) End If End If Next Next End With Lv_To_Fg = 1 '转换完毕End Function 回复人: MysticBoys(郁闷中,勿打扰。。。。。。。。。。。。) ( ) 靠,这么多代码啊!哈哈。明天调着那个好用谁就分多!

评论