鼠标点击picturebox ptrEinstein的事件

本文关键字:事件 ptrEinstein picturebox 鼠标 | 更新日期: 2023-09-27 18:20:04

我正在C#窗体应用程序中创建一个彩弹游戏。我想要它,所以当你左键点击目标(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时,消息框就会出现。如果没有足够的信息,请告诉我。谢谢

鼠标点击picturebox ptrEinstein的事件

如果你真的确定你有第二个名为"ptrEinstein"的图片框,那么你应该用ptrEiinstein_MouseClick方法写下面的一行,而不是用pictureBox1_MouseClick写。

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 
    }