2013-08-20 17 views
2

我正在開發一個系統,允許用戶在同一個面板內拖動對象,我經歷了一些研究並發現我應該使用鼠標事件,如mouse_up,mouse_down和mouse_move。C#拖動面板周圍的控件

該程序將生成3個圖片框,並允許用戶拖動面板內的每個圖片框,但程序我的代碼並不完美,因爲當我拖動圖片框時,圖片框會移動,但不會按照到我的鼠標光標位置,這是另一個地方,除了拖動時,面板上有picturebox陰影,我試過update(),refresh()和invalidate(),但它對我來說似乎沒有用。下面是我的代碼,感謝幫助

public partial class Form1 : Form 
{ 

    List<PictureBox> pictureBoxList = new List<PictureBox>(); 
    private bool isDragging = false; 

    public Form1() 
    { 
     InitializeComponent(); 

     for (int i = 0; i < 3; i++) 
     { 
      PictureBox picture = new PictureBox 
      { 
       Name = "pictureBox" + i, 
       Size = new Size(20, 20), 
       Location = new Point(i * 40, i * 40), 
       BorderStyle = BorderStyle.FixedSingle, 
       SizeMode = PictureBoxSizeMode.Zoom, 
       ImageLocation = "A.jpg" 
      }; 
      pictureBoxList.Add(picture); 


      foreach (PictureBox p in pictureBoxList) 
      { 
       p.MouseDown += new MouseEventHandler(c_MouseDown); 
       p.MouseMove += new MouseEventHandler(c_MouseMove); 
       p.MouseUp += new MouseEventHandler(c_MouseUp); 
       pnlDisplayImage.Controls.Add(p); 
       pnlDisplayImage.Refresh(); 
      } 
     } 
    } 


    void c_MouseDown(object sender, MouseEventArgs e) 
    { 
     isDragging = true; 
    } 

    void c_MouseMove(object sender, MouseEventArgs e) 
    { 

     if (isDragging == true) { 
      Control c = sender as Control; 
      for (int i = 0; i < pictureBoxList.Count(); i++) 
      { 
       if (c.Equals(pictureBoxList[i])) 
       { 
        pictureBoxList[i].Location = new Point(e.X, e.Y); 
       } 
      } 
     } 
    } 

    void c_MouseUp(object sender, MouseEventArgs e) 
    { 
     PictureBox c = sender as PictureBox; 
     isDragging = false; 
     for (int i = 0; i < pictureBoxList.Count(); i++) { 
      if (c.Equals(pictureBoxList[i])){ 
       pictureBoxList[i].Location = new Point(e.X, e.Y); 
      } 
     } 
    } 

    private void pnlDisplayImage_Paint(object sender, PaintEventArgs e) 
    { 
     foreach (PictureBox p in pictureBoxList) 
     { 
      pnlDisplayImage.Controls.Add(p); 
     } 
    } 

} 

回答

2

最後我發現哪些導致我的程序無法按我的期望運行的問題。主要的問題是我不小心把foreach循環放到了我用來創建pictureBox的for循環中,這個問題導致pictureBox在運行時拖動時出現了一些陰影效果,因爲有幾個相同的pictureBox。此外,我已經修改了一些代碼,現在它按照我的預期運行。以下是我想要回答的代碼。

public partial class Form1 : Form 
{ 

    List<PictureBox> pictureBoxList = new List<PictureBox>(); 
    private bool isDragging = false; 
    Point move; 

    public Form1() 
    { 
     InitializeComponent(); 

     for (int i = 0; i < 3; i++) 
     { 
      PictureBox picture = new PictureBox 
      { 
       Name = "pictureBox" + i, 
       Size = new Size(20, 20), 
       Location = new Point(i * 40, i * 40), 
       BorderStyle = BorderStyle.FixedSingle, 
       SizeMode = PictureBoxSizeMode.Zoom, 
       ImageLocation = "A.jpg" 
      }; 
      pictureBoxList.Add(picture); 
     } 

     foreach (PictureBox p in pictureBoxList) 
     { 
       p.MouseDown += new MouseEventHandler(c_MouseDown); 
       p.MouseMove += new MouseEventHandler(c_MouseMove); 
       p.MouseUp += new MouseEventHandler(c_MouseUp); 
       pnlDisplayImage.Controls.Add(p); 
       pnlDisplayImage.Refresh(); 
     } 

    } 

    void c_MouseDown(object sender, MouseEventArgs e) 
    { 
     Control c = sender as Control; 
     isDragging = true; 
     move = e.Location; 
    } 

    void c_MouseMove(object sender, MouseEventArgs e) 
    { 

     if (isDragging == true) { 
      Control c = sender as Control; 
      for (int i = 0; i < pictureBoxList.Count(); i++) 
      { 
       if (c.Equals(pictureBoxList[i])) 
       { 
        pictureBoxList[i].Left += e.X - move.X; 
        pictureBoxList[i].Top += e.Y - move.Y; 
       } 
      } 
     } 
    } 

    void c_MouseUp(object sender, MouseEventArgs e) 
    { 
     isDragging = false; 
    } 
} 
0

嘗試類似的東西(它與覆蓋自定義控件,但應該很容易轉換爲事件):

private bool _isMoved = false; // true if move mode on 
    private Point _pointMove = new Point(0); // for moving 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     // if left button pressed 
     if(e.Button == MouseButtons.Left) 
     { 
      _pointMove.X = e.X; 
      _pointMove.Y = e.Y; 
      _isMoved = true; 
      Cursor = Cursors.SizeAll; 
      Capture = true; 
     } 
     base.OnMouseDown (e); 
    } 

    protected override void OnMouseUp(MouseEventArgs e) 
    { 
     // if move mode on 
     if(_isMoved) 
     { 
      _isMoved = false; 
      Cursor = Cursors.Default; 
      Capture = false; 
     } 
     base.OnMouseUp (e); 
    } 

    protected override void OnMouseMove(MouseEventArgs e) 
    { 
     // if move mode on 
     if (_isMoved) 
     { 
      Left += e.X - _pointMove.X; 
      Top += e.Y - _pointMove.Y; 
     } 
     base.OnMouseMove (e); 
    } 
+0

嗨Sinatr,謝謝你的回答,我試着將你的代碼轉換成我的情況,它給了我相同的結果,只是鼠標光標改變了。 我在想是不是我的pnlDisplayImage_Paint事件導致問題 –