vb.net 教程 11-1 打印组件 补充资料

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

我把打印里面自己容易迷糊的地方在这里做个记录:

1、Printing组件和PageSetupDialog组件之间的关联

(1)Printing组件使用PageSetupDialog中设置的参数:

        Dim psettings As Printing.PageSettings
        If PageSetupDialog1.ShowDialog = DialogResult.OK Then
            psettings = PageSetupDialog1.PageSettings
        Else
            Exit Sub
        End If
        PrintDocument1.DefaultPageSettings = psettings

(2)PageSetupDialog组件使用Printing中设置的参数(谢谢网友 keepee  的指正):1

PageSetupDialog1.Document = PrintDocument1  

2、打印的时候使页边距有效:

PrintDocument. OriginAtMargins属性:指示与页关联的图形对象的位置是位于用户指定边距内,还是位于该页可打印区域的左上角。如果图形原点起始于页边距,则为 true;如果图形原点位于可打印页的左上角,则为 false。 默认值为 false。
也就是说,如果我们要让设置的页边距真实有效,那么应该将OriginAtMargins设置为True。

 PrintPage打印代码同 vb.net 教程 11-1 打印组件 4 PrintDocument 3

    '打印页面
    Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim g As Graphics = e.Graphics
        If i = 0 Then
            g.DrawRectangle(New Pen(New SolidBrush(Color.Red), 2), New Rectangle(10, 10, 100, 60))
            g.DrawString("vb.net教程 http://blog.csdn.net/uruseibest", New Font("宋体", 20), New SolidBrush(Color.Blue), New Point(10, 80))
            g.DrawString("这是一个打印的例子:第1页", New Font("宋体", 20), New SolidBrush(Color.Blue), New Point(10, 120))
            g.DrawImage(PictureBox1.Image, New Rectangle(10, 160, 600, 400), New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height), GraphicsUnit.Pixel)
            i = i + 1
            e.HasMorePages = True
        Else
            g.DrawRectangle(New Pen(New SolidBrush(Color.Red), 2), New Rectangle(10, 10, 100, 60))
            g.DrawString("vb.net教程 http://blog.csdn.net/uruseibest", New Font("宋体", 20), New SolidBrush(Color.Blue), New Point(10, 80))
            g.DrawString("这是一个打印的例子:第2页", New Font("宋体", 20), New SolidBrush(Color.Blue), New Point(10, 120))
            g.DrawImage(PictureBox1.Image, New Rectangle(10, 160, 600, 400), New Rectangle(0, 0, PictureBox1.Image.Width, PictureBox1.Image.Height), GraphicsUnit.Pixel)
            e.HasMorePages = False
        End If
    End Sub

调用代码如下:

    '按下打印按钮
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim ps As New Printing.PaperSize()
        ps.PaperName = "我的自定义纸张"
        ps.Height = 600
        ps.Width = 400
        PrintDocument1.PrinterSettings.PrintToFile = True
        PrintDocument1.PrinterSettings.PrintFileName = "d:\dd.xps"

        PrintDocument1.DefaultPageSettings.PaperSize = ps

        Dim psettings As Printing.PageSettings
        If PageSetupDialog1.ShowDialog = DialogResult.OK Then
            psettings = PageSetupDialog1.PageSettings
        Else
            Exit Sub
        End If
        PrintDocument1.OriginAtMargins = True       '设置页边距是否有效。也就是相对页边距还是打印纸张位置绘图。
        PrintDocument1.DefaultPageSettings = psettings
        PrintDocument1.Print()
    End Sub

当弹出打印对话框时,大家可以设置不同的页边距进行测试。

可以将 PrintDocument1.OriginAtMargins = True 去掉再进行对比。

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值