2014-09-01 81 views
-1

我有一个窗口窗体应用程序中的图片框,可以使用箭头键移动。我希望它有一定的限制,特别是在表格中。我该怎么做呢?我的类移动目标低于:C#在form1中保存图片框

命名空间AmazingPaintball { 类目标 {
私人点p;

public Target(Point myPoi) 
    {   
     p = myPoi;  
    } 

    public Point Move(Keys key) 
    {    
      if (key == Keys.Left) 
      { 
       p.Offset(-50, 0); 
      } 
      else if (key == Keys.Right) 
      { 
       p.Offset(50, 0); 
      } 
      else if (key == Keys.Up) 
      { 
       p.Offset(0, -50); 
      } 
      else if (key == Keys.Down) 
      { 
       p.Offset(0, 50); 
      } 
     return p; 

    } 
} 

}

下面是在Form1:

命名空间AmazingPaintball {

public partial class Form1 : Form 
{ 
    Random positionX = new Random(); 
    Random positionY = new Random(); 
    Target einstein; 
    int count = 0; 
    Paintballs pBalls = new Paintballs(); 
    Stopwatch stopwatch = new Stopwatch(); 


    SoundPlayer wavPlayer = new SoundPlayer(@"G:\ChefBrohansPaintballFunNew\ChefBrohansPaintballFun\Resources\singlegunshot.wav"); 
    SoundPlayer wavPlayer2 = new SoundPlayer(@"G:\ChefBrohansPaintballFunNew\ChefBrohansPaintballFun\bin\Debug\Resources\Applause.wav"); 


    public Form1() 
    { 
     InitializeComponent(); 
     Point point = new Point(positionX.Next(0, 638), positionY.Next(0, 404)); 
     einstein = new Target(point); 
     ptrEinstein.Location = point;  

    } 


    private void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     pBalls.paint(e.Graphics); 

    } 

    private void Form1_KeyDown(object sender, KeyEventArgs e) 
    {    
     ptrEinstein.Location = einstein.Move(e.KeyData);       
     pictureBox1.Update(); 
     pictureBox1.Refresh();   

    } 




    private void pictureBox1_MouseClick(object sender, MouseEventArgs e) 
    { 
     wavPlayer.Play(); 
     pBalls.add(e.Location); 
     pictureBox1.Refresh();       
     count++; 
    }   

    private void Form1_Load(object sender, EventArgs e) 
    {      
     stopwatch.Start();   

    } 

    private void ptrEinstein_MouseClick(object sender, MouseEventArgs e) 
    { 
     count++;    
     ptrEinstein.Image = Properties.Resources.AlbertEinsteinTongue; 
     stopwatch.Stop();    
     wavPlayer2.Play(); 
     MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target"); 
     wavPlayer2.Stop();    
     ptrEinstein.Image = Properties.Resources.AlbertEinsteinFace;    
     count = 0;    
     stopwatch.Reset(); 
     stopwatch.Start(); 

    } 


} 

}

在PictureBox是ptrEinstein,它能够在form1_keydown移动事件。

+0

可能有助于显示您用来移动PictureBox的代码。 – 2014-09-01 21:44:02

+0

我编辑了帖子,让它有代码。 – ChefBrohan 2014-09-01 21:59:11

回答

0

当您移动图片框时,首先检查以确定您将图片移动到的位置在表单内。例如。检查新的X +图片框的宽度是否小于表格的宽度等。

+0

它能够在form1中移动,但例如,如果我按住右箭头键,它将继续沿着该方向前进并脱离表单本身。 – ChefBrohan 2014-09-01 21:44:04

+0

为了能够给出更详细的答案,我需要看看移动图片框的代码。答案是一样的:在实际移动图片框之前,首先检查它将移动到哪里是有效的(不是关闭表格) – OSborn 2014-09-01 21:46:34

+0

我编辑了帖子,使其具有代码。 – ChefBrohan 2014-09-01 21:59:32

0

这将是不完整的,因为我们没有您的代码,但您应该将clientSize属性与picturebox位置进行比较(考虑到picturebox的大小):

PictureBox pb = new PictureBox(); 

int newX = oldX + xOffset; // xOffset is whatever you're incrementing x by 
int newY = oldY + yOffset; // yOffset is whatever you're incrementing y by 


if (newX < 0) { 
    newX = 0; 
} else if (newX > this.ClientSize.Width - pb.Width) { 
    newX = this.ClientSize.Width - pb.Width; 
} 

if (newY < 0) { 
    newY = 0; 
} else if (newY > this.ClientSize.Height - pb.Height) { 
    newY = this.ClientSize.Height - pb.Height; 
} 

// New point to move it to 
Point newP = new Point(newX, newY); 
+0

还需要处理newX <0或newY <0. – OSborn 2014-09-01 22:15:39

+0

@OSBON良好的通话,我编辑了代码。 – Chad 2014-09-01 22:29:56