2014-09-01 40 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