前两天看到这个帖子 http://topic.csdn.net/t/20050901/16/4244899.html,回去整了下 发现Form 挺有意思的,今天把有意思的代码分享,有点类似 磁性窗体
疯狂的Form主类
using System;
using System.Collections.Generic;
using System.Windows.Forms; 
using System.Text;
using System.Drawing; namespace FormMoving
{        public class MagneticForm : Form
        {
            private int leftX;
            private int rightX;
            private int topY;
            private int bottomY;
            private int HorizonLength ; //计算发生重叠的水平长度
            private int VerticalLength ;//计算发生重叠的垂直长度
            private bool moveSelf;
            /// <summary>
            /// 存放互动窗体
            /// </summary>
            public List<MagneticForm> FormList = new List<MagneticForm>();
            /// <summary>
            /// 用于设置其他窗体的Location
            /// </summary>
            private Point tmp = new Point();
            /// <summary>
            /// 设置或获取Form左边x坐标
            /// </summary>
            public int LeftX
            {
                get { return this.leftX; }
                set { this.leftX = value; }
            }
            /// <summary>
            /// 设置或获取Form右边x坐标
            /// </summary>
            public int RightX
            {
                get { return this.rightX; }
                set { this.rightX = value; }
            }
            /// <summary>
            /// 设置或获取Form上边y坐标
            /// </summary>
            public int TopY
            {
                get { return this.topY; }
                set { this.topY = value; }
            }
            /// <summary>
            /// 设置或获取Form下边y坐标
            /// </summary>
            public int BottomY
            {
                get { return this.bottomY; }
                set { this.bottomY = value; }
            }
            public bool MoveSelf
            {
                get { return this.moveSelf; }
                set { this.moveSelf = value; }
            }
            /// <summary>
            ///设置联动窗体列表
            /// </summary>
            /// <param name="form">MagneticForm 类型的窗体</param>
            public void SetForms(MagneticForm form)
            {
                ///是自己则不添加
                if (!this.Equals(form))
                {
                    this.FormList.Add(form);
                }
            }
            /// <summary>
            /// 设置窗体MagneticForm数组相互关联
            /// </summary>
            /// <param name="forms"></param>
            public void SetFormsArray(MagneticForm[] forms)
            {
                foreach (MagneticForm f in forms)
                {
                    this.SetForms(f); 
                }
            }
            /// <summary>
            /// 构造函数
            /// </summary>
            public MagneticForm(bool sign)
            {
                this.MoveSelf = sign; 
                InitializeComponent();
            }
            private void InitializeComponent()
            {
                this.SuspendLayout();
                // 
                // MagneticForm
                // 
                this.ClientSize = new System.Drawing.Size(292, 266);
                this.Name = "MagneticForm";
                this.ClientSizeChanged += new System.EventHandler(this.MagneticForm_ClientSizeChanged);
                this.Move += new System.EventHandler(this.MagneticForm_Move);
                this.ResumeLayout(false);            }
            /// <summary>
            /// 自身窗体移动事件函数
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void MagneticForm_Move(object sender, EventArgs e)
            {
                this.LeftX = this.Location.X;
                this.RightX = this.Location.X + this.Width;
                this.TopY = this.Location.Y;
                this.BottomY = this.Location.Y + this.Height;
                OthersFormMoving();
            }
            

解决方案 »

  1.   

    /// <summary>
                /// 设置其他窗体移动
                /// </summary>
                private void OthersFormMoving()
                {
                    foreach (MagneticForm form in this.FormList)
                    {
                         HorizonLength = 0;
                         VerticalLength = 0;
                         ///判断是从哪个方向靠近费了很大力气没有上面两个变量就没法区分发生重叠时候从哪个方向靠近                    //从左边靠近
                        if ((this.RightX >= form.LeftX && this.leftX < form.LeftX) && !(this.TopY >= form.BottomY || this.BottomY <= form.TopY))
                        {                        //tmp.X = this.RightX+form.Width;方法一
                            //tmp.Y = this.TopY; 方法一
                            //form.Moving(tmp); 方法一                        //以下为方法二
                            VerticalLength = (this.BottomY < form.BottomY ? this.BottomY : form.BottomY) - (this.topY < form.TopY ? form.TopY : this.TopY); 
                            HorizonLength  =this.RightX-form.LeftX;
                            ///重叠的水平长度大于等垂直长度说明是从左靠近
                            if (VerticalLength >= HorizonLength)  //不成立则是上下靠近
                            {
                                //方法二 移动自己
                                if (this.MoveSelf == true)
                                {
                                    tmp.X = form.LeftX - this.Width;
                                    tmp.Y = form.TopY;
                                    this.Moving(tmp);
                                }
                                else //方法一移动其他的
                                {
                                    tmp.X = this.Left+this.Width;
                                    tmp.Y = this.TopY;
                                    form.Moving(tmp);                             }
                                continue;
                            }
                        }
                         
                        //从右边靠近
                        if ((this.RightX > form.RightX && this.leftX <= form.RightX) && !(this.TopY >= form.BottomY || this.BottomY <= form.TopY))
                        {
                            // 方法一
                            //tmp.X = this.LeftX - form.Width;
                            //tmp.Y = form.TopY;
                            //form.Moving(tmp); 方法一
                            // //方法二
                            VerticalLength = (this.BottomY < form.BottomY ? this.BottomY : form.BottomY) - (this.topY < form.TopY ? form.TopY : this.TopY);
                            HorizonLength = form.RightX - this.LeftX;
                            ///重叠的水平长度大于等垂直长度说明是从右靠近
                            if (VerticalLength >= HorizonLength)  //不成立则是上下靠近
                            {
                                if (this.MoveSelf) //方法二
                                {
                                    tmp.X = form.RightX;
                                    tmp.Y = form.TopY;
                                    this.Moving(tmp);
                                }
                                else //一
                                {
                                    tmp.X = this.LeftX - form.Width;
                                    tmp.Y = this.TopY;
                                    form.Moving(tmp);
                                }
                                continue;
                            }
                        }                    //往上边靠近
                        if ((this.TopY <= form.BottomY && this.BottomY > form.BottomY) && !(this.RightX <= form.LeftX || this.Left >= form.RightX))
                        {
                            //方法一
                            //tmp.X = this.LeftX;
                            //tmp.Y = this.BottomY;
                            //form.Moving(tmp);
                            //方法二
                            if (this.MoveSelf)
                            {
                                tmp.X = form.LeftX;
                                tmp.Y = form.BottomY;
                                this.Moving(tmp);
                            }
                            else
                            {
                                tmp.X = this.LeftX;
                                tmp.Y = this.TopY-form.Height;
                                form.Moving(tmp);
                            }
                            continue;
                        }                    //往下边靠近
                        if ((this.BottomY >= form.TopY && this.TopY < form.TopY) && !(this.RightX <= form.LeftX || this.Left >=form.RightX))
                        {
                            //方法一
                            //tmp.X = this.LeftX;
                            //tmp.Y = this.TopY-form.Height;
                            //form.Moving(tmp);                         //方法二
                            if (this.MoveSelf)
                            {
                                tmp.X = form.LeftX;
                                tmp.Y = form.TopY - this.Height;
                                this.Moving(tmp);
                            }
                            else
                            {
                                tmp.X = this.LeftX;
                                tmp.Y = this.BottomY; 
                                form.Moving(tmp); 
                            }
                            continue;
                        }
                         /**/
                        //左右翻转模式 得把上面的全 注释了
                        //if ((this.RightX >= form.LeftX) && !(this.TopY > form.BottomY || this.BottomY < form.TopY))
                        //{
                        //    tmp.X = this.RightX;
                        //    tmp.Y = this.TopY;
                        //    form.Moving(tmp);
                        //    continue;
                        //}
                    }
                }
                /// <summary>
                ///  窗体移动
                /// </summary>
                /// <param name="pt">要移动到的坐标,左上角</param>
                public void Moving(Point pt)
                {
                    //方法二移动自己
                    if (this.MoveSelf)
                    {
                        this.Location = pt;
                    }
                    else
                    {
                        //方法一移动其他窗体如果不这样先处理的话会造成死锁。
                        this.Move -= new System.EventHandler(this.MagneticForm_Move);
                        this.Location = pt;
                        this.Move += new System.EventHandler(this.MagneticForm_Move);
                    }               
                }
                /// <summary>
                /// 更改窗体大小时候的事件
                /// </summary>
                /// <param name="sender"></param>
                /// <param name="e"></param>
                private void MagneticForm_ClientSizeChanged(object sender, EventArgs e)
                {
                    this.LeftX = this.Location.X;
                    this.RightX = this.Location.X + this.Width;
                    this.TopY = this.Location.Y;
                    this.BottomY = this.Location.Y + this.Height;
                }
            }
        }测试的显示类
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;  namespace FormMoving
    {
        class AllForm:Form 
        {
            //public   Form1 form1 = new Form1();
            //public   Form2 form2 = new Form2();
            //public static  int form1_x = 0; //窗体form1 的 x坐标
            //public static  int form2_x = 0; //窗体form2 的 x坐标        
            public AllForm()
            {
                int i = 0;
                MagneticForm[] Myforms = new MagneticForm[10];
                for (int m = 0; m < Myforms.Length; m++)
                {
                    Myforms[m] = new MagneticForm(false);  
                }
                    foreach (MagneticForm form in Myforms)
                    {
                        form.Text = "--" + (i++).ToString();
                        form.SetFormsArray(Myforms);
                    }          for (int j = 0; j < Myforms.Length; j++)
                  Myforms[j].Show();  
             
            
            
            }
                }
    }就是在一个 Form里面 定义一群 MagneticForm  然后运行
    当然还有不少缺陷,例如 窗体容易跑到 超出桌面的范围,用自动自身的方式 当  移动到两个窗体之间时就发生死循环了,这个回头再改进,如果一个窗体显示一张图片,并且设置成无边框的透明窗体,再加上缓慢的移动控制效果应该不错,开10个窗体cpu 能到 5% 还不错... 
      

  2.   

    好玩的东西   设置 MagneticForm[] Myforms = new MagneticForm[2]; 就明白了
      

  3.   

     晚上回去弄个 检测 是否超出桌面范围了 form 一多  都跑外面去了  郁闷
      

  4.   

    上面 就是 源代码了  难道还得弄个 Demon
      

  5.   

    弄了个 Demo 
     http://download.csdn.net/source/2653934
    差点不知道怎么上传资源了  那个上传资源按钮做的真小 BS下CSDN, 没下载分了,所以收取1分,嘎嘎
      

  6.   

     改进
    1 添加 MoveOver事件响应
    2 添加location重叠检测
    3 添加设置吸引距离
    4 添加桌面边缘检测
    5 添加靠边自动伸缩
      

  7.   

    有意思,给我留言 上班就来看了 呵呵 不过我已经做好了 
    现在也没时间做WinForm