原创:wgscd

http://blog.csdn.net/wgsnet

 

两个窗体分别是FormMain FormList

 

第1个窗体:FormMain :

 

public partial class FormMain : Form
    {

 

      FormList formlist=null ;

 

        public FormMain()
        {

             FormMain fm = this;
             formlist = new FormList(ref fm);//传到第2个窗体去
          
              InitializeComponent();
        }

 

public bool isLinked ;
        private void FormMain_Load(object sender, EventArgs e)
        {
          
            Application.DoEvents();
            formlist.Show();
            isLinked = false;
            formlist.Left = this.Left + this.Width+20;//开始分开点,也可以不分开不要20
            formlist.Top = this.Top;
            this.Show();

}

 

 

private void FormMain_LocationChanged(object sender, EventArgs e)
        {

            int fltop = formlist.flTop;


            if (formlist == null) { return; }

            // 一起移2个窗体
            if (isLinked || Math.Abs(this.Left + this.Width - formlist.Left) <= 15)
            {

                formlist.Left = this.Left + this.Width;
               // formlist.Top = this.Top;
                   formlist.Top = this.Top - (fltop);


                isLinked = true;
                return;


            }


            isLinked = false;

 


        }

 

 

}

 

 

 

---------------------------------------------------------------------------------------------

第2个窗体:FormList

public partial class FormList : Form
    {
        public FormList()
        {
            InitializeComponent();

            ClassCommon.com_FormList = this;

        }
        public FormList(ref FormMain fm)
        {
            _fm = fm;//引用FormMain
            InitializeComponent();
        }
        FormMain _fm = null;

 

     public int flTop = 0;//记录两个窗体高度差
        private void FormList_LocationChanged(object sender, EventArgs e)
        {
            this.Text = this.Location.ToString();
            //if (_fm == null) { return; }
            flTop = _fm.Top - this.Top;
            if (Math.Abs(_fm.Left + _fm.Width - this.Left) <= 15)
            {

                this.Left = _fm.Left + _fm.Width;


                _fm.isLinked = true;

            }
            else
            {

                _fm.isLinked = false;
            }

 

 

        }

 

 

}

 

 

哈哈运行是不是很酷。。。。

别急,要千千静听那样就要设置无边框窗体,要实现拖动

OK ,用下面的加到2个窗体类里:

 

 

   #region "拖动窗体"
        private bool moveFlag = false;
        private int x = 0;
        private int y = 0;

        protected override void OnMouseMove(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag && (e.Button == MouseButtons.Left))
                this.SetBounds(Left + e.X - x, Top + e.Y - y, this.Width, this.Height);
            base.OnMouseMove(e);
        }

        protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
        {
            if (!moveFlag && e.Clicks >= 1)
                moveFlag = true;
            x = e.X;
            y = e.Y;
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(System.Windows.Forms.MouseEventArgs e)
        {
            if (moveFlag)
                moveFlag = false;
            base.OnMouseUp(e);
        }


        #endregion

自己把界面设计好看点,说不定你能比它还炫呵。

希望对大家有用。

Posted on 2009-12-04 15:58  LucasLee  阅读(918)  评论(0编辑  收藏  举报