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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| Public Class ClassImpression
Inherits PrintDocument
Private buffer As Bitmap
Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc As IntPtr, ByVal X As Integer, _
ByVal Y As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, ByVal xSrc As Integer, ByVal ySrc As Integer, _
ByVal nSrcWidth As Integer, ByVal nSrcHeight As Integer, ByVal dwRop As Integer) As Integer
Public Sub SetDocument(ByVal c As Control)
Dim controlGraphics As Graphics = c.CreateGraphics ' graphique form
Const SRCCOPY As Integer = &HCC0020
'*** DEFINITION DE L'IMAGE
Dim controlSize As Size
controlSize = c.ClientSize()
buffer = New Bitmap(controlSize.Width, controlSize.Height)
Dim bufferGraphics As Graphics = controlGraphics.FromImage(buffer)
Dim bufferHdc As IntPtr = bufferGraphics.GetHdc 'hdc de la form
Dim controlHdc As IntPtr = controlGraphics.GetHdc
'*** DESSINE LE FORMULAIRE DANS LA PICTURE
StretchBlt(bufferHdc, 0, 0, controlSize.Width, controlSize.Height, _
controlHdc, 0, 0, controlSize.Width, controlSize.Height, SRCCOPY)
bufferGraphics.ReleaseHdc(bufferHdc)
controlGraphics.ReleaseHdc(controlHdc)
End Sub
Public Function Imprime(Optional ByVal dialog As Boolean = False, Optional ByVal preview As Boolean = False) As Long
Imprime = 0
If Not (Me.PrinterSettings.InstalledPrinters.Count > 0) Then
Imprime = -1
Exit Function
End If
If dialog Then
Dim printDialog As New PrintDialog()
printDialog.Document = Me
If printDialog.ShowDialog <> DialogResult.OK Then
Exit Function
End If
End If
If preview Then
Dim printPreview As New PrintPreviewDialog()
printPreview.Document = Me
printPreview.ShowDialog()
Else
Me.Print()
End If
End Function
Protected Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
e.Graphics.DrawImage(buffer, 0, 0)
e.HasMorePages = False
End Sub
End Class |