用鼠标点击picturebox ptr爱因斯坦的事件

时间:2014-08-31 14:49:56

标签: c#

我在C#windows窗体应用程序中创建彩弹游戏。我想要它,所以当你左键点击目标(ptrEinstein)这是一个图片框时,它会出现一个消息框。这是我的表格1。

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时,消息框会显示出来。如果没有足够的信息,请告诉我。谢谢

1 个答案:

答案 0 :(得分:0)

如果你确定你有第二个名为" ptrEinstein"的图片框,那么你应该在 ptrEinstein_MouseClick 方法中写下面这一行,而不是在pictureBox1_MouseClick中写下来。 / p>

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

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

在“设计”页面中,选择ptrEinstein图片并从其属性中单击闪电符号,然后双击鼠标单击,它将为您添加必要的空方法,然后您可以在其中添加消息框。它看起来像:

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