Advertisement
coderail

Better RichTextBox - VB.NET

Jun 14th, 2013
1,365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. Imports System.Runtime.InteropServices
  2.  
  3. '------------------
  4. 'Creator: aeonhack
  5. 'Site: nimoru.com
  6. 'Created: 6/14/2013
  7. 'Changed: 6/14/2013
  8. 'Version: 1.0.0.0
  9. '------------------
  10. Public Class BetterRichTextBox
  11. Inherits RichTextBox
  12.  
  13. <DllImport("user32.dll")> _
  14. Private Shared Function SendMessage( _
  15. ByVal handle As IntPtr, _
  16. ByVal message As Integer, _
  17. ByVal wParam As Integer, _
  18. ByVal lParam As Integer) As IntPtr
  19. End Function
  20.  
  21. <DllImport("user32.dll", EntryPoint:="GetScrollInfo")> _
  22. Private Shared Function GetScrollInfo(
  23. ByVal handle As IntPtr, _
  24. ByVal bar As Integer, _
  25. ByRef info As SCROLLINFO) As Boolean
  26. End Function
  27.  
  28. <StructLayout(LayoutKind.Sequential)> _
  29. Private Structure SCROLLINFO
  30. Public size As UInteger
  31. Public mask As UInteger
  32. Public min As Integer
  33. Public max As Integer
  34. Public page As Integer
  35. Public position As Integer
  36. Public trackPosition As Integer
  37. End Structure
  38.  
  39. Private Scroll As SCROLLINFO
  40. Private Function ScrolledToBottom() As Boolean
  41. Scroll = New SCROLLINFO()
  42. Scroll.size = CUInt(Marshal.SizeOf(Scroll))
  43. Scroll.mask = 7
  44.  
  45. If Not GetScrollInfo(Handle, 1, Scroll) Then Return True
  46. Return (Scroll.page = 0) OrElse ((Scroll.page + Scroll.position) >= Scroll.max)
  47. End Function
  48.  
  49. Public Overloads Sub AppendText(ByVal color As Color, ByVal text As String)
  50. Dim Focus As Boolean = Focused
  51.  
  52. Dim SelectionStart As Integer = Me.SelectionStart
  53. Dim SelectionLength As Integer = Me.SelectionLength
  54.  
  55. Dim AutoScroll As Boolean = ScrolledToBottom()
  56.  
  57. If Not AutoScroll Then
  58. If Focus Then Parent.Focus()
  59. SendMessage(Handle, 1087, 1, 0) 'Hide selection, prevents auto-scrolling
  60. End If
  61.  
  62. Me.SelectionStart = TextLength
  63. Me.SelectionLength = 0
  64. Me.SelectionColor = color
  65.  
  66. Me.AppendText(text)
  67.  
  68. Me.SelectionColor = ForeColor
  69. Me.SelectionStart = SelectionStart
  70. Me.SelectionLength = SelectionLength
  71.  
  72. If AutoScroll Then
  73. SendMessage(Handle, 277, 7, 0) 'Reliably scroll to bottom
  74. Else
  75. SendMessage(Handle, 1087, 0, 0) 'Unhide selection
  76. If Focus Then Me.Focus()
  77. End If
  78. End Sub
  79.  
  80. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement