Impression d'une datagridview et un label dans la meme page

Signaler
Messages postés
144
Date d'inscription
dimanche 24 avril 2011
Statut
Membre
Dernière intervention
16 juin 2016
-
Messages postés
144
Date d'inscription
dimanche 24 avril 2011
Statut
Membre
Dernière intervention
16 juin 2016
-
Bonsoir tout le monde,
depuis quelques jours je séche et j'ai pas trouvé de solution à mon problème, meme avec plusieurs recherches dans les forums et sur le net, et voilà maintenant je fais appel aux experts de vbfrance pour m'aider.

voilà mon problème j'ai une classe qui est faite pour imprimer seulement une datagridview alors dans mon form j'ai mon datagridview et un label comme titre comme les datagridview en vb2008 n'ont pas de titre.

alors comment faire pour imprimer en meme temps la datagridview et le lable en une seule page.
merci bcp pour votre aide.



galaxy2009

2 réponses


Bonjour,

Si tu as le composant gratuit VBPowerPack, tu as le composant PrintForm qui permet d'imprimer une Form au complet. Mais,je ne sais pas si en cachant les autres contrôles avant d'imprimer (.visible = False) tu peux imprimer des contôles sélectivement.
Messages postés
144
Date d'inscription
dimanche 24 avril 2011
Statut
Membre
Dernière intervention
16 juin 2016

Bonjour,
merci pour votre réponse, mais je crois pas bien saisie votre réponse, en faite je cherche qu'elles lignes à ajouter à ma classe pour que je puisse inmprimer les deux en meme temps.

voilà ma classe :

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Text
Imports System.Drawing.Printing
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Namespace PrintControl

Partial Public Class ControlPrint
Inherits PrintDocument
Private m_ctrl As Control

''''
''''code original en c#
''''http://www.codeproject.com/KB/printing/ControlPrint.aspx
''''
''''
Private stretch As Boolean

''' <summary>
''' Set true to stretch the control to fill a single printed page
''' </summary>
Public Property StretchControl() As Boolean
Set(ByVal value As Boolean)
stretch = value
End Set
Get
Return stretch
End Get
End Property

Private width As Integer
Private height As Integer
''' <summary>
''' The width of the control to print. You can change this value if the automatically
''' calculated width does not fit all the elements of the control
''' </summary>
Public Property PrintWidth() As Integer
Set(ByVal value As Integer)
width = value
End Set
Get
Return width
End Get
End Property
''' <summary>
''' The height of the control to print. You can change this value if the automatically
''' calculated height does not fit all the elements of the control
''' </summary>
Public Property PrintHeight() As Integer
Set(ByVal value As Integer)
height = value
End Set
Get
Return height
End Get
End Property

Private inbetween As Integer
''' <summary>
''' The area to be reprinted between pages if there are more than one
''' pages to be printed, to prevent data loss
''' </summary>
Public Property RepeatArea() As Integer
Get
Return inbetween
End Get
Set(ByVal value As Integer)
inbetween = value
End Set
End Property

''' <summary>
''' Default constructor
''' </summary>
Public Sub New()
InitializeComponent()
m_ctrl = New Control()
stretch = False
width = m_ctrl.Width
height = m_ctrl.Height
inbetween = 10
End Sub

''' <summary>
''' Intialize the component with the selected control
''' </summary>
''' The control to printed


Public Sub New(ByVal print As Control)
InitializeComponent()
SetControl(print)
stretch = False
inbetween = 10
End Sub

''' <summary>
''' Initialize the component with the selected control specifying
''' whether to stretch or not
''' </summary>
''' The control to be printed


''' Set true to stretch the control to fill a single printed page


Public Sub New(ByVal print As Control, ByVal Str As Boolean)
InitializeComponent()
SetControl(print)
stretch = Str
inbetween = 10
End Sub

''' <summary>
''' Initialize the component with the selected control specifying
''' specific width and height
''' </summary>
''' The control to be printed


''' Printed width


''' Printed height


Public Sub New(ByVal print As Control, ByVal Width As Integer, ByVal Height As Integer)
InitializeComponent()
SetControl(print)
stretch = False
Me.width = Width
Me.height = Height
inbetween = 10
End Sub

''' <summary>
''' Intialize the component with a specific container, Insignificant
''' </summary>
'''


Private Sub New(ByVal container As IContainer)
container.Add(Me)
InitializeComponent()
m_ctrl = New Control()
stretch = False
inbetween = 10
End Sub

''' <summary>
''' Set the control to be printed
''' </summary>
''' The control you wish to print


Public Sub SetControl(ByVal print As Control)
m_ctrl = print
Dim NewSize As Size = m_ctrl.GetPreferredSize(Size.Empty)
width = NewSize.Width
height = NewSize.Height
printedheight = 0
End Sub

''' <summary>
''' Set the control with a specified height & width
''' </summary>
''' The control to be printed


''' Control's width


''' Control's height


Public Sub SetControl(ByVal print As Control, ByVal Width As Integer, ByVal Height As Integer)
m_ctrl = print
Me.width = Width
Me.height = Height
printedheight = 0
End Sub

Private printedheight As Integer
Private Sub ControlPrint_PrintPage(ByVal sender As Object, ByVal e As PrintPageEventArgs) Handles MyBase.PrintPage
'We get the old status of the control
Dim NewSize As New Size(width, height)
Dim OldDock As DockStyle = m_ctrl.Dock
Dim OldSize As New Size(m_ctrl.Width, m_ctrl.Height)
Dim parent As Control = m_ctrl
Dim Parents As List(Of Control) = New List(Of Control)()
Dim OldSizes As List(Of Size) = New List(Of Size)()

'enumerate the parents
Do While parent.Parent IsNot Nothing
Parents.Add(parent.Parent)
OldSizes.Add(parent.Parent.Size)
parent = parent.Parent
Loop
'Change the size of the control to fullt display it and get rid of scrollbars
m_ctrl.Dock = DockStyle.None
m_ctrl.Size = NewSize
'Make sure that the size changes otherwise resize the parents
Do While m_ctrl.Size = OldSize
For Each c As Control In Parents
c.Size = New Size(c.Width + 100, c.Height + 100)
Next c
m_ctrl.Size = NewSize
Loop
'print dimentions will be according to page size and margins
Dim printwidth As Integer = width
Dim printheight As Integer = height

'change the width to fit the paper, and change the height to maintain the ratio
If printwidth > e.MarginBounds.Width Then
printheight = CInt(Fix((CSng(e.MarginBounds.Width) / CSng(printwidth)) * printheight))
printwidth = e.MarginBounds.Width
End If

'if too long, we will need more papers
If printheight - printedheight > e.MarginBounds.Height AndAlso (Not stretch) Then
e.HasMorePages = True
Else
e.HasMorePages = False
End If


Dim gp As GraphicsUnit = GraphicsUnit.Point
Dim b As New Bitmap(width, height)
'Good, now we can draw
m_ctrl.DrawToBitmap(b, New Rectangle(0, 0, width, height))
'Will we stretch the image?
If stretch Then
e.Graphics.DrawImage(b, e.MarginBounds, b.GetBounds(gp), gp)
e.HasMorePages = False
Else
'If not stretched then make sure to print the area of the current page only
Dim ScaleF As Single = CSng(height) / CSng(printheight)
printheight -= printedheight
If printheight > e.MarginBounds.Height Then
printheight = e.MarginBounds.Height
End If
Dim rect As New Rectangle(0, CInt(Fix(printedheight * ScaleF)), b.Width, CInt(Fix(printheight * ScaleF)))
Dim b2 As Bitmap = b.Clone(rect, System.Drawing.Imaging.PixelFormat.DontCare)
e.Graphics.DrawImage(b2, New Rectangle((e.PageBounds.Width / 2) - (printwidth \ 2), e.MarginBounds.Top, printwidth, printheight))
End If

If e.HasMorePages Then
'Change the printed height, and don't forget the RepeatArea
printedheight += e.MarginBounds.Height - inbetween
Else
printedheight = 0
End If
'Restore the control's state
Dim i As Integer = 0
Do While i < Parents.Count
Parents(i).Size = OldSizes(i)
i += 1
Loop
m_ctrl.Size = New Size(OldSize.Width, OldSize.Height)
m_ctrl.Dock = OldDock
End Sub

''' <summary>
''' Draw the control fully to a bitmap & return it
''' </summary>
''' <returns></returns>
Public Function GetBitmap() As Bitmap
'Get old states
Dim NewSize As New Size(width, height)
Dim OldDock As DockStyle = m_ctrl.Dock
Dim OldSize As New Size(m_ctrl.Width, m_ctrl.Height)
Dim parent As Control = m_ctrl
Dim Parents As List(Of Control) = New List(Of Control)()
Dim OldSizes As List(Of Size) = New List(Of Size)()
Do While parent.Parent IsNot Nothing
Parents.Add(parent.Parent)
OldSizes.Add(parent.Parent.Size)
parent = parent.Parent
Loop
m_ctrl.Dock = DockStyle.None
m_ctrl.Size = NewSize
Do While m_ctrl.Size = OldSize
For Each c As Control In Parents
c.Size = New Size(c.Width + 100, c.Height + 100)
Next c
m_ctrl.Size = NewSize
Loop
Dim b As New Bitmap(width, height)
'Draw
m_ctrl.DrawToBitmap(b, New Rectangle(0, 0, width, height))
'Restore states
For i As Integer = 0 To Parents.Count - 1
Parents(i).Size = OldSizes(i)
Next i
m_ctrl.Size = New Size(OldSize.Width, OldSize.Height)
m_ctrl.Dock = OldDock
Return b
End Function

Private Nodes As List(Of TreeNode) ' All enumerated nodes are here
Private Sub EnumNodes(ByVal TN As TreeNode)
If (Not Nodes.Contains(TN)) AndAlso (TN.Parent Is Nothing OrElse (TN.Parent IsNot Nothing AndAlso TN.Parent.IsExpanded)) Then
Nodes.Add(TN)
End If
'Enum all subnodes of the current node
For Each R As TreeNode In TN.Nodes
EnumNodes(R)
Next R
End Sub

''' <summary>
''' Return the best size that fits the control
''' </summary>
''' <returns></returns>
Public Function CalculateSize() As Size
'Identify the control's type
If m_ctrl.GetType().AssemblyQualifiedName.IndexOf("TreeView") >= 0 Then
If (CType(m_ctrl, TreeView)).Nodes.Count = 0 Then
'if no elemts return 1X1 to avoid exceptions
Return New Size(1, 1)
Else
Dim TempW As Integer 0, TempH As Integer 0
Nodes = New List(Of TreeNode)()
For Each T As TreeNode In (CType(m_ctrl, TreeView)).Nodes
EnumNodes(T)
Next T
For Each N As TreeNode In Nodes
If TempW < N.Bounds.Right Then
TempW = N.Bounds.Right
End If
Next N
TempH = Nodes.Count * (CType(m_ctrl, TreeView)).ItemHeight
Return New Size(TempW + 30, TempH + 30)
End If
ElseIf m_ctrl.GetType().AssemblyQualifiedName.IndexOf("ListView") >= 0 Then
If (CType(m_ctrl, ListView)).Items.Count > 0 Then
'This will work if the listview is in details mode
CType(m_ctrl, ListView).Items(0).EnsureVisible()
Dim tempwidth As Integer = (CType(m_ctrl, ListView)).Items(0).Bounds.Width
Dim tempheight As Integer = (CType(m_ctrl, ListView)).Items((CType(m_ctrl, ListView)).Items.Count - 1).Bounds.Bottom
'This is for other modes
For Each i As ListViewItem In (CType(m_ctrl, ListView)).Items
If tempwidth < i.Bounds.Right Then
tempwidth = i.Bounds.Right
End If
If tempheight < i.Bounds.Bottom Then
tempheight = i.Bounds.Bottom
End If
Next i
Return New Size(tempwidth + 30, tempheight + 30)
Else
Return New Size(1, 1)
End If
ElseIf m_ctrl.Controls.Count > 0 Then
'Just in case the form didn't calculate its PreferredSize properly
Dim tempwidth As Integer = 1
Dim tempheight As Integer = 1
For Each c As Control In m_ctrl.Controls
If c.Bounds.Right > tempwidth Then
tempwidth = c.Bounds.Right
End If
If c.Bounds.Bottom > tempheight Then
tempheight = c.Bounds.Bottom
End If
Next c
Return New Size(tempwidth, tempheight)
Else
Return New Size(m_ctrl.PreferredSize.Width, m_ctrl.PreferredSize.Height)
End If
Return Size.Empty
End Function

''' <summary>
''' Apply the best size that fits the control
''' </summary>
Public Sub ApplyBestSize()
Dim temp As Size = CalculateSize()
width = temp.Width
height = temp.Height
End Sub
End Class
End Namespace


et dans le boutton imprimer :

Dim m_print As New ControlPrint(DataGridView1)
m_print.PrinterSettings = PageSetupDialog1.PrinterSettings
m_print.DefaultPageSettings = PageSetupDialog1.PageSettings
m_print.Print()

galaxy2009