⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.vb

📁 vb 应用实例 简单应用 ddddddddddddddddddddddddddddddddd
💻 VB
字号:
Imports System.Drawing.Printing

'See CommonDialogs for using CommonDialogs PrintDialog1, PrintPreviewDialog1, PageSetupDialog1
Public Class Form1
    Inherits System.Windows.Forms.Form


#Region " Windows Form 设计工具产生的程序代码 "

    Public Sub New()
        MyBase.New()

        '此呼叫为 Windows Form 设计工具的必要项。
        InitializeComponent()

        '在 InitializeComponent() 呼叫之后加入所有的初始设定

    End Sub

    'Form 覆写 Dispose 以清除组件清单。
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    '为 Windows Form 设计工具的必要项
    Private components As System.ComponentModel.IContainer

    '注意: 以下为 Windows Form 设计工具所需的程序
    '您可以使用 Windows Form 设计工具进行修改。
    '请勿使用程序代码编辑器来修改这些程序。
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox()
        Me.Button1 = New System.Windows.Forms.Button()
        Me.Button2 = New System.Windows.Forms.Button()
        Me.Button3 = New System.Windows.Forms.Button()
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(16, 56)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(280, 192)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "Accept now, love now, enjoy now. I am nothing, and everything. I am nowwhere, and" & _
        " everywhere. 我原来的家在天堂, 我来这个世界是来玩的, 来看看的, 来体验的, 来休息的. "
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(208, 16)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(88, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "预览打印"
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(112, 16)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(88, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "设定打印格式"
        '
        'Button3
        '
        Me.Button3.Location = New System.Drawing.Point(16, 16)
        Me.Button3.Name = "Button3"
        Me.Button3.Size = New System.Drawing.Size(88, 24)
        Me.Button3.TabIndex = 3
        Me.Button3.Text = "打印"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 15)
        Me.ClientSize = New System.Drawing.Size(312, 266)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button3, Me.Button2, Me.Button1, Me.TextBox1})
        Me.Name = "Form1"
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "Print"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private WithEvents pdoc1 As New PrintDocument()

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        '设定打印格式

        '若由工具箱拖拉 PageSetupDialog1 则不需要下行
        Dim PageSetupDialog1 As New PageSetupDialog()

        PageSetupDialog1.Document = pdoc1
        PageSetupDialog1.PageSettings = pdoc1.DefaultPageSettings

        If PageSetupDialog1.ShowDialog = DialogResult.OK Then
            pdoc1.DefaultPageSettings = PageSetupDialog1.PageSettings
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        '打印

        '若由工具箱拖拉 PrintDialog1 则不需要下行
        Dim PrintDialog1 As New PrintDialog()

        PrintDialog1.Document = pdoc1

        If PrintDialog1.ShowDialog = DialogResult.OK Then
            Try
                pdoc1.Print()
            Catch e1 As Exception
                MessageBox.Show(e1.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '预览打印

        '若由工具箱拖拉 PrintPreviewDialog1 则不需要下行
        Dim PrintPreviewDialog1 As New PrintPreviewDialog()
        Try
            PrintPreviewDialog1.Document = pdoc1
            PrintPreviewDialog1.ShowDialog()
        Catch e1 As Exception
            MessageBox.Show(e1.ToString, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    '打印每一页时将触发 PrintPage 事件(event), 以可使用 BeginPrint, EndPrint 事件
    Private Sub pdoc1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles pdoc1.PrintPage

        '指到待打印的字
        Static intCurrentChar As Int32

        '设定打印字型
        Dim font1 As New Font("新细明体", 12)

        Dim intPrintAreaHeight, intPrintAreaWidth, marginLeft, marginTop As Int32
        With pdoc1.DefaultPageSettings
            '设定打印区域
            intPrintAreaHeight = .PaperSize.Height - .Margins.Top - .Margins.Bottom
            intPrintAreaWidth = .PaperSize.Width - .Margins.Left - .Margins.Right

            '设定左上角坐标
            marginLeft = .Margins.Left ' X 坐标
            marginTop = .Margins.Top ' Y 坐标
        End With

        '若为横印 (Landscape), 则长宽对调
        If pdoc1.DefaultPageSettings.Landscape Then
            Dim intTemp As Int32
            intTemp = intPrintAreaHeight
            intPrintAreaHeight = intPrintAreaWidth
            intPrintAreaWidth = intTemp
        End If

        '根据打印区域的高度与打印字型的高度, 计算每页可印行数, 每页可印行数 = 打印区域的高度 / 打印字型的高度
        Dim intLineCount As Int32 = CInt(intPrintAreaHeight / font1.Height)

        '定义打印区域的长方型
        Dim rectPrintingArea As New RectangleF(marginLeft, marginTop, intPrintAreaWidth, intPrintAreaHeight)

        'StringFormat class 定义文字排版(layout)的信息, 譬如对齐, 行距等
        Dim fmt As New StringFormat(StringFormatFlags.LineLimit)

        '呼叫 MeasureString 以决定长方型打印区域的字数
        Dim intLinesFilled, intCharsFitted As Int32
        e.Graphics.MeasureString(Mid(TextBox1.Text, intCurrentChar + 1), font1, _
                    New SizeF(intPrintAreaWidth, intPrintAreaHeight), fmt, _
                    intCharsFitted, intLinesFilled)

        '打印文字
        e.Graphics.DrawString(Mid(TextBox1.Text, intCurrentChar + 1), font1, Brushes.Black, rectPrintingArea, fmt)

        '指到下一个字
        intCurrentChar += intCharsFitted

        '是否触发下一页的 PrintPage event
        If intCurrentChar < TextBox1.Text.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            '下一页重新计数
            intCurrentChar = 0
        End If
    End Sub
End Class

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -