본문 바로가기
2_ 바삭바삭 프로그래밍/C# and Visual C++

[C#] 폼 드래그 할때 이동

by 준환이형님_ 2012. 10. 5.

using System.Drawing;


// 오버라이딩이므로 이벤트를 별도 생성하실 필요 없이 코드에 붙이시면 됩니다

// MouseDown 시의 커서 위치를 저장해서, MouseMove 시에 Form의 Location에 반영하게 됩니다

 

        private Point mCurrentPosition = new Point(0, 0); //

   

       protected override void OnMouseDown(MouseEventArgs e)

        {

            base.OnMouseDown(e);

            if(e.Button == MouseButtons.Left)

                mCurrentPosition = new Point(-e.X, -e.Y);


        }


        protected override void OnMouseMove(MouseEventArgs e)

        {

            base.OnMouseMove(e);

            if (e.Button == MouseButtons.Left)

            {   

                this.Location = new Point(

                    this.Location.X + (mCurrentPosition.X + e.X),

                    this.Location.Y + (mCurrentPosition.Y + e.Y));// 마우스의 이동치를 Form Location에 반영한다.

            }

        }