Рисование символов для формирования в таймер - PullRequest
0 голосов
/ 24 июня 2019

Я пытаюсь нарисовать символ на экране каждые 500 миллисекунд, но он не будет отображаться на экране

using System;
using System.Drawing;
using System.Windows.Forms;

public partial class Form4 : Form
{
    public Form4()
    {
        InitializeComponent();
    }

    PictureBox pb;
    Bitmap surface;
    Graphics device;
    Timer timer;
    int num = 0;
    string text = "This is a test";
    char[] textChar;
    Font font = new Font("Black Ops One", 20, FontStyle.Regular, GraphicsUnit.Pixel);

    private void Form4_Load(object sender, EventArgs e)
    {
        //picture box
        pb = new PictureBox();
        pb.Parent = this;
        pb.Dock = DockStyle.Fill;
        pb.BackColor = Color.Black;
        //create graphics device
        surface = new Bitmap(this.Size.Width, this.Size.Height);
        pb.Image = surface;
        device = Graphics.FromImage(surface);
        //set up timer
        timer = new Timer();
        timer.Interval = 500;
        timer.Enabled = true;
        timer.Tick += new System.EventHandler(TimerTick);
        //mis
        textChar = text.ToCharArray();
    }

    public void TimerTick(object sender, EventArgs e)
    {
        DrawText();
        if (num > textChar.Length - 1) 
        {
           timer.Enabled = false;
           MessageBox.Show("have hit the end");
        }
    } 

    public void DrawText()
    {
        device.DrawString(textChar[num].ToString(), font, Brushes.Red, 10, 10 + num * 22)
        num++;
    }
}

Я надеюсь, что в конце формы есть строка в форме,но пусть персонажи придут один за другим.Форма не будет отображать текст.Показывает только черный фон.

1 Ответ

1 голос
/ 24 июня 2019

Вам нужно сделать растровое изображение изображением коробки pb.Image = surface.

public void DrawText()
    {
        device.DrawString(textChar[num].ToString(), font, Brushes.Red, 10, 10 + num * 22)
        num++;
        pb.Image = surface;
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...