공부/c#

c# 컨트롤 오브젝트 마우스 드래그 이동

령선 2021. 2. 24. 15:44
반응형
 private Point start_p;             // 클릭시 마우스 위치
 private Point end_p;               // 마우스 이동할때 위치
 private bool mouse_move = false;   // 마우스가 드레그 상태인지 확인

/// <summary>
/// 마우스 이동 이벤트
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Object_MouseMove(object sender, MouseEventArgs e)
{
	Point th;
    if (objmove)
    {
        // 마우스의 현재위치를 계산하기 위한 폼의 위치
        if (mouse_move)
        {
          th = this.Location;
          end_p = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
          Point tmp = new Point((((Control)sender).Location.X + (end_p.X - start_p.X)), ((Control)sender).Location.Y + (end_p.Y - start_p.Y));
          start_p = ((Control)sender).PointToScreen(new Point(e.X, e.Y));

          ((Control)sender).Location = tmp;
		}
	}
}
        
private void Object_MouseDown(object sender, MouseEventArgs e)
{
      mouse_move = true;
      start_p = ((Control)sender).PointToScreen(new Point(e.X, e.Y));
}

private void Object_MouseUp(object sender, MouseEventArgs e)
{
    mouse_move = false;
}

프로그램에 마우스 드래그 이동 기능이 필요 해서

 

내 검색 능력이 안 좋은건지 form이동이 더 많이 나오더라

 

일단 찾아서 기능 구현 해서 기록 해둔다

 

나 같은 경우는 드래그 이동이 필요한 오브젝트에 이벤트를 선언해서 사용하기 위해

(Control)sender 를 사용했다

 

ㅊㅊ : 항상 처음처럼~ :: C# 컨트롤을 마우스로 드래그로 이동할때 (tistory.com)

반응형