读书人

VB.net中richtextbox控件中怎么改变选

发布时间: 2013-10-18 20:53:13 作者: rapoo

VB.net中richtextbox控件中如何改变选中行的背景色
如题,是改变行的背景色,不是行中的字符(中/英文)颜色改变哈。
找了些方法,只能改变行中字符的颜色,暂时没找到改变一行背景的颜色,在这里求助大家了。
[解决办法]

Imports System.Runtime.InteropServices

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cf As New CHARFORMAT2A
Dim colr As Color = Color.Red
Dim ret As Integer
With cf
.cbSize = Marshal.SizeOf(cf) 'setup the size of the character format

.dwMask = CFM_BACKCOLOR 'what to test

.crBackColor = Drawing.ColorTranslator.ToWin32(colr)
ret = SendMessage(Me.RichTextBox1.Handle, EM_SETCHARFORMAT, SCF_SELECTION, cf)
End With 'CF
End Sub

Private Const WM_USER As Integer = &H400

Private Const CFM_BACKCOLOR As Integer = &H4000000

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 LF_FACESIZE As Integer = 32

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, _
ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As CHARFORMAT2A) As Integer

<StructLayout(LayoutKind.Sequential, Pack:=4)> _
Public Class CHARFORMAT2A
Public cbSize As Integer = Marshal.SizeOf(GetType(CHARFORMAT2A))
Public dwMask As Integer
Public dwEffects As Integer
Public yHeight As Integer
Public yOffset As Integer
Public crTextColor As Integer
Public bCharSet As Byte
Public bPitchAndFamily As Byte
<MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H20)> _
Public szFaceName As Byte() = New Byte(&H20 - 1) {}
Public wWeight As Short
Public sSpacing As Short
Public crBackColor As Integer
Public lcid As Integer


Public dwReserved As Integer
Public sStyle As Short
Public wKerning As Short
Public bUnderlineType As Byte
Public bAnimation As Byte
Public bRevAuthor As Byte
End Class



End Class

读书人网 >VB Dotnet

热点推荐