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
|
Option Explicit On
Imports System
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Runtime.InteropServices
Imports System.Drawing.Printing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Namespace RichTextBoxPrintCtrl
Public Class RichTextBoxPrintCtrl
Inherits RichTextBox
' Convert the unit that is used by the .NET framework (1/100 inch)
' and the unit that is used by Win32 API calls (twips 1/1440 inch)
Private Const AnInch As Double = 14.4
Const MaximumTextLength As Integer = 10000
Const TextLengthToBeRemoved As Integer = 3000
Const MaximumNoOfControl As Integer = 50
Const NoOfControlToBeRemoved As Integer = 20
Private KeepShort As Boolean = True
<StructLayout(LayoutKind.Sequential)> _
Private Structure RECT
Public Left As Integer
Public Top As Integer
Public Right As Integer
Public Bottom As Integer
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure CHARRANGE
Public cpMin As Integer ' First character of range (0 for start of doc)
Public cpMax As Integer ' Last character of range (-1 for end of doc)
End Structure
<StructLayout(LayoutKind.Sequential)> _
Private Structure FORMATRANGE
Public hdc As IntPtr ' Actual DC to draw on
Public hdcTarget As IntPtr ' Target DC for determining text formatting
Public rc As Rect ' Region of the DC to draw to (in twips)
Public rcPage As Rect ' Region of the whole DC (page size) (in twips)
Public chrg As CHARRANGE ' Range of text to draw (see above declaration)
End Structure
Private Const WM_USER As Integer = &H400
Private Const EM_FORMATRANGE As Integer = WM_USER + 57
<DllImport("user32.dll")> _
Private Shared Function SendMessage(hwnd As IntPtr, wMsg As Int32, wParam As Int32, ByRef pt As Point) As Integer
End Function
Const EM_GETSCROLLPOS As Integer = &H400 + 221
Private ControlList As New List(Of MetaInfo)()
'Contains the initial position data of a control relative to the text content.
Friend Class MetaInfo
Private m_charIndex As Integer
Public Property CharIndex() As Integer
Get
Return m_charIndex
End Get
Set(value As Integer)
m_charIndex = value
End Set
End Property
Private m_deltaY As Integer
Public Property DeltaY() As Integer
Get
Return m_deltaY
End Get
Set(value As Integer)
m_deltaY = value
End Set
End Property
Private m_theControl As Control
Public Property TheControl() As Control
Get
Return m_theControl
End Get
Set(value As Control)
m_theControl = value
End Set
End Property
Public Sub New(theControl As Control)
Me.m_theControl = theControl
End Sub
End Class
Public Sub AddControl(oneControl As Control)
' Obtain the initial metadata.
Dim one As New MetaInfo(oneControl)
MyBase.Controls.Add(oneControl)
one.CharIndex = Me.TextLength
one.TheControl.Location = Me.GetPositionFromCharIndex(one.CharIndex)
one.DeltaY = Me.GetPositionFromCharIndex(0).Y - one.TheControl.Location.Y
ControlList.Add(one)
'"Push" the text away from the space occupied by the control.
Do
Me.AppendText(Environment.NewLine)
Loop While Me.GetPositionFromCharIndex(Me.TextLength).Y < (oneControl.Location.Y + oneControl.Height)
RemoveSome()
AutoScroll()
End Sub
Public Sub AutoScroll()
Me.SelectionStart = Me.TextLength - 1
Me.ScrollToCaret()
End Sub
Private Sub RemoveSome()
'Optional.
'Remove some text and control if too many, to release system resources and improve performance.
If Not KeepShort Then
Return
End If
Dim texttoRemove As Integer = 0
Dim imgtoRemove As Integer = 0
Try
If Me.TextLength > MaximumTextLength Then
texttoRemove = TextLengthToBeRemoved
Me.Text = Me.Text.Substring(texttoRemove)
texttoRemove += Me.Text.IndexOf(vbLf)
If texttoRemove > TextLengthToBeRemoved Then
Me.Text = Me.Text.Substring(texttoRemove - TextLengthToBeRemoved)
End If
For Each oldone As MetaInfo In ControlList
If oldone.CharIndex < texttoRemove Then
imgtoRemove += 1
Else
oldone.CharIndex -= texttoRemove
End If
Next
For i As Integer = 0 To imgtoRemove - 1
Me.Controls(0).Dispose()
ControlList.RemoveAt(0)
Next
'need to calculate the metadata again.
CalculateDelta()
End If
Catch ex As Exception
Throw ex
End Try
Try
If ControlList.Count > MaximumNoOfControl Then
imgtoRemove = NoOfControlToBeRemoved
For i As Integer = 0 To imgtoRemove - 1
texttoRemove = ControlList(0).CharIndex
ControlList.RemoveAt(0)
Me.Controls(0).Dispose()
Next
Me.Text = Me.Text.Substring(texttoRemove)
For Each oldone As MetaInfo In ControlList
oldone.CharIndex -= texttoRemove
Next
'need to calculate the metadata again.
CalculateDelta()
End If
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub TRichTextBox_VScroll(sender As Object, e As EventArgs)
Dim pt As New Point()
SendMessage(Me.Handle, EM_GETSCROLLPOS, 0, pt)
For Each one As MetaInfo In ControlList
one.TheControl.Location = New Point(one.TheControl.Location.X, -pt.Y - one.DeltaY)
Next
End Sub
Private Sub TRichTextBox_SizeChanged(sender As Object, e As EventArgs)
CalculateDelta()
End Sub
Private Sub CalculateDelta()
For Each one As MetaInfo In ControlList
one.TheControl.Location = Me.GetPositionFromCharIndex(one.CharIndex)
one.DeltaY = Me.GetPositionFromCharIndex(0).Y - one.TheControl.Location.Y
Next
End Sub
Private Sub TRichTextBox_LinkClicked(sender As Object, e As LinkClickedEventArgs)
System.Diagnostics.Process.Start(e.LinkText)
End Sub
Private Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wp As IntPtr, ByVal lp As IntPtr) As IntPtr
' Render the contents of the RichTextBox for printing
' Return the last character printed + 1 (printing start from this point for next page)
Public Function Print(ByVal charFrom As Integer, ByVal charTo As Integer, ByVal e As PrintPageEventArgs) As Integer
' Mark starting and ending character
Dim cRange As CHARRANGE
cRange.cpMin = charFrom
cRange.cpMax = charTo
' Calculate the area to render and print
Dim rectToPrint As RECT
rectToPrint.Top = e.MarginBounds.Top * AnInch
rectToPrint.Bottom = e.MarginBounds.Bottom * AnInch
rectToPrint.Left = e.MarginBounds.Left * AnInch
rectToPrint.Right = e.MarginBounds.Right * AnInch
' Calculate the size of the page
Dim rectPage As RECT
rectPage.Top = e.PageBounds.Top * AnInch
rectPage.Bottom = e.PageBounds.Bottom * AnInch
rectPage.Left = e.PageBounds.Left * AnInch
rectPage.Right = e.PageBounds.Right * AnInch
Dim hdc As IntPtr = e.Graphics.GetHdc()
Dim fmtRange As FORMATRANGE
fmtRange.chrg = cRange ' Indicate character from to character to
fmtRange.hdc = hdc ' Use the same DC for measuring and rendering
fmtRange.hdcTarget = hdc ' Point at printer hDC
fmtRange.rc = rectToPrint ' Indicate the area on page to print
fmtRange.rcPage = rectPage ' Indicate whole size of page
Dim res As IntPtr = IntPtr.Zero
Dim wparam As IntPtr = IntPtr.Zero
wparam = New IntPtr(1)
' Move the pointer to the FORMATRANGE structure in memory
Dim lparam As IntPtr = IntPtr.Zero
lparam = Marshal.AllocCoTaskMem(Marshal.SizeOf(fmtRange))
Marshal.StructureToPtr(fmtRange, lparam, False)
' Send the rendered data for printing
res = SendMessage(Handle, EM_FORMATRANGE, wparam, lparam)
' Free the block of memory allocated
Marshal.FreeCoTaskMem(lparam)
' Release the device context handle obtained by a previous call
e.Graphics.ReleaseHdc(hdc)
' Return last + 1 character printer
Return res.ToInt32()
End Function
End Class
Class Utility
'Bonus. A simple check on the GIF files because some may contain "bad" data and crash the program.
Public Shared Function isImageCorrupted(img As Image) As Boolean
Dim itis As Boolean = False
Try
If Not ImageAnimator.CanAnimate(img) Then
Return itis
End If
Dim frames As Integer = img.GetFrameCount(System.Drawing.Imaging.FrameDimension.Time)
If frames <= 1 Then
Return itis
End If
Dim times As Byte() = img.GetPropertyItem(&H5100).Value
Dim frame As Integer = 0
While True
Dim dur As Integer = BitConverter.ToInt32(times, 4 * frame)
If System.Threading.Interlocked.Increment(frame) >= frames Then
Exit While
End If
img.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Time, frame)
End While
Catch ex As Exception
Throw ex
End Try
Return itis
End Function
End Class
End Namespace |
Partager