Visual Basic 2010-RTB: como alterar o tipo de sublinhamento e sua cor?

Cambalinho

Power Member
com a RichTextBox, como posso alterar o tipo de sublinhado e sua cor?
eu consegui 1 codigo em API e consegui alterar o tipo de sublinhado, mas com a cor fica tudo baralhado. alguem me pode ajudar?
Código:
#Region "Interop-Defines"
  <StructLayout(LayoutKind.Sequential)> _
  Private Structure CHARFORMAT
  Public cbSize As Integer
  Public dwMask As UInteger
  Public dwEffects As UInteger
  Public yHeight As Integer
  Public yOffset As Integer
  Public crTextColor As Integer
  Public bCharSet As Byte
  Public bPitchAndFamily As Byte
  <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _
  Public szFaceName As Char()

  ' CHARFORMAT2 from here onwards.
  Public wWeight As Short
  Public sSpacing As Short
  Public crBackColor As Integer
  Public LCID As Integer
  Public dwReserved As UInteger
  Public sStyle As Short
  Public wKerning As Short
  Public bUnderlineType As Byte
  Public bAnimation As Byte
  Public bRevAuthor As Byte
  Public bUnderlineColor As Byte
  End Structure
<DllImport("user32", CharSet:=CharSet.Auto)> _
  Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lp As CHARFORMAT) As Integer
  End Function

  Private Const WM_USER As Integer = &H400
  Private Const EM_GETCHARFORMAT As Integer = WM_USER + 58
  Private Const EM_SETCHARFORMAT As Integer = WM_USER + 68
  Private Const SCF_SELECTION As Integer = &H1
  Private Const SCF_WORD As Integer = &H2
  Private Const SCF_ALL As Integer = &H4
  Private Const EM_GETPARAFORMAT As Integer = 1085
  Private Const EM_SETPARAFORMAT As Integer = 1095
  Private Const PFM_ALIGNMENT As Integer = 8

#Region "CHARFORMAT2 Flags"
  Private Const CFE_BOLD As UInt32 = &H1
  Private Const CFE_ITALIC As UInt32 = &H2
  Private Const CFE_UNDERLINE As UInt32 = &H4
  Private Const CFE_STRIKEOUT As UInt32 = &H8
  Private Const CFE_PROTECTED As UInt32 = &H10
  Private Const CFE_LINK As UInt32 = &H20
  Private Const CFE_AUTOCOLOR As UInt32 = &H40000000
  Private Const CFE_SUBSCRIPT As UInt32 = &H10000
  ' Superscript and subscript are
  Private Const CFE_SUPERSCRIPT As UInt32 = &H20000
  ' mutually exclusive
  Private Const CFM_SMALLCAPS As Integer = &H40
  ' (*)
  Private Const CFM_ALLCAPS As Integer = &H80
  ' Displayed by 3.0
  Private Const CFM_HIDDEN As Integer = &H100
  ' Hidden by 3.0
  Private Const CFM_OUTLINE As Integer = &H200
  ' (*)
  Private Const CFM_SHADOW As Integer = &H400
  ' (*)
  Private Const CFM_EMBOSS As Integer = &H800
  ' (*)
  Private Const CFM_IMPRINT As Integer = &H1000
  ' (*)
  Private Const CFM_DISABLED As Integer = &H2000
  Private Const CFM_REVISED As Integer = &H4000
  Private Const CFM_BACKCOLOR As Integer = &H4000000
  Private Const CFM_LCID As Integer = &H2000000
  Private Const CFM_UNDERLINETYPE As Integer = &H800000
  ' Many displayed by 3.0
  Private Const CFM_WEIGHT As Integer = &H400000
  Private Const CFM_SPACING As Integer = &H200000
  ' Displayed by 3.0
  Private Const CFM_KERNING As Integer = &H100000
  ' (*)
  Private Const CFM_STYLE As Integer = &H80000
  ' (*)
  Private Const CFM_ANIMATION As Integer = &H40000
  ' (*)
  Private Const CFM_REVAUTHOR As Integer = &H8000
  Private Const CFM_BOLD As UInt32 = &H1
  Private Const CFM_ITALIC As UInt32 = &H2
  Private Const CFM_UNDERLINE As UInt32 = &H4
  Private Const CFM_STRIKEOUT As UInt32 = &H8
  Private Const CFM_PROTECTED As UInt32 = &H10
  Private Const CFM_LINK As UInt32 = &H20
  Private Const CFM_SIZE As UInt32 = &H8000000
  Private Const CFM_COLOR As UInt32 = &H40000000
  Private Const CFM_FACE As UInt32 = &H20000000
  Private Const CFM_OFFSET As UInt32 = &H10000000
  Private Const CFM_CHARSET As UInt32 = &H8000000
  Private Const CFM_SUBSCRIPT As UInt32 = CFE_SUBSCRIPT Or CFE_SUPERSCRIPT
  Private Const CFM_SUPERSCRIPT As UInt32 = CFM_SUBSCRIPT
  Private Const CFU_UNDERLINENONE As Byte = &H0
  Private Const CFU_UNDERLINE As Byte = &H1
  Private Const CFU_UNDERLINEWORD As Byte = &H2
  ' (*) displayed as ordinary underline
  Private Const CFU_UNDERLINEDOUBLE As Byte = &H3
  ' (*) displayed as ordinary underline
  Private Const CFU_UNDERLINEDOTTED As Byte = &H4
  Private Const CFU_UNDERLINEDASH As Byte = &H5
  Private Const CFU_UNDERLINEDASHDOT As Byte = &H6
  Private Const CFU_UNDERLINEDASHDOTDOT As Byte = &H7
  Private Const CFU_UNDERLINEWAVE As Byte = &H8
  Private Const CFU_UNDERLINETHICK As Byte = &H9
  Private Const CFU_UNDERLINEHAIRLINE As Byte = &HA
  ' (*) displayed as ordinary underline
#End Region
#End Region
''' <summary>
  ''' Specifies the underline styles for a segment of rich text.
  ''' </summary>
  Public Enum UnderlineStyle
  ''' <summary>
  ''' No underlining.
  ''' </summary>
  None = 0

  ''' <summary>
  ''' Single-line solid underline.
  ''' </summary>
  Normal = 1

  ''' <summary>
  ''' Single-line underline broken between words.
  ''' </summary>
  Word = 2

  ''' <summary>
  ''' Double-line underline.
  ''' </summary>
  [Double] = 3

  ''' <summary>
  ''' 'Dotted' pattern underline.
  ''' </summary>
  Dotted = 4

  ''' <summary>
  ''' 'Dash' pattern underline.
  ''' </summary>
  Dash = 5

  ''' <summary>
  ''' 'Dash-dot' pattern underline.
  ''' </summary>
  DashDot = 6

  ''' <summary>
  ''' 'Dash-dot-dot' pattern underline.
  ''' </summary>
  DashDotDot = 7

  ''' <summary>
  ''' Single-line wave style underline.
  ''' </summary>
  Wave = 8

  ''' <summary>
  ''' Single-line solid underline with extra thickness.
  ''' </summary>
  Thick = 9

  ''' <summary>
  ''' Single-line solid underline with less thickness.
  ''' </summary>
  HairLine = 10

  ''' <summary>
  ''' Double-line wave style underline.
  ''' </summary>
  DoubleWave = 11

  ''' <summary>
  ''' Single-line wave style underline with extra thickness.
  ''' </summary>
  HeavyWave = 12

  ''' <summary>
  ''' 'Long Dash' pattern underline.
  ''' </summary>
  LongDash = 13

  ''' <summary>
  ''' 'Dash' pattern underline with extra thickness.
  ''' </summary>
  ThickDash = 14

  ''' <summary>
  ''' 'Dash-dot' pattern underline with extra thickness.
  ''' </summary>
  ThickDashDot = 15

  ''' <summary>
  ''' 'Dash-dot-dot' pattern underline with extra thickness.
  ''' </summary>
  ThickDashDotDot = 16

  ''' <summary>
  ''' 'Dotted' pattern underline with extra thickness.
  ''' </summary>
  ThickDotted = 17

  ''' <summary>
  ''' 'Long Dash' pattern underline with extra thickness.
  ''' </summary>
  ThickLongDash = 18
  End Enum
  ''' <summary>
  ''' Specifies the color of underline for a segment of rich text.
  ''' </summary>
  Public Enum UnderlineColor
  ''' <summary>
  ''' No specific underline color specified.
  ''' </summary>
  None = 0

  ''' <summary>
  ''' Black.
  ''' </summary>
  Black = 1

  ''' <summary>
  ''' Blue.
  ''' </summary>
  Blue = 2

  ''' <summary>
  ''' Cyan.
  ''' </summary>
  Cyan = 3

  ''' <summary>
  ''' LimeGreen.
  ''' </summary>
  LimeGreen = 4

  ''' <summary>
  ''' Magenta.
  ''' </summary>
  Magenta = 5

  ''' <summary>
  ''' Red.
  ''' </summary>
  Red = 6

  ''' <summary>
  ''' Yellow.
  ''' </summary>
  Yellow = 7

  ''' <summary>
  ''' White.
  ''' </summary>
  White = 8

  ''' <summary>
  ''' DarkBlue.
  ''' </summary>
  DarkBlue = 9

  ''' <summary>
  ''' DarkCyan.
  ''' </summary>
  DarkCyan = 10

  ''' <summary>
  ''' Green.
  ''' </summary>
  Green = 11

  ''' <summary>
  ''' DarkMagenta.
  ''' </summary>
  DarkMagenta = 12

  ''' <summary>
  ''' Brown.
  ''' </summary>
  Brown = 13

  ''' <summary>
  ''' OliveGreen.
  ''' </summary>
  OliveGreen = 14

  ''' <summary>
  ''' DarkGray.
  ''' </summary>
  DarkGray = 15

  ''' <summary>
  ''' Gray.
  ''' </summary>
  Gray = 16
  End Enum
Public Sub SelectionUnderline(ByVal UTUnderlineType As UnderlineStyle, Optional ByVal UnderlineColor As UnderlineColor = PlusRichTextBox.UnderlineColor.Black)

  ' Set the underline color
  Dim RTFformat As New CHARFORMAT()
  RTFformat.cbSize = Marshal.SizeOf(RTFformat)
  RTFformat.dwMask = CFM_UNDERLINETYPE 'Or CFM_UNDERLINE


  RTFformat.bUnderlineType = CByte(UTUnderlineType)
  RTFformat.bUnderlineColor = CByte(UnderlineColor)
  SendMessage(Me.Handle, EM_SETCHARFORMAT, SCF_SELECTION, RTFformat)

  End Sub
o sublinhado aparece com o tipo, mas nao a cor. alguem me pode explicar o que esta mal?
1 - eu uso o windows 7;
2 - eis 1 alteraçao que fiz no UserControl:
Código:
Imports System.ComponentModel
Imports System.Runtime.InteropServices

<Serializable()>
Public Class PlusRichTextBox
  Inherits RichTextBox

  'lets Wrap the Text Table
#Region "WrapTable"
  <DllImport("kernel32.dll", CharSet:=CharSet.Auto)> _
  Private Shared Function LoadLibrary(dllName As String) As IntPtr
  End Function
  Protected Overrides ReadOnly Property CreateParams() As CreateParams
  Get
  Dim baseParams As CreateParams = MyBase.CreateParams
  If LoadLibrary("msftedit.dll") <> IntPtr.Zero Then
  baseParams.ClassName = "RICHEDIT50W"
  End If
  Return baseParams
  End Get
  End Property
#End Region
 
Back
Topo