简体   繁体   中英

My shape will appear for a few seconds

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

namespace WindowsFormsApplication1
{
public partial class Engine : Form
{
    int x, y;
    int w, h;

    Boolean running = false;

    public Engine()
    {
        InitializeComponent();
    }

    private void Engine_Load(object sender, EventArgs e)
    {

    }

    private void Engine_Paint(object sender, PaintEventArgs e)
    {

    }

    private void XTB_TextChanged(object sender, EventArgs e)
    {
        if (XTB.Text.Equals("10"))
        {
            x = 10;
        }

        if (XTB.Text.Equals("20"))
        {
            x = 20;
        }
    }

    private void YTB_TextChanged(object sender, EventArgs e)
    {
        if (YTB.Text.Equals("10"))
        {
            y = 10;
        }
    }

    private void WTB_TextChanged(object sender, EventArgs e)
    {
        if (WTB.Text.Equals("8"))
        {
            w = 8;
        }

        if (WTB.Text.Equals("16"))
        {
            w = 16;
        }

        if (WTB.Text.Equals("32"))
        {
            w = 32;
        }
    }

    private void HTB_TextChanged(object sender, EventArgs e)
    {
        if (HTB.Text.Equals("8"))
        {
            h = 8;
        }

        if (HTB.Text.Equals("16"))
        {
            h = 16;
        }

        if (HTB.Text.Equals("32"))
        {
            h = 32;
        }
    }

    private void drawShapeOnForm()
    {
        Graphics g = this.CreateGraphics();
        g.DrawRectangle(new Pen(Brushes.Blue), x, y, w, h);
        g.FillRectangle(Brushes.Blue, x, y, w, h);
    }

    //Button draws square
    private void COIN_Click(object sender, EventArgs e)
    {
        drawShapeOnForm();
    }

    private void Engine_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.A)
        {
            x--;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        running = true;

        if (running)
        {
            PlayerCreator.Enabled = false;
            EnemyCreator.Enabled = false;
        }
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        Invalidate();
    }
}
}

My shape only appears for a few secs when I press the button to make it, I'm not to sure how to fix this.

When I press the button it appears and then disappears, but if the function is put on the void Engine_Paint() it will work just fine. Please help me fix this problem.

You need to do your drawing in the Paint handler, using e.Graphics . Do not use CreateGraphics .

From your button click, you can just call this.Invalidate(); to trigger a repaint.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM