基类MListItem---------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MoveableListLib
{
    public partial class MListItem : UserControl
    {
        private Point startLocation;
        private Point endLocation;
        private Point initLocation;
        private int id = -1;
        private int dy = 0;
        public MListItem(string index)
        {
            InitializeComponent();
            this.lblContent.Text += index;
            id = int.Parse(index);
            initLocation = this.Location;
        }

        private void label1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(this.lblContent.Text +"的详细信息!");
        }

        private void MListItem_MouseDown(object sender, MouseEventArgs e)
        {
            MoveableList.firstCtrlLocation = this.Parent.Controls[0].Location;
            if (e.Button == MouseButtons.Left)
            {
                int size = this.Parent.Controls.Count;
                startLocation = e.Location;
            }
        }

        private void MListItem_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (startLocation != null)
                {
                    endLocation = e.Location;
                    int xx = endLocation.Y - startLocation.Y;
                    if (Math.Abs(xx)>0.1)
                    reloadCtrlPosition(xx);
                 }
            }
        }

        private void MListItem_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (startLocation != null)
                {
                    dy = this.Location.Y - initLocation.Y;
                    if (dy == 0)
                        return;
                    SetPosition();
                }
            }
        }

        /// <summary>
        /// 重新计算位置
        /// </summary>
        /// <param name="xx">变化值</param>
        private void reloadCtrlPosition(int xx)
        {
            foreach (Control ctrl in this.Parent.Controls)
            {
                ctrl.Location = new Point(0, ctrl.Location.Y + xx);
            }

        }

        private void SetPosition()
        {
            if (dy > 0)
            {
                if (-MoveableList.firstCtrlLocation.Y == 0)
                {
                    do
                    {
                        foreach (Control ctrl in this.Parent.Controls)
                        {
                            ctrl.Location = new Point(0, ctrl.Location.Y - 1);
                        }
                    }
                    while (--dy != 0);
                }
                else
                {
                    if(dy>=100)
                    {
                        dy = this.Height - dy;
                        do
                        {
                            foreach (Control ctrl in this.Parent.Controls)
                            {
                                ctrl.Location = new Point(0, ctrl.Location.Y + 1);
                            }
                        }
                        while (--dy != 0);
                    }
                    else
                    {
                        do
                        {
                            foreach (Control ctrl in this.Parent.Controls)
                            {
                                ctrl.Location = new Point(0, ctrl.Location.Y - 1);
                            }
                        }
                        while (--dy != 0);

                    }
                    MoveableList.firstCtrlLocation.Y = this.Parent.Controls[0].Location.Y;

                }

            }
            else
            {
                dy = -dy;
                if (-MoveableList.firstCtrlLocation.Y == this.Height * (this.Parent.Controls.Count - 1))
                {
                    do
                    {
                        foreach (Control ctrl in this.Parent.Controls)
                        {
                            ctrl.Location = new Point(0, ctrl.Location.Y + 1);
                        }
                    }
                    while (--dy != 0);
                }
                if (-MoveableList.firstCtrlLocation.Y < this.Height * (this.Parent.Controls.Count - 1))
                {
                    if (dy >= 100)
                    {
                        dy = this.Height - dy;
                        do
                        {
                            foreach (Control ctrl in this.Parent.Controls)
                            {
                                ctrl.Location = new Point(0, ctrl.Location.Y - 1);
                            }
                        }
                        while (--dy != 0) ;

                    }
                    else
                    {
                        do
                        {
                            foreach (Control ctrl in this.Parent.Controls)
                            {
                                ctrl.Location = new Point(0, ctrl.Location.Y + 1);
                            }
                        }
                        while (--dy != 0);
                    }
                    MoveableList.firstCtrlLocation.Y = this.Parent.Controls[0].Location.Y;
                }
            }

        }

       }
   }

基类MoveableList----------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MoveableListLib
{
    public partial class MoveableList : UserControl
    {
        //存储菜单项
        public ArrayList arrList = new ArrayList();

        //最大数目
        private int max =5;
        //单元数目
        private int unit = 5;

        public static Point firstCtrlLocation;

        public MoveableList()
        {
            InitializeComponent();       
            //初始化
            initListContent(); 
        }

        public MoveableList(int pMax,int pUnit):this()
        {
            max = pMax;
            unit = pUnit;
        }

        /// <summary>
        /// 设置大小
        /// </summary>
        /// <param name="pMax">最大记录数</param>
        /// <param name="pUnit">加载单位记录数</param>
        public void setItemsSize(int pMax, int pUnit)
        {
            max = pMax;
            unit = pUnit;
        }

        /// <summary>
        /// 初始化内容
        /// </summary>
        public  void initListContent()
        {
            for (int i = 0; i < unit; i++)
            {
                MListItem item = new MListItem(i.ToString());
                item.Location = new Point(0,item.Height * i);
                this.Controls.Add(item);
            }
        }

        /// <summary>
        /// 加载新数据
        /// </summary>
        /// <param name="lastPosition">最后一条记录的位置</param>
        public void loadNewData(int lastPosition)
        {
            int currentSize =this.Controls.Count;
            for (int i = 0; i < unit; i++)
            {
                MListItem item = new MListItem((currentSize+i).ToString());
                item.Location = new Point(0, item.Height *i + lastPosition);
                this.Controls.Add(item);
            }
        }
        /// <summary>
        /// 是否还存在其他数据
        /// </summary>
        /// <returns></returns>
        public bool existOtherData()
        {
            if (this.Controls.Count < max)
                return true;
            return false;
        }
    }

}

 

來源:http://blog.csdn.net/bluesky_zl/article/details/7564064

arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()