Kirjoittaja: tnb
Kirjoitettu: 19.10.2004 – 19.10.2004
Tagit: koodi näytille, vinkki
Printtaa formin kokonaisena, as seen on screen.
Kokoonpantu seuraavista lähteistä:
Thanks to metty: http://weblogs.asp.net/mschiffer/archive/2004/07/13/181111.aspx
Also thanks to: Matthew MacDonald
Projekti on muuten vaan sub main:sta alkava (moduli1)
Form1 nimi on siis PrintTest, button1 on cmdPrint
Moduliin
Module Module1
Public vormi As Form
Public Sub main()
vormi = New PrintTest
vormi.ShowDialog()
End Sub
End ModuleFormiin
Imports System.Drawing.Printing
Public Class PrintTest
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.
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
Friend WithEvents cmdPrint As System.Windows.Forms.Button
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'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()
Me.cmdPrint = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'cmdPrint
'
Me.cmdPrint.Location = New System.Drawing.Point(16, 16)
Me.cmdPrint.Name = "cmdPrint"
Me.cmdPrint.Size = New System.Drawing.Size(148, 24)
Me.cmdPrint.TabIndex = 0
Me.cmdPrint.Text = "Print"
'
'PrintTest
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14)
Me.ClientSize = New System.Drawing.Size(528, 342)
Me.Controls.Add(Me.cmdPrint)
Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name = "PrintTest"
Me.Text = "Print Status"
Me.ResumeLayout(False)
End Sub
#End Region
Public formImage As Bitmap
Private Const SRCCOPY As Integer = &HCC0020
Private Declare Function BitBlt _
Lib "gdi32.dll" ( _
ByVal hdcDest As IntPtr, _
ByVal x As Int32, _
ByVal y As Int32, _
ByVal Width As Int32, _
ByVal Height As Int32, _
ByVal hdcSrc As IntPtr, _
ByVal xSrc As Int32, _
ByVal ySrc As Int32, _
ByVal dwRop As Int32 _
) As Boolean
Private Sub cmdPrint_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdPrint.Click
' Create the document and attach an event handler.
Dim MyDoc As PrintDocument = New PrintDocument
AddHandler MyDoc.PrintPage, AddressOf MyDoc_PrintPage
' Allow the user to choose a printer and specify other settings.
Dim dlgSettings As New PrintDialog
dlgSettings.Document = MyDoc
Dim formGraphics As Graphics = Me.CreateGraphics
formImage = New Bitmap(Me.Width, Me.Height, formGraphics)
Dim memGraphics As Graphics = Graphics.FromImage(formImage)
' Get the target and source device context handles (hDC)
Dim sourceDC As IntPtr = formGraphics.GetHdc
Dim targetDC As IntPtr = memGraphics.GetHdc
' Consider the border width and the titlebar height
Dim widthDelta As Integer = (Me.Width - _
Me.ClientRectangle.Width)
Dim heightDelta As Integer = (Me.Height - _
Me.ClientRectangle.Height)
' Copy the form including its titlebar and borders
BitBlt(targetDC, _
0, 0, _
Me.ClientRectangle.Width + widthDelta, _
Me.ClientRectangle.Height + heightDelta, _
sourceDC, _
0 - widthDelta \ 2, 0 - (heightDelta - widthDelta \ 2), _
SRCCOPY)
' Release DCs and dispose objects
formGraphics.ReleaseHdc(sourceDC)
formGraphics.Dispose()
memGraphics.ReleaseHdc(targetDC)
memGraphics.Dispose()
' formImage now has the form's image. Print it before disposing it.
Dim Result As DialogResult = dlgSettings.ShowDialog()
' If the user clicked OK, print the document.
If Result = DialogResult.OK Then
' This method returns immediately, before the print job starts.
' The PrintPage event will fire asynchronously.
MyDoc.Print()
End If
' Finally dispose the image
formGraphics.Dispose()
End Sub
Private Sub MyDoc_PrintPage(ByVal sender As Object, _
ByVal e As PrintPageEventArgs)
' Define the font.
Dim MyFont As New Font("Arial", 30)
' Determine the position on the page.
' In this case, we read the margin settings
' (although there is nothing that prevents your code
' from going outside the margin bounds.)
Dim x As Single = e.MarginBounds.Left
Dim y As Single = e.MarginBounds.Top
' Determine the height of a line (based on the font used).
Dim LineHeight As Single = MyFont.GetHeight(e.Graphics)
' Draw an image.
e.Graphics.DrawImage(formimage, x, y)
End Sub
Private Sub PrintTest_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim br As Brush = Brushes.Blue
Dim ft As Font = Me.DefaultFont
vormi.CreateGraphics.DrawString("Hello", ft, br, 200, 200)
End Sub
End ClassVB6 Form1.PrintForm.. tulostaa formin sisällön :p
Voihan sitä olla useampiakin vaihtoehtoja.