- 題名: RichTextBoxで指定した文字列の背景色を着色前に戻す方法
- 日時: 2014/11/10 3:40:26
- ID: 32694
- この記事の返信元:
- (なし)
- この記事への返信:
- [32695] Re[1]: RichTextBoxで指定した文字列の背景色を着色前に戻す方法2014/11/10 7:27:08
- [32696] Re[1]: RichTextBoxで指定した文字列の背景色を着色前に戻す方法2014/11/10 12:04:29
- ツリーを表示
■No32698に返信(くるりさんの記事) > RichTextBoxの背景色クリアについては > 教えていただいた方法を参考に対応を試みてみようと思いますが > 現時点では理解できていない部分も多いため、対応には > 時間がかかるかもしれません。 Hongliangさんのコードで実装してみました。 Imports System.Runtime.InteropServices Imports System.Runtime.CompilerServices Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Button1.Text = " No32694 " Button2.Text = " No32696 " CheckBox1.Checked = True RichTextBox1.Text = <![CDATA[あああああ いいいいい ううううう えええええ おおおおお]]>.Value RichTextBox1.Select(6, 5) RichTextBox1.SelectionBackColor = Color.YellowGreen RichTextBox1.Select(18, 5) RichTextBox1.SelectionBackColor = Color.Cyan RichTextBox1.Select(0, 0) End Sub Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 'RichTextBox1.SelectionBackColor = Color.Transparent RichTextBox1.SelectionBackColor = Color.Empty End Sub Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click RichTextBox1.ResetSelectionBackColor() End Sub Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged RichTextBox1.BackColor = If(CheckBox1.Checked, Color.Khaki, Color.LightPink) End Sub End Class Public Module HongliangAnswer <Extension()> _ Public Sub ResetSelectionBackColor(rtb As RichTextBox) If rtb.IsHandleCreated Then Const CFM_BACKCOLOR As Integer = &H4000000 Const CFE_AUTOBACKCOLOR As Integer = CFM_BACKCOLOR Const EM_SETCHARFORMAT As Integer = (&H400 + 68) Const SCF_SELECTION As Integer = &H1 Dim cf2 As New CHARFORMAT2A() With {.dwMask = CFM_BACKCOLOR, .dwEffects = CFE_AUTOBACKCOLOR} SendMessage(New HandleRef(rtb, rtb.Handle), EM_SETCHARFORMAT, SCF_SELECTION, cf2) End If End Sub <DllImport("User32", CharSet:=CharSet.Ansi)> _ Private Function SendMessage( hWnd As HandleRef, msg As Integer, wParam As Integer, _ <[In](), [Out](), MarshalAs(UnmanagedType.LPStruct)> lParam As CHARFORMAT2A _ ) As IntPtr End Function <StructLayout(LayoutKind.Sequential, Pack:=4)> _ Private Class CHARFORMAT2A Public cbSize As Integer = Marshal.SizeOf(GetType(CHARFORMAT2A)) Public dwMask As Integer = 0 Public dwEffects As Integer = 0 Public yHeight As Integer = 0 Public yOffset As Integer = 0 Public crTextColor As Integer = 0 Public bCharSet As Byte = 0 Public bPitchAndFamily As Byte = 0 <MarshalAs(UnmanagedType.ByValArray, SizeConst:=32)> _ Public szFaceName() As Byte = New Byte(31) {} Public wWeight As Short = 0 Public sSpacing As Short = 0 Public crBackColor As Integer = 0 Public lcid As Integer = 0 Public dwReserved As Integer = 0 Public sStyle As Short = 0 Public wKerning As Short = 0 Public bUnderlineType As Byte = 0 Public bAnimation As Byte = 0 Public bRevAuthor As Byte = 0 End Class End Module
分類:[.NET]
RichTextBoxで選択した文字の背景色を変更したのち
初期状態(着色前のRichTextBox.Backcolorと同期がとれている状態)に
戻すことは可能でしょうか?
もし、可能であればその方法を教えていただきたく、よろしくお願いします。
NG例1:
「RichTextBox1.SelectionBackColor = Color.Empty」や
「RichTextBox1.SelectionBackColor = Color.Transparent」では
文字列の背景色は白となってしまいました。
NG例2:
「RichTextBox1.SelectionBackColor = RichTextBox1.BackColor」では
一時的にRichTextBox1のBackColorと同一になりますが
RichTextBox1のBackColorを変更した場合、同期はしません。