文本和边框之间的富文本框填充
是否可以在文本和边框之间的Rich Text Box控件中添加填充?
我尝试将一个富文本框对接在一个面板内,其四边的填充设置为10,这就完成了我想要的。 除了需要填充富文本框的垂直滚动条之外,它也会被填充。
RichTextBox没有填充属性。
通过将RichTextBox放在Panel中可以实现快速和脏的填充,该Panel具有与RichTextBox(通常是Color.White
)相同的BackColor
属性。
然后,将RichTextBox的Dock
属性设置为Fill
,并使用Panel控件的Padding
属性进行播放。
有EM_GETRECT
和EM_SETRECT
。
将这两者结合在一起,你可以做到这一点:
……看起来像这样:
我写了一个小的C#扩展类来包装这一切。
用法示例:
const int dist = 24; richTextBox1.SetInnerMargins(dist, dist, dist, 0);
这将左边,上边和右边的内边距设置为24,底部为零。
请注意,滚动时,上边距保持不变,如下所示:
就个人而言,这对我来说看起来“不自然”。 我希望当滚动上边距变为零时也是如此。
也许有一个解决方法……
完整的源代码
截至要求:
public static class RichTextBoxExtensions { public static void SetInnerMargins(this TextBoxBase textBox, int left, int top, int right, int bottom) { var rect = textBox.GetFormattingRect(); var newRect = new Rectangle(left, top, rect.Width - left - right, rect.Height - top - bottom); textBox.SetFormattingRect(newRect); } [StructLayout(LayoutKind.Sequential)] private struct RECT { public readonly int Left; public readonly int Top; public readonly int Right; public readonly int Bottom; private RECT(int left, int top, int right, int bottom) { Left = left; Top = top; Right = right; Bottom = bottom; } public RECT(Rectangle r) : this(r.Left, r.Top, r.Right, r.Bottom) { } } [DllImport(@"User32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)] private static extern int SendMessageRefRect(IntPtr hWnd, uint msg, int wParam, ref RECT rect); [DllImport(@"user32.dll", EntryPoint = @"SendMessage", CharSet = CharSet.Auto)] private static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam); private const int EmGetrect = 0xB2; private const int EmSetrect = 0xB3; private static void SetFormattingRect(this TextBoxBase textbox, Rectangle rect) { var rc = new RECT(rect); SendMessageRefRect(textbox.Handle, EmSetrect, 0, ref rc); } private static Rectangle GetFormattingRect(this TextBoxBase textbox) { var rect = new Rectangle(); SendMessage(textbox.Handle, EmGetrect, (IntPtr) 0, ref rect); return rect; } }
我有同样的问题,所描述的答案对我没有帮助,这对我有用,所以如果有帮助我会分享它。
richTextBox1.SelectAll(); richTextBox1.SelectionIndent += 15;//play with this values to match yours richTextBox1.SelectionRightIndent += 15;//this too richTextBox1.SelectionLength = 0; //this is a little hack because without this //i've got the first line of my richTB selected anyway. richTextBox1.SelectionBackColor = richTextBox1.BackColor;
一种快速简便的方法是通过在表单加载和form / control resize事件中调用此示例方法来从垂直滚动中偏移文本:
private void AdjustTextBoxRMargin() { richTextBox1.RightMargin = richTextBox1.Size.Width - 35; }
35的值似乎适用于Win7,但在其他版本的Windows上可能有所不同。
由于RichTextBox没有填充属性。 您可以像本文中的那样创建自己的RichTextBoxSubclass:
http://www.codeproject.com/Articles/21437/A-Padded-Rich-Text-Box-Subclass
也许这就是你需要的?
richTextBox.SelectionIndent += 20;
也许你正在寻找缩进文字? 像左右缩进? 如果是这样,那么你可以使用rtf对象的ReGEtIndent和ReSetIndent方法。
我在这做什么:
//first I find the length of the active window nLen := REGEtIndent( ::oActive:hWnd ) //now resetIndents of the rtf control RESetIndent( ::oActive:hWnd, nLen[ 1 ] + 100, nLen[ 2 ] + 100, -25 )
希望有所帮助。
将右侧的填充设置为0,然后将RichTextBox的RightMargin设置为10.未测试,但应该可以工作。