23-03-17, 09:20 AM
الكود التالي يوضح شكل كلاس بسيط يمكن استخدامه لطباعة صورة الفورم و ذلك بإستخدام مقياس رسم يتناسب مع حجم ورقة الطباعة
الإستخدام
أضف الكلاس لمشروعك ثم استخدم الكود بالشكل التالي
أتمني أن يكون الكلاس مفيدا للبعض منكم
و يمكنكم أن تضعوا اي استفسار في حالة إن لم يكن الكود غير واضحا للبعض منكم
PHP كود :
Imports System.ComponentModel
Imports System.Drawing.Printing
Public Class PrintForm
Implements IDisposable
Private formtoPrint As Form
Private formBackcolor As Color
Private formImage As Bitmap
Private _printDocument As PrintDocument
Private _disposed As Boolean
Public Sub New(f As Form)
Me.New(f, Color.White)
End Sub
Private Sub New(f As Form, backColor As Color)
formtoPrint = f
formBackcolor = backColor
End Sub
<Description("Print Document"), Bindable(False), Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Property PrintDocument() As PrintDocument
Get
If Me._printDocument Is Nothing Then
Me._printDocument = New PrintDocument()
AddHandler Me._printDocument.PrintPage, New PrintPageEventHandler(AddressOf Me.pd_PrintPage)
End If
Return Me._printDocument
End Get
Set(value As PrintDocument)
Me._printDocument = value
End Set
End Property
Public Property BackColor As Color
Get
Return Me.formBackcolor
End Get
Set(value As Color)
Me.formBackcolor = value
End Set
End Property
Public ReadOnly Property Form As Form
Get
Return formtoPrint
End Get
End Property
Public ReadOnly Property Image As Bitmap
Get
If Me.formImage Is Nothing Then
Me.formImage = New Bitmap(Me.GetFormImage())
End If
Return Me.formImage
End Get
End Property
Friend Function CanInvokeForm(f As Form) As Boolean
Return f IsNot Nothing AndAlso Not f.IsDisposed AndAlso f.IsHandleCreated
End Function
Private Function GetFormImage() As Bitmap
Dim bmp As Bitmap = Nothing
If CanInvokeForm(Me.Form) Then
bmp = New Bitmap(Me.Form.Width, Me.Form.Height)
Dim bounds As Rectangle = Me.Form.Bounds
bounds.Offset(-bounds.X, -bounds.Y)
Me.Form.DrawToBitmap(bmp, bounds)
End If
Return bmp
End Function
Private Sub pd_PrintPage(sender As Object, e As PrintPageEventArgs)
Paint(e.Graphics, e.MarginBounds, 50, 0)
End Sub
Private Sub Paint(g As Graphics, bounds As Rectangle, marginX As Integer, marginY As Integer)
If Me.Image Is Nothing Then
Return
End If
Dim rect As Rectangle = New Rectangle(bounds.X, bounds.Y, bounds.Width, bounds.Height)
Dim ratio As Double = Math.Max(CDbl(Me.Image.Width) / rect.Width, CDbl(Me.Image.Height) / rect.Height)
Dim x As Single = CSng(rect.Width * ratio / 2 - Me.Image.Width \ 2)
Dim y As Single = CSng(rect.Height * ratio / 2 - Me.Image.Height \ 2)
Dim mx As New Drawing2D.Matrix(1.0F / CSng(ratio), 0, 0, 1.0F / CSng(ratio), 0, 0)
mx.Translate(x, y)
g.Transform = mx
g.DrawImageUnscaled(Me.Image, marginX, marginY)
End Sub
Public Sub Print(show As Boolean)
If show Then
Dim result As DialogResult = New PrintDialog() With {.Document = Me.PrintDocument}.ShowDialog()
If result <> DialogResult.OK AndAlso result <> DialogResult.Yes Then
Return
End If
End If
Me.PrintDocument.Print()
End Sub
Public Sub ShowPrintPreview()
Dim previewDialog As PrintPreviewDialog = New PrintPreviewDialog() With {.Document = Me.PrintDocument}
previewDialog.ShowDialog()
End Sub
Public Sub ShowPageSetup()
Dim setupDialog As PageSetupDialog = New PageSetupDialog() With {.Document = Me.PrintDocument}
setupDialog.ShowDialog()
End Sub
Protected Overridable Sub Dispose(disposing As Boolean)
If _disposed Then
Return
End If
If disposing Then
If Me.formImage IsNot Nothing Then
Me.formImage.Dispose()
Me.formImage = Nothing
End If
End If
_disposed = True
End Sub
' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources.
'Protected Overrides Sub Finalize()
' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above.
' Dispose(False)
' MyBase.Finalize()
'End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
' TODO: uncomment the following line if Finalize() is overridden above.
' GC.SuppressFinalize(Me)
End Sub
End Class
الإستخدام
أضف الكلاس لمشروعك ثم استخدم الكود بالشكل التالي
PHP كود :
Using pf As New PrintForm(Me)
pf.ShowPrintPreview()
pf.Print(False)
End Using
أتمني أن يكون الكلاس مفيدا للبعض منكم
و يمكنكم أن تضعوا اي استفسار في حالة إن لم يكن الكود غير واضحا للبعض منكم