1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
Imports System.Drawing.Printing
Imports System.Runtime.InteropServices
Public Class ClassImpression
Inherits PrintDocument
Public Sub SetDocument(ByVal feuille As Control)
Const SRCCOPY As Integer = &HCC0020 ' pour copier la source
Dim controlGraphics As Graphics = feuille.CreateGraphics ' graphique de la feuille à imprimer
Dim controlSize As Size = feuille.ClientSize ' taille de la forme à copier
Buffer = New Bitmap(controlSize.Width + 204, controlSize.Height + 312) ' buffer contenant la feuille à copier
Dim bufferGraphics As Graphics = Graphics.FromImage(Buffer)
Dim bufferHdc As IntPtr = bufferGraphics.GetHdc 'hdc de la form destinée à l'impression
Dim controlHdc As IntPtr = controlGraphics.GetHdc
'*** DESSINE LE FORMULAIRE DANS LA PICTURE EN LE CENTRANT
NativeMethods.StretchBlt(bufferHdc, 102, 156, controlSize.Width, controlSize.Height, controlHdc, 0, 0, controlSize.Width, controlSize.Height, SRCCOPY)
bufferGraphics.ReleaseHdc(bufferHdc)
controlGraphics.ReleaseHdc(controlHdc)
End Sub
Public Sub Imprimeforme()
Using printDialogue As New PrintDialog()
printDialogue.Document = Me
If printDialogue.ShowDialog = DialogResult.OK Then
printDialogue.PrinterSettings.DefaultPageSettings.PaperSize = printDialogue.PrinterSettings.PaperSizes.Item(6) 'dimensions pour A4
Me.Print()
End If
End Using
End Sub
Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
e.Graphics.DrawImage(buffer, 0, 0) ' impression proprement dite
e.HasMorePages = False ' document une seule page
End Sub
End Class |
Partager