张光荣的正能量 [机械软件...改革利剑...向着改变世界...]

博客园 首页 新随笔 联系 订阅 管理

/// <summary>
/// 根据内容重设大小以达到不会显示滚动条
/// 测试:正确
/// 时间:202106021957
/// </summary>
public class ReSizer
{
    static int padBtm = 6;//此值基本合适不会出现滚动条[至少字体在 rtb 默认字体属性时]
    static int padRit = 20;

    RichTextBox rtb;
    Rectangle rtg;

    bool isIni = false;
    bool reWid = true, reHei=true;
    int idx;

    List<RichTextBox> rtbL;// = new List<RichTextBox>();
    /// <summary>
    /// 获取需要增设的 rtb
    /// </summary>
    public RichTextBox IAddRtb
    {
        set
        {
            idx = rtbL.IndexOf(value);
            if (idx > -1)
                return;

            ICalAddRtb(value);
        }
    }
    public ReSizer()
    {
        rtbL = new List<RichTextBox>();
    }
    public ReSizer(bool reWid, bool reHei)
    {
        this.reWid = reWid;
        this.reHei = reHei;
        rtbL = new List<RichTextBox>();
    }
    public ReSizer(RichTextBox rtb, bool reWid, bool reHei)
    {
        this.reWid = reWid;
        this.reHei = reHei;
        rtbL = new List<RichTextBox>();

        ICalAddRtb(rtb);
    }
    /// <summary>
    /// 添加需要增设的 rtb
    /// </summary>
    /// <param name="rtb"></param>
    public void ICalAddRtb(RichTextBox rtb)
    {
        #region

        rtb.Margin = new Padding(0);
        rtb.MouseDown += _MouseDown;

        if (reWid)
        {
            rtb.WordWrap = false;
            if (reHei)
            {
                rtb.ScrollBars = RichTextBoxScrollBars.None;
                rtb.ContentsResized += _ContentsResized;
            }
            else
            {
                rtb.ScrollBars = RichTextBoxScrollBars.Vertical;
                rtb.ContentsResized += _ContentsResizedWid;
            }
        }
        else if (reHei)
        {
            rtb.WordWrap = true;
            rtb.ScrollBars = RichTextBoxScrollBars.Horizontal;
            rtb.ContentsResized += _ContentsResizedHei;
        }

        rtbL.Add(rtb);

        #endregion
    }
    /// <summary>
    /// 清除记录的 rtb
    /// </summary>
    public void ICalIniRtbL()
    {
        rtbL.Clear();
    }

    #region

    private void _MouseDown(object sender, MouseEventArgs e)
    {
        rtb = sender as RichTextBox;

        if (!isIni)
            isIni = true;
    }
    private void _ContentsResized(object sender, ContentsResizedEventArgs e)
    {
        if (!isIni)
            return;

        rtg = e.NewRectangle;
        //rtb.Size = rtg.Size;//用此行会出现向上隐藏一行...
        rtb.Height = rtg.Height + padBtm;
        rtb.Width = rtg.Width + padRit;
    }
    private void _ContentsResizedHei(object sender, ContentsResizedEventArgs e)
    {
        if (!isIni)
            return;

        rtg = e.NewRectangle;
        rtb.Height = rtg.Height + padBtm;
    }
    private void _ContentsResizedWid(object sender, ContentsResizedEventArgs e)
    {
        if (!isIni)
            return;

        rtg = e.NewRectangle;
        rtb.Width = rtg.Width + padRit;
    }

    #endregion
}

使用方法:

1.实例化: ReSizer reSizer=new ReSizer();
2.添加需要设置此类功能的 RichTextBox 方式:
a.通过属性:IGetAddRtb=richTextBox1;
b.通过函数:ICalAddRtb(richTextBox1);
3.清除已记录的添加此类功能的 RichTextBox 列表:ICalIniRtbL();

 

技术参考网址:

https://www.cnblogs.com/sdner/p/8329280.html

https://blog.csdn.net/gooodiuck/article/details/39291385

 

交流 QQ : 2412366909@qq.com

手机号码:177-7499-4428

注: 1,本人使用过程中暂未发现造成存取不正确的问题

      2.此类应该有此可以优化的地方,即在什么情况下才 reSiz...

 

posted on 2021-06-02 20:19  张光荣的正能量  阅读(250)  评论(0编辑  收藏  举报