📄 pagesettingsform.vb
字号:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Public Overrides Sub Dispose()
MyBase.Dispose()
If Not (components Is Nothing) Then
components.Dispose()
End If
End Sub
Private WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Private WithEvents PrintPreviewDialog1 As System.Windows.Forms.PrintPreviewDialog
Private WithEvents Button1 As System.Windows.Forms.Button
Private WithEvents PageSetupDialog1 As System.Windows.Forms.PageSetupDialog
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
Me.PrintPreviewDialog1 = New System.Windows.Forms.PrintPreviewDialog()
Me.Button1 = New System.Windows.Forms.Button()
Me.PageSetupDialog1 = New System.Windows.Forms.PageSetupDialog()
Me.SuspendLayout()
'
'PrintDocument1
'
'
'PrintPreviewDialog1
'
Me.PrintPreviewDialog1.AutoScrollMargin = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.AutoScrollMinSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.ClientSize = New System.Drawing.Size(400, 300)
Me.PrintPreviewDialog1.Enabled = True
Me.PrintPreviewDialog1.Location = New System.Drawing.Point(147, 17)
Me.PrintPreviewDialog1.MaximumSize = New System.Drawing.Size(0, 0)
Me.PrintPreviewDialog1.Name = "PrintPreviewDialog1"
Me.PrintPreviewDialog1.Opacity = 1
Me.PrintPreviewDialog1.TransparencyKey = System.Drawing.Color.Empty
Me.PrintPreviewDialog1.Visible = False
'
'Button1
'
Me.Button1.Font = New System.Drawing.Font("Comic Sans MS", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(161, Byte))
Me.Button1.Location = New System.Drawing.Point(24, 24)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(216, 56)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Preview and Print Page Settings"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(272, 109)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
Me.Name = "Form1"
Me.Text = "Printer Page Settings"
Me.ResumeLayout(False)
End Sub
#End Region
' This project will work as advertised only if the captions fit in the width of the corresponding
' side of the printable area of the page. If you set the top and bottom margins to 3 inches each, then
' the left and right captions won't be properly centered.
' You can edit the code so that, if the length of the left or right margin caption exceeds
' the height of the printable area of the page, it will center the two captions on the page, and not
' in the printable area of the page.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
PrintPreviewDialog1.Document = PrintDocument1
PageSetupDialog1.PageSettings = PrintDocument1.DefaultPageSettings
PageSetupDialog1.ShowDialog()
PrintDocument1.DefaultPageSettings = PageSetupDialog1.PageSettings
PrintPreviewDialog1.ShowDialog()
Catch exc As Exception
MsgBox("Printing Operation Failed" & vbCrLf & _
exc.Message)
End Try
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim R As Rectangle
e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
Dim strWidth, strHeight As Integer
Dim pFont As Font
pFont = New Font("Comic Sans MS", 20)
e.Graphics.DrawString("ORIGIN", pFont, Brushes.Black, 0, 0)
pFont = New Font("Comic Sans MS", 28)
' X and Y are the coordinates on the page, where each caption
' will be printed. Their values are calculated for each orientation
Dim X, Y As Integer
' The margin captions
Dim TMarginCaption As String = "Top Margin String"
Dim LMarginCaption As String = "Left Margin String"
Dim RMarginCaption As String = "Right Margin String"
Dim BMarginCaption As String = "Bottom Margin String"
' The four margins around the rectangle that encloses the printable are of the page
Dim LMargin, RMargin, TMargin, BMargin As Integer
With PrintDocument1.DefaultPageSettings.Margins
LMargin = .Left
RMargin = .Right
TMargin = .Top
BMargin = .Bottom
End With
' The dimensions of the printable area of the page (PrintWidth, PrintHeight)
' and the dimensions of the page (PageWidth, PageHeight)
Dim PrintWidth, PrintHeight As Integer
Dim PageWidth, PageHeight As Integer
With PrintDocument1.DefaultPageSettings.PaperSize
PrintWidth = .Width - LMargin - RMargin
PrintHeight = PrintDocument1.DefaultPageSettings.PaperSize.Height - TMargin - BMargin
PageWidth = PrintDocument1.DefaultPageSettings.PaperSize.Width
PageHeight = PrintDocument1.DefaultPageSettings.PaperSize.Height
End With
' If the user has chosen landscape orientations, we must swap the
' width and height of the useful are of the page
' The margins need not be swapped
If PrintDocument1.DefaultPageSettings.Landscape Then
PrintWidth = PrintDocument1.DefaultPageSettings.PaperSize.Height - TMargin - BMargin
PrintHeight = PrintDocument1.DefaultPageSettings.PaperSize.Width - RMargin - LMargin
PageWidth = PrintDocument1.DefaultPageSettings.PaperSize.Height
PageHeight = PrintDocument1.DefaultPageSettings.PaperSize.Width
End If
R = New Rectangle(LMargin, TMargin, PageWidth - LMargin - RMargin, PageHeight - BMargin - TMargin)
e.Graphics.DrawRectangle(Pens.Black, R)
' TOP MARGIN CAPTION
strWidth = e.Graphics.MeasureString(TMarginCaption, pFont).Width
strHeight = e.Graphics.MeasureString(TMarginCaption, pFont).Height
X = LMargin + (PrintWidth - strWidth) / 2
Y = (TMargin - strHeight) / 2
e.Graphics.TranslateTransform(X, Y)
e.Graphics.DrawString(TMarginCaption, pFont, Brushes.Black, 0, 0)
' RIGHT MARGIN CAPTION
strWidth = e.Graphics.MeasureString(RMarginCaption, pFont).Width
strHeight = e.Graphics.MeasureString(RMarginCaption, pFont).Height
X = PageWidth - (RMargin - strHeight) / 2
Y = TMargin + (PrintHeight - strWidth) / 2 ' when we rotate by 90, width and height are swapped
e.Graphics.ResetTransform()
e.Graphics.TranslateTransform(X, Y)
e.Graphics.RotateTransform(90)
e.Graphics.DrawString(RMarginCaption, pFont, Brushes.Black, 0, 0)
' BOTTOM MARGIN CAPTION
e.Graphics.ResetTransform()
strWidth = e.Graphics.MeasureString(BMarginCaption, pFont).Width
strHeight = e.Graphics.MeasureString(BMarginCaption, pFont).Height
X = PageWidth - RMargin - (PrintWidth - strWidth) / 2
Y = PageHeight - (BMargin - strHeight) / 2
e.Graphics.TranslateTransform(X, Y)
e.Graphics.RotateTransform(180)
e.Graphics.DrawString(BMarginCaption, pFont, Brushes.Black, 0, 0)
' LEFT MARGIN CAPTION
e.Graphics.ResetTransform()
strWidth = e.Graphics.MeasureString(LMarginCaption, pFont).Width
strHeight = e.Graphics.MeasureString(LMarginCaption, pFont).Height
X = (LMargin - strHeight) / 2
Y = TMargin + (PrintHeight + strWidth) / 2
e.Graphics.TranslateTransform(X, Y)
e.Graphics.RotateTransform(-90)
e.Graphics.DrawString(LMarginCaption, pFont, Brushes.Black, 0, 0)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -