标题:乱打印的问题
只看楼主
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
Imports System.Drawing.Printing

Private printpagesettings As New PageSettings
    Private stringtoprint As String
    Private printfont As New Font("宋体", 11)
    Private printfont1 As New Font("宋体", 10, FontStyle.Regular)
2013-05-30 19:22
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
程序代码:
 Private Sub printgraphictongzhidan(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim dfk As New Pen(Color.Black, 2)
        Dim newpen As New Pen(Color.Black)
        ev.Graphics.DrawString("阿 图 什 市 建 设 局 建 设 工 程 申 请 表", New Font("宋体", 18, FontStyle.Bold), Brushes.Black, 177, 99)
        ev.Graphics.DrawRectangle(dfk, 60, 140, 710, 958)
        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer
        x1 = 60
        y1 = 180
        x2 = 770
        y2 = 180
        ev.Graphics.DrawLine(newpen, 60, 180, 770, 180)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 239
        y2 = y2 + 239
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 239
        y2 = y2 + 239
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 240
        y2 = y2 + 240
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 80
        y2 = y2 + 80
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
     

        ev.Graphics.DrawLine(newpen, 136, 140, 136, 300)
        ev.Graphics.DrawLine(newpen, 465, 140, 465, 300)
        ev.Graphics.DrawLine(newpen, 541, 140, 541, 300)

        ev.Graphics.DrawString("建设单位", printfont1, Brushes.Black, 71, 151)
        ev.Graphics.DrawString("建设项目", printfont1, Brushes.Black, 71, 192)
        ev.Graphics.DrawString("建筑面积", printfont1, Brushes.Black, 71, 232)
        ev.Graphics.DrawString("总 投 资", printfont1, Brushes.Black, 71, 271)
        ev.Graphics.DrawString("市住建局(城市配套费):", printfont1, Brushes.Black, 71, 314)
        ev.Graphics.DrawString("市统筹站(劳保统筹费):", printfont1, Brushes.Black, 71, 553)
        ev.Graphics.DrawString("市规划局(定桩费):", printfont1, Brushes.Black, 71, 792)
        ev.Graphics.DrawString("主管领导审查结果:", printfont1, Brushes.Black, 71, 1032)


        ev.Graphics.DrawString(建设单位TextBox2.Text, printfont1, Brushes.Black, 140, 152)
        ev.Graphics.DrawString(J新建TextBox.Text, printfont1, Brushes.Black, 140, 192)
        ev.Graphics.DrawString(面积TextBox.Text & 面积标注ComboBox2.Text, printfont1, Brushes.Black, 140, 232)
        ev.Graphics.DrawString(TextBox45.Text, printfont1, Brushes.Black, 140, 271)

        ev.Graphics.DrawString("建设位置", printfont1, Brushes.Black, 474, 151)
        ev.Graphics.DrawString("结    构", printfont1, Brushes.Black, 474, 192)
        ev.Graphics.DrawString("层    数", printfont1, Brushes.Black, 474, 232)
        ev.Graphics.DrawString("定 桩 费", printfont1, Brushes.Black, 474, 271)

        ev.Graphics.DrawString(建设位置ComboBox1.Text, printfont1, Brushes.Black, 546, 152)
        ev.Graphics.DrawString(J结构ComboBox.Text, printfont1, Brushes.Black, 546, 192)
        ev.Graphics.DrawString(层数TextBox1.Text, printfont1, Brushes.Black, 546, 232)
        ev.Graphics.DrawString(TextBox46.Text, printfont1, Brushes.Black, 546, 271)

        x1 = Nothing
        y1 = Nothing
        x2 = Nothing
        y2 = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub
    Private Sub printgraphicshenpi(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim dfk As New Pen(Color.Black, 2)
        Dim newpen As New Pen(Color.Black)
        ev.Graphics.DrawString("阿图什市规划局建设项目选址意见审批表", New Font("宋体", 18, FontStyle.Bold), Brushes.Black, 177, 99)
        ev.Graphics.DrawRectangle(dfk, 60, 140, 710, 958)
        Dim x1 As Integer
        Dim y1 As Integer
        Dim x2 As Integer
        Dim y2 As Integer
        x1 = 60
        y1 = 180
        x2 = 770
        y2 = 180
        ev.Graphics.DrawLine(newpen, 60, 180, 770, 180)
        y1 = y1 + 40
        y2 = y2 + 40
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 146
        y2 = y2 + 146
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)
        y1 = y1 + 148
        y2 = y2 + 148
        ev.Graphics.DrawLine(newpen, x1, y1, x2, y2)

        ev.Graphics.DrawLine(newpen, 136, 140, 136, 220)
        ev.Graphics.DrawLine(newpen, 465, 140, 462, 220)
        ev.Graphics.DrawLine(newpen, 541, 140, 541, 220)

     
        ev.Graphics.DrawString("申请单位", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 70, 152)
        ev.Graphics.DrawString("建设项目", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 70, 191)
        ev.Graphics.DrawString("建设位置", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 476, 152)
        ev.Graphics.DrawString("建筑面积", New Font("宋体", 10, FontStyle.Bold), Brushes.Black, 476, 191)
        ev.Graphics.DrawString(TextBox30.Text, printfont1, Brushes.Black, 141, 152)
        ev.Graphics.DrawString(TextBox49.Text, printfont1, Brushes.Black, 141, 191)
        ev.Graphics.DrawString(ComboBox7.Text, printfont1, Brushes.Black, 547, 152)
        ev.Graphics.DrawString(TextBox51.Text, printfont1, Brushes.Black, 547, 191)
        ev.Graphics.DrawString("经办人意见:", printfont1, Brushes.Black, 66, 225)
        ev.Graphics.DrawString("经办人:" & TextBox48.Text & "    日期:" & DateTimePicker5.Text, printfont1, Brushes.Black, 480, 349)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 495)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 641)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 787)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 933)
        ev.Graphics.DrawString("签名:              " & "日期:", printfont1, Brushes.Black, 476, 1081)


        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(70, 246, 690, 130)
        Dim sizemeasure As New SizeF(690, 130 - printfont.GetHeight(ev.Graphics))
        strformat.Trimming = StringTrimming.Word
        ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
        stringfopage = stringtoprint.Substring(0, numchars)
        ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
        x1 = Nothing
        y1 = Nothing
        x2 = Nothing
        y2 = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub
    Private Sub printgraphicxcxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 995
        y = 110
        ev.Graphics.DrawString(TextBox44.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 455, 274)
        ev.Graphics.DrawString(TextBox39.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString(TextBox41.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString(ComboBox6.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString(TextBox40.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 50
        ev.Graphics.DrawString(TextBox38.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        ev.Graphics.DrawString("阿图什市规划局", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)

        ev.Graphics.DrawString(DateTimePicker4.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)

        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(820, 325, 530, 237)
        Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics))
        strformat.Trimming = StringTrimming.Word
        ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
        stringfopage = stringtoprint.Substring(0, numchars)
        ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
        x = Nothing
        y = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub
    Private Sub printgraphicYDxukezheng(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
        Dim x As Integer
        Dim y As Integer
        x = 990
        y = 120
        ev.Graphics.DrawString(TextBox12.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 450, 274)
        ev.Graphics.DrawString(建设单位TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D新划TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D位置在TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D该用地TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(D总面积TextBox.Text & 用地面积标注ComboBox1.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        ev.Graphics.DrawString(TextBox6.Text & 建设规模标注ComboBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        y = y + 45
        x = x - 35
        ev.Graphics.DrawString("建设用地蓝线图:" & 建设用地红线图TextBox.Text, New Font("宋体", 11, FontStyle.Regular), Brushes.Black, x, y)
        ev.Graphics.DrawString("阿图什市规划局", New Font("宋体", 16, FontStyle.Regular), Brushes.Black, 405, 700)

        ev.Graphics.DrawString(DateTimePicker1.Text, New Font("宋体", 13, FontStyle.Regular), Brushes.Black, 419, 753)


        Dim numchars As Integer
        Dim numlines As Integer
        Dim stringfopage As String
        Dim strformat As New StringFormat
        Dim rectdraw As New RectangleF(820, 420, 530, 237)
        Dim sizemeasure As New SizeF(543, 180 - printfont.GetHeight(ev.Graphics))
        strformat.Trimming = StringTrimming.Word
        ev.Graphics.MeasureString(stringtoprint, printfont, sizemeasure, strformat, numchars, numlines)
        stringfopage = stringtoprint.Substring(0, numchars)
        ev.Graphics.DrawString(stringfopage, printfont, Brushes.Black, rectdraw, strformat)
        x = Nothing
        y = Nothing
        ev.Graphics.Dispose()
        ev.HasMorePages = False

    End Sub


程序代码:
    Private Sub Button20_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox4.Text
            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            If PrintDialog1.ShowDialog = DialogResult.OK Then

                PageSetupDialog1.PageSettings.Landscape = True
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicYDxukezheng

                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

程序代码:
    Private Sub Button38_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button38.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox5.Text
            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                PageSetupDialog1.PageSettings.Landscape = True
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicxcxukezheng
                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

程序代码:
  Private Sub Button42_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button42.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings
            stringtoprint = RichTextBox6.Text
            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                PageSetupDialog1.PageSettings.Landscape = False
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphicshenpi

                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

       
    End Sub

程序代码:
Private Sub Button43_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button43.Click
        Try

            PrintDocument1.DefaultPageSettings = printpagesettings

            PrintDialog1.Document = PrintDocument1
            PageSetupDialog1.PageSettings = printpagesettings
            PrintDialog1.PrinterSettings.Copies = 1 ''打印的份数
            PageSetupDialog1.PageSettings.Landscape = False
            If PrintDialog1.ShowDialog = DialogResult.OK Then
                If PageSetupDialog1.ShowDialog = DialogResult.OK Then
                    AddHandler PrintDocument1.PrintPage, AddressOf Me.printgraphictongzhidan
                    PrintDocument1.Print()
                    PrintDocument1.Dispose()
                End If
            End If
        Catch ex As Exception
            MessageBox.Show("打印出现错误", ex.ToString())
        End Try

    End Sub

这是部分代码,我看这足够了。这些代码全部在一个窗口里
2013-05-30 19:28
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
要是设置纸张大小怎么做?
2013-05-30 19:30
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
我是一个窗口里用TabControl1控件有5个TabPage页面,所以代码多、乱
2013-05-30 19:35
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
楼主所说的打印乱,是不是指打印出来的结果布局上零乱?
Private printpagesettings As New PageSettings'这个对象可以定义为窗体公共变量,但不要New
然后在每一个Button的Click事件中写上:printpagesettings = New PageSettings'在10楼给过你例子
在New之后,可以对printpagesettings对象的左右边距等进行设置,
例如:
printpagesettings = New PageSettings '对打印纸张进行设置就是这个对象。记得你上个贴子问过。
'然后,你可以通过printpagesettings对象设置打印的各种属性
printpagesettings.Margins.Left = 50 '以百分之一英寸为单位,设置页面的左边距
PrintDocument1.DefaultPageSettings = printpagesettings
stringtoprint = RichTextBox5.Text



===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 13:16
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
试过了,还不行,我想问题出在ev.Graphics上,加上ev.Graphics.Dispose()他就出现错误(第二次打印时),不加上就乱打印(第一次打印的和第二次打印的覆盖打印)。真是头痛
2013-05-31 13:49
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
仔细研究代码的逻辑关系。一步一步来。测试时先把try catch注释掉,查明错误的原因。

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 14:26
smy727
Rank: 2
等 级:论坛游民
帖 子:95
专家分:20
注 册:2012-1-20
得分:0 
试过一切办法了问题还是存在,我看就是ev.Graphics解放引起的问题,也说不定是多线程问题,不知道怎么解决,还是求高人指点。谢谢!
2013-05-31 20:25
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
你指的乱是AddHandler调用自定义函数出现混乱,还是在打印输出后,页面布局混乱?
我感觉应当是后者吧?

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 20:28
不说也罢
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:39
帖 子:1481
专家分:4989
注 册:2007-10-7
得分:0 
TRY模块注释掉后,每出现错误都会抛出一个错误提示及纠正意见的哦。错误出现在哪一行?
任何人想帮到你,必须能重现你的错误才能共同研究解决办法的

===================================================
讨厌C#的行尾的小尾巴;和一对大括号{ }
===================================================
2013-05-31 20:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-412490-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.221255 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved