Windows 양식에 관해서는 실제로 붙여 넣기 만 복사하더라도
당신이 게시 한 코드는 실제로 그 자체로 아무것도하지 않을 것입니다!
작동하려면 컨트롤의 Mousedown () 및 MouseMove () 이벤트는 이러한 방법에 연결되어야합니다.
- 양식 (PictureBox1)에서 컨트롤을 선택하십시오.
- 속성 창 (기본적으로 오른쪽 하단)에서 "Lightning Bolt"아이콘을 클릭하여 해당 제어의 이벤트 목록을 얻으십시오.
- 찾아보세요
MouseDown
드롭 다운을 오른쪽으로 입력하고 변경하십시오.pictureBox1_MouseDown
. - 찾아보세요
MouseMove
드롭 다운을 오른쪽으로 입력하고 변경하십시오.pictureBox1_MouseMove
.
이제 실행하고 PictureBox1을 드래그하십시오.
편집 : 아래의 주석에 설명 된대로 여러 컨트롤에 대한 코드가 작동하는 방법은 다음과 같습니다.
bool isDragged = false;
Point ptOffset;
private void button1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isDragged = true;
Button btn = (Button)sender;
ptOffset = new Point(btn.Location.X - Cursor.Position.X, btn.Location.Y - Cursor.Position.Y);
}
else
{
isDragged = false;
}
}
private void button1_MouseMove(object sender, MouseEventArgs e)
{
if (isDragged)
{
Point newPoint = Cursor.Position;
newPoint.Offset(ptOffset);
Button btn = (Button)sender;
btn.Location = newPoint;
}
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
isDragged = false;
}