VB.net e form.printform

martedì 02 marzo 2004 - 10.03

Mrevelli Profilo | Newbie

Ciao a tutti ,
sto aggiornando un app VB6 a VB.NET ...
qualcuno conosce l'equivalente di Form.PrintForm di VB6 in VB.net , o comunque se non esiste + questo metodo un workaround funzionale ?
Devo per forza usare una textbox ?
Grazie anticipatamente

Brainkiller Profilo | Guru

Non c'è un equivalente.
Ti posto però una soluzione fornita dal team di sviluppo di VB.NET:

There is no direct way to do this in VB .NET; however, you can use the
following workaround.

Workaround:

Create a new windows application using Visual Basic .NET.
Add one TextBox and change the Text to whatever you prefer for tesing
purpose.

Add the following Imports statement as the First line in the Form's class:

Imports System.Runtime.InteropServices


Then add two buttons on the form and One PrintDocument and one PrintDialog
component.
The button names are "PrintForm" and "printPreviewForm".

Now copy and paste the following code after windows desinger gerenerated
code section:


Private memImage As Bitmap
Private Declare Auto Function BitBlt Lib "gdi32.dll" (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 Integer) As Boolean
Private Sub print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PrintForm.Click

BuildFormImage()
PrintDialog1.Document = PrintDocument1
If PrintDialog1.ShowDialog = DialogResult.OK Then
PrintDocument1.Print()
End If
End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim formGraphics As Graphics = e.Graphics
If (memImage Is Nothing) Then BuildFormImage()
formGraphics.DrawImage(memImage, 0, 0)
memImage.Dispose()
memImage = Nothing
formGraphics.Dispose()
e.HasMorePages = False
End Sub

Private Sub BuildFormImage()
Dim graphicID As Graphics = Me.CreateGraphics
Dim sizeID As Size = Me.Size
Const SRCCOPY As Integer = &HCC0020

memImage = New Bitmap(sizeID.Width, sizeID.Height, graphicID)
Dim memGraphic As Graphics = graphicID.FromImage(memImage)
Dim deviceContext1 As IntPtr = graphicID.GetHdc
Dim deviceContext2 As IntPtr = memGraphic.GetHdc

BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
graphicID.ReleaseHdc(deviceContext1)
memGraphic.ReleaseHdc(deviceContext2)
End Sub


Private Sub printPreviewForm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printPreviewForm.Click
Dim PrintPreviewID As New PrintPreviewDialog()
PrintPreviewID.Document = PrintDocument1
BuildFormImage()
PrintPreviewID.ShowDialog()
End Sub
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5