You are on page 1of 4

Imports System.Windows.

Forms
Imports System.Runtime.InteropServices
''' <summary>
''' Represents a standard <see cref="RichTextBox"/> with some
''' minor added functionality.
''' </summary>
''' <remarks>
''' AdvRichTextBox provides methods to maintain performance
''' while it is being updated. Additional formatting features
''' have also been added.
''' </remarks>
Public Class AdvRichTextBox
Inherits RichTextBox
''' <summary>
''' Maintains performance while updating.
''' </summary>
''' <remarks>
''' <para>
''' It is recommended to call this method before doing
''' any major updates that you do not wish the user to
''' see. Remember to call EndUpdate when you are finished
''' with the update. Nested calls are supported.
''' </para>
''' <para>
''' Calling this method will prevent redrawing. It will
''' also setup the event mask of the underlying richedit
''' control so that no events are sent.
''' </para>
''' </remarks>
Public Sub BeginUpdate()
' Deal with nested calls.
updating += 1
If updating > 1 Then
Return
End If
' Prevent the control from raising any events.
oldEventMask = SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0
, 0)
' Prevent the control from redrawing itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 0, 0)
End Sub
''' <summary>
''' Resumes drawing and event handling.
''' </summary>
''' <remarks>
''' This method should be called every time a call is made
''' made to BeginUpdate. It resets the event mask to it's
''' original value and enables redrawing of the control.
''' </remarks>
Public Sub EndUpdate()
' Deal with nested calls.
updating -= 1
If updating > 0 Then
Return
End If
' Allow the control to redraw itself.
SendMessage(New HandleRef(Me, Handle), WM_SETREDRAW, 1, 0)
' Allow the control to raise event messages.
SendMessage(New HandleRef(Me, Handle), EM_SETEVENTMASK, 0, oldEventMask)
End Sub
''' <summary>
''' Gets or sets the alignment to apply to the current
''' selection or insertion point.
''' </summary>
''' <remarks>
''' Replaces the SelectionAlignment from
''' <see cref="RichTextBox"/>.
''' </remarks>
Public Shadows Property SelectionAlignment() As TextAlign
Get
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
' Get the alignment.
SendMessage(New HandleRef(Me, Handle), EM_GETPARAFORMAT, SCF_SELECTI
ON, fmt)
' Default to Left align.
If (fmt.dwMask And PFM_ALIGNMENT) = 0 Then
Return TextAlign.Left
End If
Return CType(fmt.wAlignment, TextAlign)
End Get
Set(value As TextAlign)
Dim fmt As New PARAFORMAT()
fmt.cbSize = Marshal.SizeOf(fmt)
fmt.dwMask = PFM_ALIGNMENT
fmt.wAlignment = CShort(value)
' Set the alignment.
SendMessage(New HandleRef(Me, Handle), EM_SETPARAFORMAT, SCF_SELECTI
ON, fmt)
End Set
End Property
''' <summary>
''' This member overrides
''' <see cref="Control"/>.OnHandleCreated.
''' </summary>
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
' Enable support for justification.
SendMessage(New HandleRef(Me, Handle), EM_SETTYPOGRAPHYOPTIONS, TO_ADVAN
CEDTYPOGRAPHY, TO_ADVANCEDTYPOGRAPHY)
End Sub
Private updating As Integer = 0
Private oldEventMask As Integer = 0
' Constants from the Platform SDK.
Private Const EM_SETEVENTMASK As Integer = 1073
Private Const EM_GETPARAFORMAT As Integer = 1085
Private Const EM_SETPARAFORMAT As Integer = 1095
Private Const EM_SETTYPOGRAPHYOPTIONS As Integer = 1226
Private Const WM_SETREDRAW As Integer = 11
Private Const TO_ADVANCEDTYPOGRAPHY As Integer = 1
Private Const PFM_ALIGNMENT As Integer = 8
Private Const SCF_SELECTION As Integer = 1
' It makes no difference if we use PARAFORMAT or
' PARAFORMAT2 here, so I have opted for PARAFORMAT2.
<StructLayout(LayoutKind.Sequential)> _
Private Structure PARAFORMAT
Public cbSize As Integer
Public dwMask As UInteger
Public wNumbering As Short
Public wReserved As Short
Public dxStartIndent As Integer
Public dxRightIndent As Integer
Public dxOffset As Integer
Public wAlignment As Short
Public cTabCount As Short
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
Public rgxTabs As Integer()
' PARAFORMAT2 from here onwards.
Public dySpaceBefore As Integer
Public dySpaceAfter As Integer
Public dyLineSpacing As Integer
Public sStyle As Short
Public bLineSpacingRule As Byte
Public bOutlineLevel As Byte
Public wShadingWeight As Short
Public wShadingStyle As Short
Public wNumberingStart As Short
Public wNumberingStyle As Short
Public wNumberingTab As Short
Public wBorderSpace As Short
Public wBorderWidth As Short
Public wBorders As Short
End Structure
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wPara
m As Integer, lParam As Integer) As Integer
End Function
<DllImport("user32", CharSet:=CharSet.Auto)> _
Private Shared Function SendMessage(hWnd As HandleRef, msg As Integer, wPara
m As Integer, ByRef lp As PARAFORMAT) As Integer
End Function
End Class
''' <summary>
''' Specifies how text in a <see cref="AdvRichTextBox"/> is
''' horizontally aligned.
''' </summary>
Public Enum TextAlign
''' <summary>
''' The text is aligned to the left.
''' </summary>
Left = 1
''' <summary>
''' The text is aligned to the right.
''' </summary>
Right = 2
''' <summary>
''' The text is aligned in the center.
''' </summary>
Center = 3
''' <summary>
''' The text is justified.
''' </summary>
Justify = 4
End Enum

You might also like