← 返回所有博客文章

Spread for WinForms 默认提供了在列头和行头上的拖动操作来改变列宽和行高的支持,不过在数据区域的网格线上的拖动操作不支持改变列宽和行高的功能,本文主要目的也是添加这一功能。

我们主要会用到Spread的MouseDown,MouseMove和MouseUp事件,代码如下:

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        int start = 0;
        float width = 0;
        bool mousedown = false;


        private void Form1_Load(object sender, EventArgs e)
        {
            fpSpread1.ActiveSheet.ColumnHeader.Visible = false;
            fpSpread1.MouseDown += new MouseEventHandler(fpSpread1_MouseDown);
            fpSpread1.MouseUp += new MouseEventHandler(fpSpread1_MouseUp);
            fpSpread1.MouseMove += new MouseEventHandler(fpSpread1_MouseMove);
        }
        
        private void fpSpread1_MouseMove(object sender, MouseEventArgs e)
        {
            if (mousedown && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                fpSpread1.ActiveSheet.ActiveColumn.Width = width + (e.X - start);
            }
        }

        void fpSpread1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                fpSpread1.Cursor = Cursors.VSplit;
                start = e.X;
                mousedown = true;
                width = fpSpread1.ActiveSheet.ActiveColumn.Width;
            }
        }

        void fpSpread1_MouseUp(object sender, MouseEventArgs e)
        {
            if (mousedown && e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                start = 0;
                width = 0;
                mousedown = false;
            }
        }
    }

源码下载:VS2010 + Spread .NET 6.0 7664_Width.zip (10.10 kb)