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
|
'- ajoute pour tester l'exemple:
'- un bouton ApercuImp
'- un TableLayoutPanel1 sur le WinForm
'- PrintDocument
'- PrintPreviewDialog
Imports System.Drawing.Printing
Public Class frmPrintTableLayoutPanel
Public Sub New()
' Cet appel est requis par le Concepteur Windows Form.
InitializeComponent()
' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
btnApercuImp.Dock = DockStyle.Top
' Un simple control TableLayoutPanel que j'ai rempli avec d'autres controls
' Dock en fill et positionne en dessous du bouton
Me.TableLayoutPanel1.Dock = DockStyle.Fill
'Maximize la taille du form
Me.WindowState = FormWindowState.Maximized
End Sub
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim MyGr As Graphics = e.Graphics
'Met PageUnit à Display (apercu conforme à sortie printer)
MyGr.PageUnit = GraphicsUnit.Display
MyGr.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
MyGr.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
'Envoie TableLayoutPanel dans un bitmap avec Control.DrawToBitmap
Dim memoryImagePanel = New Bitmap(Me.TableLayoutPanel1.Bounds.Width, Me.TableLayoutPanel1.Bounds.Height, MyGr)
Me.TableLayoutPanel1.DrawToBitmap(memoryImagePanel, Me.TableLayoutPanel1.Bounds)
'code .net managed qui recopie l'image en gardant les proportions
'rect source =>memoryImagePanel
Dim RectSource As RectangleF = New Rectangle(0, 0, memoryImagePanel.Width, memoryImagePanel.Height)
'definit les "marges" diaboliques
Dim myMargin As Margins = New Margins(20, 20, 20, 20)
'les assignes à PrintDocument1
PrintDocument1.DefaultPageSettings.Margins = myMargin
'rect destination c'est notre format de page choisi par user (A4,A3 etc...)
'il se trouve dans e.MarginBounds que nous avons personnalise cette fois
Dim RectDest As RectangleF = e.MarginBounds
'surcharge de DrawImage qui recopie l'image en gardant les proportions
MyGr.DrawImage(memoryImagePanel, RectDest, RectSource, GraphicsUnit.Pixel)
End Sub
Private Sub btnApercuImp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnApercuImp.Click
PrintPreviewDialog1.Document = PrintDocument1
PrintPreviewDialog1.ShowDialog()
End Sub
End Class |
Partager