c# - 在计时器刻度处绘制字符以形成
问题描述
我试图每 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++;
}
}
我希望最后的表格有表格上的字符串,但是让字符一个一个出现。表单不会显示任何文本。它只显示黑色背景。
解决方案
您需要使位图成为图片框的图像pb.Image = surface
。
public void DrawText()
{
device.DrawString(textChar[num].ToString(), font, Brushes.Red, 10, 10 + num * 22)
num++;
pb.Image = surface;
}
推荐阅读
- python - 如何根据所选选项从烧瓶中获取 Optgroup 表单值?
- node.js - 为什么我的应用程序不能在 Heroku 的预期端口上运行?
- javascript - 为什么js不能将方法作为函数运行
- angular - 将 Observable 流转换为数组
- c++ - 在合并排序算法中使用二分搜索合并步骤
- ios - FirebaseDatabase - 如何根据 userId 对帖子进行分页
- javascript - 触发孩子时如何更改父母的值?
- conv-neural-network - 了解 YOLOv3 CNN 中的锚点
- php - php exec函数在cpanel上不起作用
- php - 显示来自一对多关系的数据