You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET Windows窗体如何打印特定区域?账单应用开发求助

嘿,作为VB.NET新手刚接触打印功能确实有点懵,不过针对你的账单应用场景,我给你整理了两种实用的实现方式,都能帮你搞定点击打印按钮只打指定区域的需求:

方案1:用PrintDocument手动控制打印内容(最灵活)

这个方法是VB.NET里打印的标准玩法,能让你完全掌控打印的格式和内容,非常适合账单这种需要规整格式的场景。

步骤很简单:

  1. 先在你的窗体上拖两个控件:PrintDocument和PrintDialog(都在工具箱的「打印」分类里)
  2. 给打印按钮的Click事件写触发代码:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    ' 弹出打印对话框让用户选择打印机
    If PrintDialog1.ShowDialog() = DialogResult.OK Then
        PrintDocument1.Print()
    End If
End Sub
  1. 重点是处理PrintDocument的PrintPage事件——这里就是你定义要打印什么的地方。比如你要打印ListView里的账单数据,还可以加上客户信息标题:
Private Sub PrintDocument1_PrintPage(sender As Object, e As Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
    ' 先设置好要用的字体和画笔
    Dim titleFont As New Font("微软雅黑", 14, FontStyle.Bold)
    Dim headerFont As New Font("微软雅黑", 11, FontStyle.Bold)
    Dim contentFont As New Font("微软雅黑", 10)
    Dim linePen As New Pen(Color.Black, 1)
    
    ' 初始化打印的起始位置(单位是百分之一英寸)
    Dim currentY As Integer = 60
    Dim currentX As Integer = 30
    
    ' 先打印账单标题
    e.Graphics.DrawString("客户采购账单", titleFont, Brushes.DarkBlue, currentX, currentY)
    currentY += 40 ' 往下挪一行的位置
    
    ' 打印ListView的列标题
    For Each col As ColumnHeader In ListView1.Columns
        e.Graphics.DrawString(col.Text, headerFont, Brushes.Black, currentX, currentY)
        currentX += col.Width ' 按列宽偏移X坐标
    Next
    currentY += 25
    currentX = 30
    
    ' 打印每一行的商品数据
    For Each item As ListViewItem In ListView1.Items
        For Each subItem As ListViewItem.ListViewSubItem In item.SubItems
            e.Graphics.DrawString(subItem.Text, contentFont, Brushes.Black, currentX, currentY)
            currentX += subItem.Width
        Next
        currentX = 30
        currentY += 20
    Next
    
    ' 如果是要打印窗体上的某个指定区域(比如包含ListView和客户信息的Panel)
    ' 可以用DrawToBitmap把控件转成图片再打印,相当于"截图打印"
    ' Dim areaBmp As New Bitmap(pnlBillArea.Width, pnlBillArea.Height)
    ' pnlBillArea.DrawToBitmap(areaBmp, New Rectangle(0, 0, pnlBillArea.Width, pnlBillArea.Height))
    ' e.Graphics.DrawImage(areaBmp, currentX, currentY)
End Sub

这个方法的好处是,你想打印什么、怎么排版都能自己定——比如只打印ListView里已选中的行,或者调整某列的字体颜色,都很方便。

方案2:用PrintForm快速打印指定控件(最简单)

如果你不想手动写绘制逻辑,追求快速实现,可以用PrintForm控件,它能直接打印整个窗体或者某个控件的区域。

步骤:

  1. 先添加引用:右键项目→添加引用→找到Microsoft.VisualBasic.PowerPacks.Printing.PrintForm勾选上
  2. 在窗体上拖一个PrintForm控件
  3. 打印按钮的代码就非常简洁了:
Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
    ' 设置只打印指定控件的客户区域(就是控件里的内容部分)
    PrintForm1.Print(ListView1, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
    ' 如果要打印包含ListView的整个面板,就改成:
    ' PrintForm1.Print(pnlBillArea, PowerPacks.Printing.PrintForm.PrintOption.ClientAreaOnly)
End Sub

这个方法适合快速原型,缺点是格式调整的空间不大,比如不能自定义标题或者调整行间距。

给新手的小提示

  • 打印前先预览!可以加个预览按钮,用PrintPreviewDialog控件,关联上你的PrintDocument,这样能避免浪费纸张:
Private Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click
    Dim previewDlg As New PrintPreviewDialog()
    previewDlg.Document = PrintDocument1
    previewDlg.ShowDialog()
End Sub
  • 如果账单数据很多,需要分页的话,在PrintPage事件里记得设置e.HasMorePages = True,然后处理分页逻辑(比如记录已经打印了多少行,剩下的下一页打印)。
  • 记得处理异常,比如打印机没连接的情况,可以套个Try-Catch块防止程序崩溃。

内容的提问来源于stack exchange,提问作者R.D

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:52:06