首页 > 解决方案 > 在计时器刻度处绘制字符以形成

问题描述

我试图每 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++;
    }
}

我希望最后的表格有表格上的字符串,但是让字符一个一个出现。表单不会显示任何文本。它只显示黑色背景。

标签: c#.netwinforms

解决方案


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

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

推荐阅读