关于 用鼠标移动拖放 TabControl 标签 操作 实现 类

1.相关说明已有

2.本类有可以改进的地方,如更进,希望交流

3.对多行标签的支持没做特别解决,效果不太好

4.初学者使用,即可通过其构造函数直接加载需要赋予此拖放功能的 TabControl 即可实现,如: tabMover=new TabMover2(tabControl1,20);

/// <summary>
/// 标签鼠标边距感应触发拖移
/// 逻辑:可自定义 向左或向右拖放 感应边区宽度
/// 通用:是
/// 测试:正确
/// 使用:建议
/// 改进:有
///
/// 时间:202108122244-131150
/// 注:未对多行标签做特殊处理,在多行标签时可以将究使用
/// </summary>
public class TabMover2
{
    TabControl tc;
    TabPage clkTp, msTp;
    Rectangle clkRct, msRct;
    /// <summary>
    /// 触发边距
    /// </summary>
    int trgBdr = 20;
    int idx;
    /// <summary>
    ///
    /// </summary>
    /// <param name="tc"></param>
    public TabMover2(TabControl tc)
    {
        tc.MouseDown += _MouseDown;
        tc.MouseMove += _MouseMove;
        tc.MouseUp += _MouseUp;

        this.tc = tc;
    }
    public TabMover2(TabControl tc, int trgBdr)
    {
        tc.MouseDown += _MouseDown;
        tc.MouseMove += _MouseMove;
        tc.MouseUp += _MouseUp;

        this.tc = tc;
    }

    private void _MouseUp(object sender, MouseEventArgs e)
    {
        tc.Cursor = Cursors.Default;
    }

    private void _MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            clkTp = tc.SelectedTab;
            //idx = tc.TabPages.IndexOf(clkTp);
            //clkRct = tc.GetTabRect(idx);
        }
    }
    private void _MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            tc.Cursor = Cursors.Hand;

            if (clkTp == null)
                return;

            F.Ctrs.TC.Tab.Mos(tc, e, out msTp);
            if (msTp == null)
                return;

            if (msTp == clkTp)
                return;

            idx = tc.TabPages.IndexOf(clkTp);
            clkRct = tc.GetTabRect(idx);

            idx = tc.TabPages.IndexOf(msTp);
            msRct = tc.GetTabRect(idx);

            if (msRct.Location.X > clkRct.Location.X)
            {
                if (e.X < msRct.Location.X + msRct.Width - trgBdr)
                    return;
            }
            else if (msRct.Location.X < clkRct.Location.X)
            {
                if (e.X > msRct.Location.X + trgBdr)
                    return;
            }

            tc.TabPages.Remove(clkTp);
            tc.TabPages.Insert(idx, clkTp);

            tc.SelectedTab = clkTp;
            msTp = clkTp;
        }
    }
}
/// <summary>
/// 标签鼠标边距感应触发拖移[触发感应区域为向左或向右 端点 以拖动]
/// 逻辑:可自定义 向左或向右拖放 感应边区宽度,且首先以被拖动标签宽度与自定义边区宽度作为对比得出实际需要的感应边区宽度
/// 通用:是
/// 测试:正确
/// 使用:[在未完全使用情况总结下] 推荐
/// 改进:可能有待
///
/// 时间:202108131044-1150
/// 注:未对多行标签做特殊处理,在多行标签时可以将究使用
/// </summary>
public class TabMover3
{
    TabControl tc;
    TabPage clkTp, msTp;
    Rectangle clkRct, msRct;
    /// <summary>
    /// 触发边距
    /// </summary>
    int trgBdr = 20, trgBdr_ = 20;
    int idx;
    /// <summary>
    ///
    /// </summary>
    /// <param name="tc"></param>
    public TabMover3(TabControl tc)
    {
        tc.MouseDown += _MouseDown;
        tc.MouseMove += _MouseMove;
        tc.MouseUp += _MouseUp;

        this.tc = tc;
    }
    public TabMover3(TabControl tc, int trgBdr)
    {
        tc.MouseDown += _MouseDown;
        tc.MouseMove += _MouseMove;
        tc.MouseUp += _MouseUp;

        this.tc = tc;
        this.trgBdr = trgBdr;
    }
    private void _MouseUp(object sender, MouseEventArgs e)
    {
        tc.Cursor = Cursors.Default;
    }

    private void _MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            clkTp = tc.SelectedTab;
        }
    }
    private void _MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            tc.Cursor = Cursors.Hand;

            if (clkTp == null)
                return;

            F.Ctrs.TC.Tab.Mos(tc, e, out msTp);
            if (msTp == null)
                return;

            if (msTp == clkTp)
                return;

            idx = tc.TabPages.IndexOf(clkTp);
            clkRct = tc.GetTabRect(idx);

            idx = tc.TabPages.IndexOf(msTp);
            msRct = tc.GetTabRect(idx);

            if (msRct.Width > clkRct.Width)
            {
                if (trgBdr > clkRct.Width)
                    trgBdr_ = clkRct.Width;
                else
                    trgBdr_ = trgBdr;
            }
            else
                trgBdr_ = trgBdr;

            if (msRct.Location.X > clkRct.Location.X)
            {
                if (e.X < msRct.Location.X + msRct.Width - trgBdr_)
                    return;
            }
            else if (msRct.Location.X < clkRct.Location.X)
            {
                if (e.X > msRct.Location.X + trgBdr_)
                    return;
            }

            tc.TabPages.Remove(clkTp);
            tc.TabPages.Insert(idx, clkTp);

            tc.SelectedTab = clkTp;
            msTp = clkTp;
        }
    }
}

  

交流 QQ : 2412366909@qq.com
手机号码:177-7499-4428

注:本人测试过程中暂未发现造成存取不正确的问题

关于 用鼠标移动拖放 TabControl 标签 操作 实现 类

上一篇:mybatis mapper接口找不到xml


下一篇:微信小程序开发 url乱码 url中文参数