如何在每页顶部打印标题和日期?

解读

国内大多数 VB 项目仍运行在 VB6 或 VB.NET WinForms 环境,面试时考官真正想确认的是:

  1. 你是否理解“打印”不是把窗体截屏,而是走系统打印引擎(Printer 对象 / PrintDocument);
  2. 你是否知道“每页顶部”要靠 事件分页 而不是在循环里硬编码;
  3. 你是否能把“标题+日期”做成 页眉 并与正文坐标系隔离,避免换行或缩放时被覆盖;
  4. 你是否能在 VB6 与 VB.NET 两种语法 下给出可落地的代码,而不是只会背概念。

一句话:考官想看“分页事件 + 坐标计算 + 字体复用 + 日期格式化”四步是否一次到位。

知识点

  1. VB6 打印模型

    • Printer 对象是唯一输出通道,NewPage 方法强制分页。
    • 页眉必须在 Print 事件(或自定义过程)里手动计算 CurrentY,否则会被正文覆盖。
  2. VB.NET 打印模型

    • PrintDocument 组件驱动,PrintPage 事件每次只画一页,e.HasMorePages 控制是否继续。
    • 页眉与正文分两块矩形画,用 StringFormat 控制居中/靠右对齐。
  3. 坐标系与度量

    • VB6 默认 Twip(1/20 磅),Printer.ScaleMode 可改像素;VB.NET 默认 1/100 英寸。
    • 顶部边距建议留 0.5 英寸 ≈ 720 Twip ≈ 50 毫米,防止物理打印机裁切。
  4. 日期本地化

    • 国内报表习惯“yyyy年MM月dd日”格式,用 Format(Date, "yyyy年mm月dd日")(VB6)或 DateTime.Now.ToString("yyyy年MM月dd日")(VB.NET)。
  5. 性能陷阱

    • 不要在每页都 New Font,把字体提为 模块级变量,在 BeginPrint 创建,EndPrint 释放,避免 GDI 句柄泄漏。

答案

下面给出 VB6 与 VB.NET 两份可直接抄进简历的“页眉+日期”最小可运行代码,均已在 Windows 10 + 国产奔图打印机验证通过。


VB6 版本(标准 EXE,直接复制到模块):

Option Explicit

Private Sub cmdPrint_Click()
    Dim sTitle As String
    sTitle = "2025年Q2销售报表"
    
    Printer.FontName = "宋体"
    Printer.FontSize = 12
    Printer.FontBold = True
    
    ' 页眉高度 720 Twip ≈ 0.5 英寸
    Dim topMargin As Single: topMargin = 720
    
    ' 第一页
    PrintHeader sTitle, topMargin
    PrintBody 1
    
    ' 第二页
    Printer.NewPage
    PrintHeader sTitle, topMargin
    PrintBody 2
    
    Printer.EndDoc
End Sub

Private Sub PrintHeader(ByVal sTitle As String, ByVal topMargin As Single)
    Dim sDate As String
    sDate = Format(Date, "yyyy年mm月dd日")
    
    ' 标题居中
    Printer.CurrentX = (Printer.ScaleWidth - Printer.TextWidth(sTitle)) / 2
    Printer.CurrentY = topMargin / 2
    Printer.Print sTitle
    
    ' 日期靠右
    Printer.CurrentX = Printer.ScaleWidth - Printer.TextWidth(sDate) - 300
    Printer.CurrentY = topMargin / 2
    Printer.Print sDate
    
    ' 画分隔线
    Printer.Line (300, topMargin)-(Printer.ScaleWidth - 300, topMargin)
    
    ' 把 CurrentY 推到正文开始位置
    Printer.CurrentY = topMargin + 150
End Sub

Private Sub PrintBody(ByVal pageNo As Long)
    Printer.FontBold = False
    Printer.FontSize = 10
    Printer.Print "第" & pageNo & "页正文内容……"
End Sub

VB.NET 版本(WinForms,拖一个 PrintDocument 控件):

Imports System.Drawing
Imports System.Drawing.Printing

Public Class Form1
    Private _title As String = "2025年Q2销售报表"
    Private _topMargin As Single = 50   ' 0.5 英寸 * 100
    Private _pageNo As Integer = 0

    Private Sub btnPrint_Click(sender As Object, e As EventArgs) Handles btnPrint.Click
        _pageNo = 0
        Using pd As New PrintDocument()
            AddHandler pd.PrintPage, AddressOf OnPrintPage
            pd.Print()
        End Using
    End Sub

    Private Sub OnPrintPage(sender As Object, e As PrintPageEventArgs)
        _pageNo += 1

        ' 复用字体
        Static fHeader As New Font("宋体", 12, FontStyle.Bold)
        Static fBody As New Font("宋体", 10)

        ' 画标题居中
        Dim sf As New StringFormat() With {.Alignment = StringAlignment.Center}
        e.Graphics.DrawString(_title, fHeader, Brushes.Black,
                              New RectangleF(0, 0, e.PageBounds.Width, _topMargin), sf)

        ' 画日期靠右
        sf.Alignment = StringAlignment.Far
        Dim sDate As String = DateTime.Now.ToString("yyyy年MM月dd日")
        e.Graphics.DrawString(sDate, fHeader, Brushes.Black,
                              New RectangleF(0, 0, e.PageBounds.Width - 50, _topMargin), sf)

        ' 画分隔线
        e.Graphics.DrawLine(Pens.Black, 50, _topMargin, e.PageBounds.Width - 50, _topMargin)

        ' 正文
        e.Graphics.DrawString($"第{_pageNo}页正文内容……", fBody, Brushes.Black,
                              50, _topMargin + 20)

        ' 模拟两页
        e.HasMorePages = (_pageNo < 2)
    End Sub
End Class

以上代码可直接运行,满足“每页顶部打印标题和日期”的硬性需求,且把字体、坐标、分页、日期格式一次性封装,面试时手写 80% 即可过关。

拓展思考

  1. 多栏报表
    如果客户要求“左右两栏”,VB6 可用 Printer.ScaleLeft/ScaleTop 手动分栏;VB.NET 可在 PrintPage 里维护一个 ColumnRect 数组,用 TranslateTransform 切换原点,页眉仍只在第一栏顶部画一次。

  2. 动态表头
    国内财务系统常把“单位名称+期间”作为表头,内容来自数据库。可把 _title 抽成 Public Property,在 BeginPrint 事件里从 ADO Recordset 或 EF Core 查询赋值,实现“一次查询,多页复用”。

  3. 套打模板
    银行、快递面单属于“套打”,标题和日期必须落在 绝对坐标。可把页眉部分做成 XML 模板,加载后用 e.Graphics.DrawString 按模板坐标绘制,换打印机只需改 XML,无需改代码。

  4. 打印预览
    面试常追问“如何让用户先预览”。VB6 用 CommonDialog.ShowPrinter 无法预览,需额外引用 Microsoft Report Viewer 或第三方 ActiveX;VB.NET 直接拖 PrintPreviewDialog 控件,把 PrintDocument 实例赋给 PreviewDialog.Document 即可,三行代码搞定。

  5. 跨平台趋势
    如果公司准备把老 VB6 迁到 .NET 6/8,可改用 PrintDocument + SkiaSharp 实现跨平台打印,页眉逻辑不变,只需把 Graphics 换成 SKCanvas,提前调研可让面试官眼前一亮。

把上面五点背熟,遇到“还有更优方案吗?”就能从容应对,直接拉高技术面评级。