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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
| Option Strict Off
Imports System.drawing
Imports System.drawing.Printing
Imports System.Windows.Forms
Public Class FormPrinting
Public TextBoxBoxed As Boolean = False ' boîte autour des textbox
Public TabControlBoxed As Boolean = True ' Boite autour des tabcontrols
Public LabelInBold As Boolean = True ' Print all labels in bold
Public Orientation As OrientationENum = OrientationENum.Automatic ' choix de l'orientation (Automatic, Protrait or Landscape)
Public PrintPreview As Boolean = True ' enabled Print preview instead of direct printing
Public Enum OrientationENum
Automatic = 1
Portrait = 2
Lanscape = 3
End Enum
Private _printFont As Font
Private _Pen As New Pen(Color.Black)
Private _Pen1 As New Pen(Color.Blue, 2)
Private _Brush As Brush
Private _f As System.Windows.forms.Form
Private _TextBoxLikeControl As New ArrayList
Private _yForm As Single
Private _xform As Single
Private tbBoxed As Boolean
Private _bBouton As Boolean
Public Sub New(ByVal f As System.Windows.forms.Form, ByVal tbBoxed As Boolean, ByVal bBouton As Boolean)
_f = f
AddTextBoxLikeControl("ComboBox")
AddTextBoxLikeControl("DateTimePicker")
AddTextBoxLikeControl("DateTimeSlicker")
tbBoxed = tbBoxed
TextBoxBoxed = tbBoxed
_bBouton = bBouton
End Sub
Public Sub AddTextBoxLikeControl(ByVal stringType As String)
_TextBoxLikeControl.Add(stringType)
End Sub
Public Sub Print()
Try
Dim pd As New PrintDocument
pd.DocumentName = _f.Text
' Calculate la position de la form pour impression
Select Case Orientation
Case OrientationENum.Automatic
If _f.Size.Width > (pd.DefaultPageSettings.Bounds.Width - pd.DefaultPageSettings.Margins.Right - pd.DefaultPageSettings.Margins.Left) Then
pd.DefaultPageSettings.Landscape() = True
End If
Case OrientationENum.Lanscape
pd.DefaultPageSettings.Landscape() = True
Case OrientationENum.Portrait
pd.DefaultPageSettings.Landscape() = False
End Select
_yForm = pd.DefaultPageSettings.Margins.Top
_xform = CInt((pd.DefaultPageSettings.Bounds.Width - _f.Size.Width) / 2)
AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage
If PrintPreview Then
Dim pp As New PrintPreviewDialog
pp.Document = pd
pp.WindowState = FormWindowState.Maximized
pp.ShowDialog()
Else
pd.Print()
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As PrintPageEventArgs)
_printFont = New Font(_f.Font.Name, CSng(_f.Font.Size * 1.2), FontStyle.Bold)
ev.Graphics.DrawString(_f.Text, _printFont, Brushes.Black, _xform, _yForm)
_yForm += _printFont.GetHeight(ev.Graphics)
_Pen = New Pen(Color.Black, 2)
_yForm += 1
Dim points As PointF() = {New PointF(_xform, _yForm), New PointF(_xform + _f.Size.Width, _yForm)}
Dim rect As New Rectangle(_xform, _yForm, _f.Size.Width, _f.Size.Height)
'ev.Graphics.DrawLines(_Pen, points)
ev.Graphics.FillRectangle(Brushes.WhiteSmoke, rect)
ev.Graphics.DrawRectangle(_Pen1, rect)
_yForm += _Pen.Width + 1
' Print chaque controle de la form
PrintControls(_f, ev, _xform, _yForm)
End Sub
Public Sub PrintControl(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
If c.Visible = True Then
PrintOneControl(c, ev, x, y) ' Myself
PrintControls(c, ev, x, y) 'Contained controls
End If
End Sub
Public Sub PrintControls(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
For Each cIn As Control In c.Controls
PrintControl(cIn, ev, x + cIn.Location.X, y + cIn.Location.Y)
Next
End Sub
Public Sub PrintOneControl(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single)
' Silver color est disable
If c.Enabled Then
_Pen = New Pen(Color.Black)
_Brush = Brushes.Black
Else
_Pen = New Pen(Color.Silver)
_Brush = Brushes.Silver
End If
Dim s As String = c.GetType.ToString
Dim founded As Boolean = False
' check si c'est un text box
If s.IndexOf("TextBox") >= 0 Then
Try
Dim tb As TextBox = c
founded = True
PrintText(c, ev, x, y, TextBoxBoxed, tb.Font.Bold, Not tb.Multiline And TextBoxBoxed, tb.TextAlign)
Catch ex As Exception
' N'est pas un TextBox
End Try
End If
If s.IndexOf("Button") >= 0 And _bBouton Then
Dim talign As Integer
Try
Dim tb As Button = c
founded = True
If tb.TextAlign = ContentAlignment.TopLeft Then
talign = 255
Else
talign = tb.TextAlign
End If
PrintText(c, ev, x, y, TextBoxBoxed, tb.Font.Bold, False, talign)
PrintCadre(c, ev, x, y, c.Width, c.Height)
Catch ex As Exception
' N'est pas un TextBox
End Try
End If
If s.IndexOf("ListBox") >= 0 Then
Try
Dim i As Integer
Dim tb As ListBox = c
Dim stringSize As New SizeF
founded = True
For i = 0 To tb.Items.Count
Dim sligne As String
stringSize = ev.Graphics.MeasureString(sligne, tb.font)
sligne = tb.Items.item(i)
y = y + stringSize.Height
PrintList(sligne, c, ev, x, y, tb.Font.Bold)
Next
Catch ex As Exception
' N'est pas un TextBox
End Try
End If
' check si ce n'est pas un textbox
If Not founded Then
For Each sType As String In _TextBoxLikeControl
If s.IndexOf(sType) >= 0 Then
PrintText(c, ev, x, y, TextBoxBoxed, False, TextBoxBoxed, HorizontalAlignment.Left)
founded = True
Exit For
End If
Next
End If
'Procede pour les autres contrôles
If Not founded Then
Select Case c.GetType.ToString
Case "System.Windows.Forms.Label"
Dim ha As HorizontalAlignment
Dim ss As String = c.Text
Dim ha2 As ContentAlignment = CType(c, Label).TextAlign
Select Case CType(c, Label).TextAlign
Case ContentAlignment.BottomLeft
ha = HorizontalAlignment.Left
Case ContentAlignment.TopLeft
ha = HorizontalAlignment.Left
Case ContentAlignment.MiddleLeft
ha = HorizontalAlignment.Left
Case ContentAlignment.BottomCenter
ha = HorizontalAlignment.Center
Case ContentAlignment.TopCenter
ha = HorizontalAlignment.Center
Case ContentAlignment.MiddleCenter
ha = HorizontalAlignment.Center
Case ContentAlignment.BottomRight
ha = HorizontalAlignment.Right
Case ContentAlignment.TopRight
ha = HorizontalAlignment.Right
Case ContentAlignment.MiddleRight
ha = HorizontalAlignment.Right
End Select
PrintText(c, ev, x, y, False, LabelInBold, False, ha)
Case "System.Windows.Forms.CheckBox"
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawRectangle(_Pen, x, y, w, w)
If CType(c, CheckBox).Checked Then
Dim d As Single = 3
Dim points1 As PointF() = {New PointF(x + d, y + d), New PointF(x + w - d, y + w - d)}
ev.Graphics.DrawLines(_Pen, points1)
Dim points2 As PointF() = {New PointF(x + w - d, y + d), New PointF(x + d, y + w - d)}
ev.Graphics.DrawLines(_Pen, points2)
End If
PrintText(c, ev, x + CInt((w * 1.4)), y - 2, False, False, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.RadioButton"
_printFont = New Font(c.Font.Name, c.Font.Size)
Dim w As Single = _printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawEllipse(_Pen, x, y, w, w)
If CType(c, RadioButton).Checked Then
Dim d As Single = 3
ev.Graphics.FillEllipse(_Brush, x + d, y + d, w - d - d, w - d - d)
End If
PrintText(c, ev, x + CInt((w * 1.4)), y - 2, False, False, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.Panel"
If CType(c, System.Windows.Forms.Panel).BorderStyle <> BorderStyle.None Then
If CType(c, System.Windows.Forms.Panel).BorderStyle = BorderStyle.Fixed3D Then
_Pen = New Pen(Color.Silver)
End If
ev.Graphics.DrawRectangle(_Pen, x, y, c.Width, c.Height)
End If
Case "System.Windows.Forms.GroupBox"
_Pen = New Pen(Color.Silver)
Dim w As Single = _printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawRectangle(_Pen, x, y + w - w, c.Width, c.Height - w + w)
PrintText(c, ev, x + w, y, False, True, False, HorizontalAlignment.Left)
Case "System.Windows.Forms.TabControl"
Dim tc As System.Windows.Forms.TabControl = c
_Pen = New Pen(Color.Gray)
If TabControlBoxed Then
ev.Graphics.DrawRectangle(_Pen, x, y, c.Width, c.Height)
End If
Dim points As PointF() = {New PointF(x, y + tc.ItemSize.Height), New PointF(x + tc.Width, y + tc.ItemSize.Height)}
ev.Graphics.DrawLines(_Pen, points)
Dim tp As System.Windows.forms.TabPage = tc.SelectedTab
'Nom du TabPage
_printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
ev.Graphics.DrawString(tp.Text, _printFont, Brushes.Black, x, y + (tc.ItemSize.Height - _printFont.GetHeight(ev.Graphics)) / 2)
Case "System.Windows.Forms.PictureBox"
Dim pic As PictureBox = c
ev.Graphics.DrawImage(pic.Image, x, y, c.Width, c.Height)
End Select
End If
End Sub
Public Sub PrintText(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single, ByVal tbBoxed As Boolean, ByVal inBold As Boolean, ByVal verticalCentering As Boolean, ByVal hAlignment As HorizontalAlignment)
Dim r As RectangleF
r.X = x
r.Y = y
r.Width = c.Width
r.Height = c.Height
'Box
If tbBoxed Then
ev.Graphics.DrawRectangle(_Pen, r.X, r.Y, r.Width, r.Height)
End If
'Text
If inBold Then
_printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
Else
_printFont = New Font(c.Font.Name, c.Font.Size)
End If
If verticalCentering Then
Dim fontHeight As Single = _printFont.GetHeight(ev.Graphics)
Dim deltaHeight As Single = (r.Height - fontHeight) / 2
r.Y += deltaHeight
Else
r.Y += 2
End If
Dim drawFormat As New StringFormat
Select Case hAlignment
Case HorizontalAlignment.Left
drawFormat.Alignment = StringAlignment.Near
Case HorizontalAlignment.Center
drawFormat.Alignment = StringAlignment.Center
Case HorizontalAlignment.Right
drawFormat.Alignment = StringAlignment.Far
Case 64
drawFormat.Alignment = StringAlignment.Far
r.Y = r.Y + CInt(r.Height / 2) - CInt(_printFont.Height / 2)
Case 32
drawFormat.Alignment = StringAlignment.Center
r.Y = r.Y + CInt(r.Height / 2) - CInt(_printFont.Height / 2)
Case 512
drawFormat.Alignment = StringAlignment.Center
r.Y = r.Y + CInt(r.Height / 2) - 2
Case 4 ' top right
drawFormat.Alignment = StringAlignment.Far
Case 1024 ' bottom right
drawFormat.Alignment = StringAlignment.Far
r.Y = r.Y + CInt(r.Height / 2) - CInt(_printFont.Height / 2)
Case 16 ' midle left
drawFormat.Alignment = StringAlignment.Near
r.Y = r.Y + 2
Case 256 ' middle left
drawFormat.Alignment = StringAlignment.Near
r.Y = r.Y + CInt(r.Height / 2) - 2
Case 255
drawFormat.Alignment = StringAlignment.Near
End Select
ev.Graphics.DrawString(c.Text, _printFont, _Brush, r, drawFormat)
End Sub
Public Sub PrintList(ByVal ctext As String, ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single, ByVal inBold As Boolean)
Dim r As RectangleF
r.X = x
r.Y = y
r.Width = c.Width
r.Height = c.Height
'Box
'Text
If inBold Then
_printFont = New Font(c.Font.Name, c.Font.Size, FontStyle.Bold)
Else
_printFont = New Font(c.Font.Name, c.Font.Size)
End If
Dim drawFormat As New StringFormat
drawFormat.Alignment = StringAlignment.Near
ev.Graphics.DrawString(ctext, _printFont, _Brush, r, drawFormat)
End Sub
Public Sub PrintCadre(ByVal c As System.Windows.forms.Control, ByVal ev As PrintPageEventArgs, ByVal x As Single, ByVal y As Single, ByVal w As Integer, ByVal h As Integer)
Dim r As RectangleF
r.X = x
r.Y = y
r.Width = w
r.Height = h
'Box
ev.Graphics.DrawRectangle(_Pen, r.X, r.Y, r.Width, r.Height)
End Sub
End Class |
Partager