如何在C#中构建图片框?

时间:2015-08-08 00:41:27

标签: c# visual-studio-2015

所以即时创建一个flappy bird程序,我想做的就是调用这个方法在屏幕上创建一个管道。我实际上看起来很麻烦。 我如何调用我的方法创建管道,我的方法实际上会创建一个管道?

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FlappyBird
{
    public partial class Form1 : Form
    {

        int yspeed = 0;
        int xspeed = 1;

        PictureBox pipe = new PictureBox();


        private void CreatePipe()
        {

            this.pipe = new PictureBox();

            this.pipe.Location = new Point(2, 2);
            this.pipe.Name = "pipe";
            this.pipe.Size = new Size(200, 200);
            this.pipe.TabIndex = 0;
            this.pipe.TabStop = false;
            this.pipe.BackColor = Color.Red;
            this.pipe.Visible = true;


        }





        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            MessageBox.Show("Press okay to start.");
            timer1.Enabled = true;
            CreatePipe();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            yspeed += xspeed;
            bird.Top += yspeed;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode==Keys.Space) {
                yspeed = -15;
            }
        }




        private void fileToolStripMenuItem_Click(object sender, EventArgs e)//new game
        {

        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)//about
        {

        }



    }

    }

1 个答案:

答案 0 :(得分:2)

您需要在表单上放置PictureBox,否则它会保留在内存中但由于没有父级而无法显示,因此可以调用:

this.Controls.Add(pipe);
相关问题