이것은 흥미 롭습니다. Windows 7/.Net 3.5에서 (내가 테스트 한 곳과 다른 곳에서) System.Windows.Forms.RichTextBox.SelectionBackColor
에 선택 색상을 지우는 버그가있을 수 있습니다. source code을 수행합니다 당신이 발견 한 것처럼 richTextBox.BackColor
로 설정하면
public Color SelectionBackColor {
set
{
//Note: don't compare the value to the old value here: it's possible that
//you have a different range selected.
selectionBackColorToSetOnHandleCreated = value;
if (IsHandleCreated)
{
NativeMethods.CHARFORMAT2A cf2 = new NativeMethods.CHARFORMAT2A();
if (value == Color.Empty)
{
cf2.dwEffects = RichTextBoxConstants.CFE_AUTOBACKCOLOR;
}
else
{
cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR;
cf2.crBackColor = ColorTranslator.ToWin32(value);
}
UnsafeNativeMethods.SendMessage(new HandleRef(this, Handle), RichTextBoxConstants.EM_SETCHARFORMAT, RichTextBoxConstants.SCF_SELECTION, cf2);
}
}
}
이, 실제로 다시 색상을 취소하지 않습니다. RichTextBox.DefaultBackColor
으로 설정하면 색상을 지우지 않으며 선택 영역의 색상을 기본 회색 조절 색상으로 설정하기 만합니다. 소스 코드가 인 것처럼 보입니다. Color.Empty
으로 설정하면 선택 색상을 지우려고합니다.하지만 적어도 내 컴퓨터에서는 아무 것도하지 않습니다.
그러나 메시지를 보내기 전에 빈 색상으로 cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR;
을 설정하는 확장 방법을 만들면 SetSelectionBackColor(Color.Empty)
이 작동합니다.here 및 here 및 here 및 here 및 here에서 적응 상수와 클래스와
public static void SetSelectionBackColor(this RichTextBox richTextBox, Color value)
{
if (richTextBox.IsHandleCreated && value == Color.Empty)
{
var cf2 = new CHARFORMAT2();
cf2.dwEffects = RichTextBoxConstants.CFE_AUTOBACKCOLOR;
cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR;
cf2.crBackColor = ColorTranslator.ToWin32(value);
UnsafeNativeMethods.SendMessage(new HandleRef(richTextBox, richTextBox.Handle), RichTextBoxConstants.EM_SETCHARFORMAT, RichTextBoxConstants.SCF_SELECTION, cf2);
}
else
{
richTextBox.SelectionBackColor = value;
}
}
전체에있어서,
public static class RichTextBoxConstants
{
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/RichTextBoxConstants.cs,31b52ac41e96a888
/* EM_SETCHARFORMAT wparam masks */
internal const int SCF_SELECTION = 0x0001;
internal const int EM_SETCHARFORMAT = (NativeMethods.WM_USER + 68);
internal const int CFM_BACKCOLOR = 0x04000000;
/* NOTE: CFE_AUTOCOLOR and CFE_AUTOBACKCOLOR correspond to CFM_COLOR and
CFM_BACKCOLOR, respectively, which control them */
internal const int CFE_AUTOBACKCOLOR = CFM_BACKCOLOR;
}
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public class CHARFORMAT2
{
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NativeMethods.cs,acde044a28b57a48
// http://pinvoke.net/default.aspx/Structures/CHARFORMAT2.html
public int cbSize = Marshal.SizeOf(typeof(CHARFORMAT2));
public int dwMask;
public int dwEffects;
public int yHeight;
public int yOffset;
public int crTextColor;
public byte bCharSet;
public byte bPitchAndFamily;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
public string szFaceName;
public short wWeight;
public short sSpacing;
public int crBackColor;
public int lcid;
public int dwReserved;
public short sStyle;
public short wKerning;
public byte bUnderlineType;
public byte bAnimation;
public byte bRevAuthor;
public byte bReserved1;
}
public static class NativeMethods
{
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/NativeMethods.cs,e75041b5218ff60b
public const int WM_USER = 0x0400;
public static void SetSelectionBackColor(this RichTextBox richTextBox, Color value)
{
if (richTextBox.IsHandleCreated && value == Color.Empty)
{
var cf2 = new CHARFORMAT2();
cf2.dwEffects = RichTextBoxConstants.CFE_AUTOBACKCOLOR;
cf2.dwMask = RichTextBoxConstants.CFM_BACKCOLOR;
cf2.crBackColor = ColorTranslator.ToWin32(value);
UnsafeNativeMethods.SendMessage(new HandleRef(richTextBox, richTextBox.Handle), RichTextBoxConstants.EM_SETCHARFORMAT, RichTextBoxConstants.SCF_SELECTION, cf2);
}
else
{
richTextBox.SelectionBackColor = value;
}
}
}
public static class UnsafeNativeMethods
{
// http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/UnsafeNativeMethods.cs,0d546f58103867e3
// For RichTextBox
//
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(HandleRef hWnd, int msg, int wParam, [In, Out, MarshalAs(UnmanagedType.LPStruct)] CHARFORMAT2 lParam);
}
방법에 따라 작동하는 몇 가지 코드를 제공 어떻습니까? –
다른 잠긴 작업 기계에서와 마찬가지로 원본 소스 코드를 사용할 수 없습니다. 위의 간단한 예제를 참조하십시오. – Matthew
나는 이것이 가능하다고 생각하지 않는다. 색상은 텍스트 청크의 값이며 일부 공간에 대한 참조는 아닙니다. 따라서 모든 텍스트를 반복하여 색상을 변경해야합니다. 내가 의미하는 바를 알기 위해 rtf 코드를 들여다 볼 수 있습니다. (그리고 투명도는 어쨌든 실제 색상이 아니기 때문에 여기서는 도움이되지 않을 것입니다.) – TaW