2016-07-18 1 views
0

지금 버튼을 클릭하면 .png (자동차)로 채워진 새로운 그림 상자가 생성됩니다. 인스턴스화 된 후 마우스로 자동차를 움직이고 싶습니다. 어떻게해야 할지를 알 수 없습니다. 나는 이미 스크린 상에있는 그림 박스를 드래그하는 방법을 이해하지만, 프로그래밍 방식으로 생성 된 것은 아니다.마우스로 인스턴스화 된 객체 이동하기 (Visual Studio C#)

public void CreatePatrolCar() 
    { 
     int picX = Properties.Resources.police.Width; 
     int picY = Properties.Resources.police.Height; 


     PictureBox pc = new PictureBox(); 
     pc.Image = Properties.Resources.police; 
     pc.Size = new Size(picX/3, picY/3); 
     pc.SizeMode = PictureBoxSizeMode.StretchImage; 
     pc.Location = new Point(100, 100); 

     Controls.Add(pc);    
    } 
+0

_ "C#을 Visual Basic에서"_? – MickyD

+0

나는 그것을 잘못 썼다. 좌절하고주의를 기울이지 않았다. C#을 사용하는 Visual Studio. – Rob

답변

0

나는 어제이 작업을하고있었습니다. 이것이 최선의 방법인지는 모르겠지만 작동합니다. 드래그를 시작하기 위해 control + mousedown 이벤트를 사용합니다.

자식 컨트롤이 만들어지면 아래에 마우스 이벤트 처리기를 추가합니다.

private void btnNotes_Click(object sender, EventArgs e) 
    { 
     if (_editor == null) 
     { 
      _editor = new VimControl(); 
      _editor.Location = new Point(400, 200); 
      _editor.Size = _editor.MinimumSize; 
      _editor.vimTextBox.Text = "Hello World!"; 
      _editor.vimTextBox.MouseDown += HandleMouseDown; 
      _editor.vimTextBox.MouseMove += HandleMouseMove; 
      _editor.vimTextBox.MouseUp += HandleMouseUp; 
      this.SuspendLayout(); 
      this.Controls.Add(_editor); 
      _editor.BringToFront(); 
      this.ResumeLayout(true); 
     } 

     _editor.Show(); 


    } 

#region Drag Child 
private Point? _mouseDown = null; 
private Point? _mouseLast = null; 
private Control frame = null; 

/// <summary> 
/// Control click to begin drag child. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void HandleMouseDown(object sender, MouseEventArgs e) 
{ 
    var child = sender as Control; 
    Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name); 

    Log.Message("{0} MouseDown at ({1}, {2})", e.Button, e.X, e.Y); 
    if (e.Button == MouseButtons.Left && (Control.ModifierKeys & Keys.Control) == Keys.Control) 
    { 
     _mouseDown = e.Location; 
     _mouseLast = e.Location; 
     frame = FormHelper.FrameControl(_editor.Size, _editor.Location); 
     this.Controls.Add(frame); 
     frame.BringToFront(); 
    } 
} 

private void HandleMouseMove(object sender, MouseEventArgs e) 
{ 
    var child = sender as Control; 
    Log.Message("Sender: {0}", child == null ? "Unknown" : child.Name); 

    if (child == null) return; 

    if (_mouseDown.HasValue) 
    { 
     Point delta = MyMath.Delta(_mouseLast.Value, e.Location); 
     frame.Left = frame.Left + delta.X; 
     frame.Top = frame.Top + delta.Y; 
     _mouseLast = e.Location; 
    } 
} 

private void HandleMouseUp(object sender, MouseEventArgs e) 
{ 
    var child = sender as Control; 
    Log.Message("My {0} MouseUp at ({1}, {2})", e.Button, e.X, e.Y); 
    if (e.Button == MouseButtons.Left && _mouseDown.HasValue) 
    { 
     _mouseDown = null; 
     _mouseLast = e.Location; 
     this.SuspendLayout(); 
     { 
      child.Location = frame.Location; 
      this.Controls.Remove(frame); 
      frame.Dispose(); 
      frame = null; 
     } 
     this.ResumeLayout(true); 
     child.Show(); 
    } 
} 
#endregion 

프레임 컨트롤은 드래그하는 동안 아이 에 대한의 의미 단지 빈 사용자 컨트롤입니다. 그것은 끌기를 매우 매끄럽게 만듭니다.

public static UserControl FrameControl(Size size, Point location) 
    { 
     UserControl frame = new UserControl(); 
     frame.Location = location; 
     frame.Size = size; 
     frame.BorderStyle = BorderStyle.Fixed3D; 
     frame.BackColor = Color.Transparent; 

     return frame; 
    } 

델타 도우미 기능은 무엇입니까

public static Point Delta(Point p1, Point p2) 
{ 
    return new Point(p2.X - p1.X, p2.Y - p1.Y); 
}