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 68 69
| Imports System.Drawing.Printing
Public Class ClassPrint
Private Declare Function StretchBlt Lib "gdi32.dll" Alias "BitBlt" (ByVal _
hdcDest As IntPtr, ByVal nXDest As Integer, ByVal nYDest As _
Integer, ByVal nWidth As Integer, ByVal nHeight As Integer, ByVal _
hdcSrc As IntPtr, ByVal nXSrc As Integer, ByVal nYSrc As Integer, _
ByVal dwRop As System.Int32) As Long
Private myimage As Bitmap
Private thectrl As Control
Public Function returnimage() As Bitmap
myimage = Capturectrl(thectrl)
Return myimage
End Function
Private Function Capturectrl(ByVal ctrl As Control) As Bitmap
Dim memoryImage As Bitmap
Dim mygraphics As Graphics = ctrl.CreateGraphics()
Dim s As Size = ctrl.Size
memoryImage = New Bitmap(s.Width, s.Height, mygraphics)
Dim memoryGraphics As Graphics = Graphics.FromImage(memoryImage)
Dim dc1 As IntPtr = mygraphics.GetHdc
Dim dc2 As IntPtr = memoryGraphics.GetHdc
StretchBlt(dc2, 0, 0, ctrl.ClientRectangle.Width, _
ctrl.ClientRectangle.Height, dc1, 0, 0, 13369376)
mygraphics.ReleaseHdc(dc1)
memoryGraphics.ReleaseHdc(dc2)
Return memoryImage
End Function
Public Sub printdoc()
Try
Dim ppd As New PrintPreviewDialog
'document à imprimer
Dim doc As New PrintDocument
'nom pour le gestionnaire d'imprimante
doc.DocumentName = "MaForm"
'abonnement à l'événement PrintPage
Capturectrl(thectrl)
AddHandler doc.PrintPage, AddressOf printform
'paramètres de page
Dim ps As New PageSettings
'ici en paysage pour l'exemple
ps.Landscape = True
doc.DefaultPageSettings = ps
'indique à la prévisualisation le document à montrer
ppd.Document = doc
'previsualisation plein écran
ppd.WindowState = FormWindowState.Maximized
'imprssion si ok
If ppd.ShowDialog = Windows.Forms.DialogResult.OK Then
ppd.Document.Print()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub printform(ByVal sender As Object, ByVal e As PrintPageEventArgs)
Try
e.Graphics.DrawImage(myimage, 0, 0)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Public Sub New(ByVal ctrl As Control)
thectrl = ctrl
End Sub
End Class |
Partager