Impression sous VB.Net

morcelet Messages postés 18 Date d'inscription lundi 5 avril 2004 Statut Membre Dernière intervention 24 janvier 2008 - 3 mai 2004 à 11:19
morcelet Messages postés 18 Date d'inscription lundi 5 avril 2004 Statut Membre Dernière intervention 24 janvier 2008 - 3 mai 2004 à 14:07
Bonjour tout le monde j'aimerais savoir comment on fait pour imprimer une form sous VB.NET. J'y arrive sous VB mais sous VB.NET ca a l'air plus compliqué.
merci d'avance...

3 réponses

cs_labout Messages postés 1356 Date d'inscription samedi 8 décembre 2001 Statut Membre Dernière intervention 23 octobre 2006 8
3 mai 2004 à 13:39
labout
Que veux tu :
Imprimer la form comme une copie d'écran

Dans ce cas voici le code

Private Sub ImprimeEcran()
Dim FrmTemp As New Form
Dim Picturebox1 As PictureBox = New PictureBox
Dim Picturebox2 As PictureBox = New PictureBox
' Copie l'écran dans le presse papier
' Si vous trouvez mieux, merci de me le faire savoir

SendKeys.Send("%{PRTSC}")
SendKeys.Flush()
' le capture screen n'est pas top donc printscreen préférable
' Ajoute les propriétés à la Form
FrmTemp.Height = Me.Height + 50
FrmTemp.Width = Me.Width
FrmTemp.Text = "Fenêtre contenant l'image copiée"
' Comment charger une image par le code
' Positionne le picturebox
With Picturebox1
.Location = New System.Drawing.Point( _
.Location.X, _
.Location.Y)
.Height = FrmTemp.Height
.Width = FrmTemp.Width
.SizeMode = PictureBoxSizeMode.StretchImage
' colle le contenu du presse papier dans le PictureBox
.Image = CType(System.Windows.Forms.Clipboard.GetDataObject.GetData( _
System.Windows.Forms.DataFormats.Bitmap), Image)

'Picturebox2.Image = CType(CType(copyRect(Picturebox1, _
'New RectangleF(.Left, .Top + 50, .Width, (.Height - 10))), Bitmap).Clone, Bitmap)
pImage = .Image
End With
' Ajoute le controle PictureBox à la forme
' s'il y avait un autre contrôle nommé label1:
' FrmTemp.Controls.AddRange(New Control() {Picturebox1,label1})
FrmTemp.Controls.AddRange(New Control() {Picturebox1})
' transfert de l'image dans l'aperçu
AddHandler m_pd.PrintPage, AddressOf m_pd_PrintPage

' il est inutile de faire apparaitre la fenetre FrmTemp
'FrmTemp.Show()
Dim ppdlg As New PrintPreviewDialog
With ppdlg
' m_pd.Print() si l'in ne souhaite pas l'aperçu

.PrintPreviewControl.Document = m_pd
.WindowState = FormWindowState.Maximized
.ShowDialog()
.Dispose()
End With
' suppression de la fenetre FrmTemp et de son Objet
Picturebox1 = Nothing
FrmTemp = Nothing

' la ligne qui suit est indispensable car sinon il y aura création
' de 2 nouvelles pages et superposition des pages
' par exemple en passant de portrait à paysage
RemoveHandler m_pd.PrintPage, AddressOf m_pd_PrintPage
End Sub
Private Sub m_pd_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim sTtexte As String
Dim iPosPageX As Integer
Dim iPosPageY As Integer
Dim iwidth As Integer
Dim iRapport As Single
Dim myBrush As New SolidBrush(Color.White)

iRapport = Me.Width / Me.Height
' permet de placer en bas de page en portrait ou paysage
iPosPageX = CType(e.MarginBounds, System.Drawing.Rectangle).Width + 100
iPosPageY = CType(e.MarginBounds, System.Drawing.Rectangle).Height + 140

iwidth = e.PageSettings.PaperSize.Width - (e.MarginBounds.Left + (e.PageSettings.PaperSize.Width - e.MarginBounds.Right))
sTtexte = "Edité le " & Microsoft.VisualBasic.Today & vbCrLf & vbCrLf

Dim strText As String = sTtexte
Dim fntFont As New Font("Arial", 12)
Dim fntFont1 As New Font("Arial", 12, FontStyle.Bold)
' Envoie l'image dans le PrintPreview
e.Graphics.DrawString( _
strText, _
fntFont, _
New SolidBrush(Color.Black), _
CSng(e.MarginBounds.Left _
), _
CSng(e.MarginBounds.Top))

sTtexte = "Titre: " & Text5.Text

' c'est ici que l'image est imprimée les valeurs suffisent pour tout écran
' x y w h
e.Graphics.DrawImage(pImage, e.MarginBounds.Left, e.MarginBounds.Top + 50, iwidth, CInt(iwidth / iRapport))
' j'efface le haut de la fenêtre et le meun
' et je trace le cadre du haut
Dim rect As New RectangleF(e.MarginBounds.Left, e.MarginBounds.Top + 47, iwidth, 36)
Dim pen1 As New Pen(SystemColors.ActiveCaption, 2)
e.Graphics.FillRectangle(myBrush, rect)
e.Graphics.DrawLine(pen1, e.MarginBounds.Left, e.MarginBounds.Top + 84, iwidth + e.MarginBounds.Left, e.MarginBounds.Top + 84)
' tracé du titre
e.Graphics.DrawString( _
sTtexte, _
fntFont1, _
New SolidBrush(Color.Black), _
CSng(e.MarginBounds.Left - 5 _
), _
CSng(e.MarginBounds.Top + 50))
' écriture de la pagination
e.Graphics.DrawString( _
"Page 1 ", _
fntFont, _
New SolidBrush(Color.Black), _
CSng(iPosPageX _
), _
CSng(iPosPageY)) ' bas
' 725 est la marge droite

End Sub

inclure
Imports System.IO.File
Imports System.IO
Imports System.Drawing.Drawing2D
Imports System.Text.Encoding
Imports System.Drawing.Font
Imports Crazyht.Windows.Forms
Imports System.Drawing
0
cs_labout Messages postés 1356 Date d'inscription samedi 8 décembre 2001 Statut Membre Dernière intervention 23 octobre 2006 8
3 mai 2004 à 13:41
labout

J'ai oublié dans les déclarations en tête de classe
Private m_pd As New Printing.PrintDocument
Private pImage As Image
0
morcelet Messages postés 18 Date d'inscription lundi 5 avril 2004 Statut Membre Dernière intervention 24 janvier 2008
3 mai 2004 à 14:07
En fait ce que je voudrais c'est l'équivalent du printform(en VB) en VB.NET...
0
Rejoignez-nous