在计时器刻度上绘制要形成的字符

时间:2019-06-24 01:19:01

标签: c# .net winforms

我试图每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 个答案:

答案 0 :(得分:1)

您需要使位图成为图片框pb.Image = surface的图像。

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