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

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

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

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

posted on 2021-08-13 12:12  张光荣的正能量  阅读(109)  评论(0编辑  收藏  举报