代码之家  ›  专栏  ›  技术社区  ›  Keith Maurino

文本和边框之间的格式文本框填充

  •  20
  • Keith Maurino  · 技术社区  · 14 年前

    是否可以在文本和边框之间的富文本框控件中添加填充?

    我试着将一个富文本框停靠在一个面板内,它的四个边的填充设置为10,这就完成了我想要的。除了需要填充富文本框的垂直滚动条外。

    8 回复  |  直到 6 年前
        1
  •  27
  •   Larry    13 年前

    RichTextBox没有填充属性。

    通过将richtextbox放入 面板 ,有相同的 BackColor 属性作为richtextbox(通常 Color.White )

    然后,设置 Dock RichTextBox的属性 Fill 和玩 Padding 面板控件的属性。

        2
  •  21
  •   Uwe Keim Tomasz    6 年前

    其中包括 em\u getrect >a>和 em\u setrect >>。

    把这两个结合在一起,你可以做到:

    看起来像这样:

    我已经写了 a small c extension class to wrap this all up.

    用法示例:

    const int dist=24;
    richtextbox1.setinnermarkins(dist,dist,dist,0);
    < /代码> 
    
    

    这会将内部页边距左、上和右设置为24,使底部为零。

    请注意,滚动时,上边距保持设置状态,如下所示:

    就我个人而言,这看起来“不自然”。当滚动时,上边缘也将变为零。

    也许有个解决办法

    完整源代码

    根据要求:

    public static class richtextboxextensions
    {
    公共静态void setinnermarkins(此textbox基文本框,int-left,int-top,int-right,int-bottom)
    {
    var rect=textbox.getFormattingRect();
    
    var new rect=新矩形(左,上,矩形宽度-左-右,矩形高度-上-下);
    textbox.setformattingrect(newrect);
    }
    
    [结构布局(layoutkind.sequential)]
    私有结构rect
    {
    剩余public readonly int;
    公共只读int top;
    公共只读权;
    公共只读int bottom;
    
    私有矩形(int-left,int-top,int-right,int-bottom)
    {
    左=左;
    顶部=顶部;
    右=右;
    底部=底部;
    }
    
    公共矩形(矩形R):这个(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;
    
    私有静态void setformattingrect(此textbox基文本框,矩形rect)
    {
    var rc=新rect(rect);
    sendmessagerefrect(textbox.handle,emsetrect,0,ref rc);
    }
    
    私有静态矩形getFormattingRect(此文本框基文本框)
    {
    var rect=new rectangle();
    sendmessage(textbox.handle,emgetrect,(intptr)0,ref rect);
    返回记录;
    }
    }
    < /代码> 
    EM_GETRECTEM_SETRECT.

    将这两者结合在一起,您可以做到:

    enter image description here

    _如下:

    enter image description here

    我已经写了a small C# extension class把这些都包起来。

    使用实例:

    const int dist = 24;
    richTextBox1.SetInnerMargins(dist, dist, dist, 0);
    

    这会将内部页边距左、上和右设置为24,使底部为零。

    请注意,滚动时,上边距保持设置状态,如下所示:

    enter image description here

    就我个人而言,这看起来“不自然”。我更希望当滚动时,上边距也变为零。

    也许有个解决办法

    完整源代码

    根据要求:

    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;
        }
    }
    
        3
  •  10
  •   Meiker    10 年前

    我也遇到过同样的问题,但所描述的答案对我没有帮助,这对我很有用,所以如果有帮助的话,我会和大家分享。

    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;
    
        4
  •  6
  •   madsolver    9 年前

    快速而简单的方法是在窗体加载和窗体/控件调整大小事件时调用此示例方法,从垂直滚动中偏移文本:

    private void AdjustTextBoxRMargin()
    {
        richTextBox1.RightMargin = richTextBox1.Size.Width - 35;
    }
    

    35的值似乎适用于Win7,但在其他版本的Windows上可能有所不同。

        5
  •  3
  •   Jordan Rhode    10 年前

    因为richtextbox没有padding属性。您可以创建自己的richtextbox子类,如本文中所述:

    http://www.codeproject.com/Articles/21437/A-Padded-Rich-Text-Box-Subclass

        6
  •  1
  •   Phil Lambert    10 年前

    也许这就是你需要的?

    richTextBox.SelectionIndent += 20;
    
        7
  •  -1
  •   Abel    9 年前

    也许您要缩进文本?像左缩进和右缩进? 如果是这样,那么您可以只使用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 )
    

    希望有帮助。

        8
  •  -3
  •   allonym    14 年前

    将右边的填充设置为0,然后将RichTextBox的右边距设置为10。没有测试,但应该有效。