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
...全文
86 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
  • 打赏
  • 举报
回复
高手帮忙解决
内容概要:本文围绕可重构电池系统中的结构分析用于主动故障诊断展开研究,基于Matlab代码实现相关算法仿真,系统探讨了电池系统在运行过程中可能出现的单点或多点故障,并充分利用系统的可重构特性实现高效故障检测与隔离。研究融合图论方法与系统建模技术,构建电池模块之间的连接拓扑结构,通过解析冗余配置与动态重组机制,显著提升系统在故障情况下的容错能力与诊断精度。文中设计并验证了主动故障诊断策略,通过多场景仿真实验,展示了系统在不同故障模式下的响应特性、诊断准确率及恢复性能,验证了所提方法在提升电池系统可靠性方面的有效性与实用性。; 适合人群:具备电力电子、电池管理系统(BMS)或故障诊断基础知识的科研人员与工程技术人员,尤其适用于从事新能源汽车、储能系统、智能电网等领域研究的研究生及以上学历人员,或具备Matlab仿真与系统建模能力的研发工作者。; 使用场景及目标:①应用于电动汽车动力电池组、大规模储能电站等可重构电源系统的故障诊断设计;②为提升电池系统的安全性、可靠性和运维智能化水平提供理论依据与技术方案;③通过仿真实践掌握基于结构分析的主动故障诊断算法实现流程,支持科研项目开发、系统优化设计及学术论文复现。; 阅读建议:建议结合Matlab代码与文本内容同步研读,重点关注系统拓扑建模、故障传播路径分析及诊断逻辑的实现细节,推荐动手调试与修改仿真参数以深入理解可重构机制对诊断性能的影响,亦可将该方法迁移至其他分布式能源系统的故障诊断研究中。

16,717

社区成员

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

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