2012-11-08 2 views
1

양식을 클릭하여 드래그 할 수 있음을 알고 있습니다 this 하지만 코드를 사용하지 않고 자기 자신을 사용하고 싶다면 어떻게해야합니까? 내가 잘못 뭐하는 거지C# 양식을 클릭하고 끌기, 클래스

public partial class Form1 : Form 
{ 
    MouseDragFormMove dragForm; 

    public Form1() 
    { 
     InitializeComponent(); 

     dragForm = new MouseDragFormMove(this); 
     dragForm.AllowMouseDownDrag = true; 
    } 
}  
public class MouseDragFormMove 
{ 
    private bool _status; 
    public bool AllowMouseDownDrag 
    { 
     get { return _status; } 
     set { _status = value; } 
    } 

    private Form parent; 
    public MouseDragFormMove(Form self) 
    { 
     _status = false; 
     parent = self; 
     parent.MouseDown +=new MouseEventHandler(parent_MouseDown); 
     parent.MouseUp += new MouseEventHandler(parent_MouseUp); 
     parent.MouseMove +=new MouseEventHandler(parent_MouseMove);    
    } 

    public void showPos() 
    { 
     MessageBox.Show(parent.Location.X + ", " + parent.Location.Y); 
    } 

    private Point CPoint; 
    private Point MPoint; 
    private bool isDragging; 

    private void parent_MouseDown(object sender, MouseEventArgs e) 
    { 
     CPoint = parent.Location; 
     MPoint = getMousePoint(e, CPoint); 
     isDragging = true; 
    } 
    private void parent_MouseUp(object sender, MouseEventArgs e) 
    { 
     isDragging = false; 
    } 
    private void parent_MouseMove(object sender, MouseEventArgs e) 
    { 
     CPoint = parent.Location; 
     MPoint = getMousePoint(e, CPoint); 

     if (isDragging && _status) 
     { 
      parent.Location = MPoint; 
     } 
    } 
    private Point getMousePoint(MouseEventArgs e, Point FP) 
    { 
     int x = FP.X + (e.Location.X * 2); 
     int y = FP.Y + (e.Location.Y * 2); 

     return new Point(x, y); 
    } 
} 

:

이것은 내가 지금까지 가지고 무엇인가? 나는 그것을 작동시킬 수 없다. 또한 깜박입니다.

+0

깜박임이 다시 그려지는 창일 것 같습니다. – Amicable

답변

1

문제는 화면을 기준으로하지 않고 마우스 포인터를 폼을 기준으로 사용했다는 것입니다. 아래 내 솔루션을 참조하십시오. 마우스 화면 위치를 얻으려면 Cursor.Position을 사용하고 있습니다.

public class MouseDragFormMove 
{ 
    private bool _status; 
    public bool AllowMouseDownDrag 
    { 
     get { return _status; } 
     set { _status = value; } 
    } 

    private Form parent; 
    public MouseDragFormMove(Form self) 
    { 
     _status = false; 
     parent = self; 
     parent.MouseDown += new MouseEventHandler(parent_MouseDown); 
     parent.MouseUp += new MouseEventHandler(parent_MouseUp); 
     parent.MouseMove += new MouseEventHandler(parent_MouseMove); 
    } 

    private Point MPoint; 
    private bool isDragging; 
    private Point touchPoint; 

    private void parent_MouseDown(object sender, MouseEventArgs e) 
    { 
     isDragging = true; 

     // Capture the point relative to the form 
     touchPoint = e.Location; 
    } 
    private void parent_MouseUp(object sender, MouseEventArgs e) 
    { 
     isDragging = false; 
    } 

    private void parent_MouseMove(object sender, MouseEventArgs e) 
    { 
     MPoint = new Point(Cursor.Position.X - touchPoint.X, Cursor.Position.Y - touchPoint.Y); 

     if (isDragging && AllowMouseDownDrag && !parent.Location.Equals(MPoint)) 
     { 
      parent.Location = MPoint; 
     } 
    } 
} 
+1

그 일을 끝냈습니다. 감사! – Ben

관련 문제