2014-08-31 61 views
0

我在C#窗体表单应用程序中创建了一个彩弹射击游戏。我需要它,所以当你左键点击作为图片框的目标(ptrEinstein)时,它会出现一个消息框。下面是我的form1。用鼠标点击图片框的事件ptrEinstein

namespace 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();   

    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) 
    {    
     pBalls.add(e.Location); 
     pictureBox1.Refresh();    



     if (ptrEinstein.Location == ??) 
     { 

      stopwatch.Stop(); 
      MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target");     
     } 
     count++; 
    }   

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

}

在picturebox1_click是问题似乎是。我需要它,当我点击ptrEinstein时,消息框出现。请让我知道如果没有足够的信息。谢谢

+0

请提供有关确切的错误或问题的信息。你想实现的目标是什么 – 2014-08-31 16:27:26

+0

你的意思是说__two__ pictureboxes?如果是这样,他们有__two distinct__ MouseClick事件,你会想要脚本。 – TaW 2014-08-31 16:35:03

回答

0

如果你真的确定你有一个名为“ptrEinstein”第二PictureBox的,那么你应该写在下面的ptrEinstein_MouseClick方法,而不是pictureBox1_MouseClick写它的线。

MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit the target"); 

使您在单击ptrEinstein时出现消息框。要自动添加正确的方法:

在Design页面中,选择ptrEinstein图片并从其Properties中单击闪电符号,然后双击MouseClick,它会为您添加必要的空方法,然后您可以在里面添加messagebox。它看起来像:

private void ptrEinstein_MouseClick(object sender, MouseEventArgs e) 
    {    
     MessageBox.Show("It took " + count + " shots and " + stopwatch.Elapsed + " seconds to hit 
    }