소년포비의 세계정복!!

[C#] 콘트롤 마우스 클릭후 드래그 이동시키기 본문

프로그램 세상/C#

[C#] 콘트롤 마우스 클릭후 드래그 이동시키기

소년포비 2009. 10. 24. 02:03

C# 콘트롤 마우스 클릭후 드래그 이동시키기

private bool        bDrag;
private Point        startPos;

private void my_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                        if (e.Button == MouseButtons.Left && bDrag == false)
                        {
                                startPos.X = e.X;
                                startPos.Y = e.Y;

                                SetCapture (Handle);
                                bDrag = true;
                        }
                }

                private void my_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                        if (e.Button == MouseButtons.Left && bDrag == true)
                        {
                                this.Left += e.X - startPos.X;
                                this.Top += e.Y - startPos.Y;
                        }
                }

                private void my_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
                {
                        if (e.Button == MouseButtons.Left && bDrag == true)
                        {
                                ReleaseCapture ();
                                bDrag = false;
                        }                
                }

                [DllImport("user32")]
                public static extern IntPtr SetCapture(IntPtr hWnd);

                [DllImport("user32")]
                public static extern bool ReleaseCapture();