2016-07-18 12 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); 
} 
関連する問題