boblam 等高手帮忙(PrintDocument选择页打印)

LYDF4151 2008-03-26 05:13:39
以下Code能打印预览,但是不能选择页打印,怎么实现?

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage


Dim CharOfPage As Int16
Dim LineOfPage As Int16
Dim StrForPage As String
Dim strformat As New StringFormat
Dim RectD As System.Drawing.RectangleF = New System.Drawing.RectangleF(e.MarginBounds.Left, e.MarginBounds.Top, e.MarginBounds.Width, e.MarginBounds.Height)
Dim SizeF As System.Drawing.SizeF

SizeF = New System.Drawing.SizeF(e.MarginBounds.Width, e.MarginBounds.Height - TextBox1.Font.Height)
strformat.Trimming = StringTrimming.Word

e.Graphics.MeasureString(StrToPage, TextBox1.Font, SizeF, strformat, CharOfPage, LineOfPage)

StrForPage = StrToPage.Substring(0, CharOfPage)

e.Graphics.DrawString(StrForPage, TextBox1.Font, Brushes.Black, RectD, strformat)

If CharOfPage < StrToPage.Length Then
e.HasMorePages = True
StrToPage = StrToPage.Substring(CharOfPage)
Else
e.HasMorePages = False
StrToPage = TextBox1.Text
End If


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try

PrintPreviewDialog1.Document = PrintDocument1
StrToPage = TextBox1.Text

PrintPreviewDialog1.ShowDialog()

Catch ex As Exception
MsgBox(ex.ToString)


End Try
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
PageSetupDialog1.Document = PrintDocument1
PageSetupDialog1.ShowDialog()

Catch ex As Exception

End Try

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Try
PrintDialog1.Document = PrintDocument1
PrintDialog1.AllowSomePages = True
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If

Catch ex As Exception

End Try
End Sub
...全文
60 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
boblaw 2008-03-27
  • 打赏
  • 举报
回复
你的代码并没有实现打印功能,下面一段代码,你参考一下,已经测试通过。

Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
Dim StrToPage As String
Dim StreamToPrint As StreamReader
Dim FileToPrint As String = "C:\PrintTest.txt"
Private Sub ReadFile()
StreamToPrint = New StreamReader(FileToPrint, System.Text.Encoding.GetEncoding("utf-8"))
End Sub
Private Sub CloseFile()
StreamToPrint.Close()
StreamToPrint.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.PageSetupDialog1.Document = PrintDocument1
Me.PrintPreviewDialog1.Document = PrintDocument1
Me.PrintDialog1.Document = PrintDocument1
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim linesPerPage As Single = 0
Dim linesCount As Integer = 0
Dim yPosition As Integer = 0
Dim lineStr As String = Nothing
Dim printfont As New Font("宋体", 12, FontStyle.Regular, GraphicsUnit.Pixel)
Dim fontHeight = printfont.GetHeight
linesPerPage = e.MarginBounds.Height / FontHeight
While (linesCount < linesPerPage)
lineStr = StreamToPrint.ReadLine()
If lineStr Is Nothing Then Exit While
yPosition = e.MarginBounds.Top + linesCount * fontHeight
e.Graphics.DrawString(lineStr, printfont, Brushes.Black, e.MarginBounds.Left, yPosition)
linesCount += 1
End While
If lineStr IsNot Nothing Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If
End Sub
'打印预览
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ReadFile()
Try
Me.PrintPreviewDialog1.ShowDialog()
Catch ex As Exception
CloseFile()
End Try
End Sub
'页面设置
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.PageSetupDialog1.ShowDialog()
End Sub
'打印
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
ReadFile()
Try
Dim res As DialogResult = Me.PrintDialog1.ShowDialog()
If res = DialogResult.OK Then
PrintDocument1.Print()
End If
Catch ex As Exception
CloseFile()
End Try
End Sub
End Class

LYDF4151 2008-03-27
  • 打赏
  • 举报
回复
up
LYDF4151 2008-03-27
  • 打赏
  • 举报
回复
高手帮忙解决
LYDF4151 2008-03-27
  • 打赏
  • 举报
回复
高手帮忙解决
LYDF4151 2008-03-26
  • 打赏
  • 举报
回复
高手帮忙解决

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧