为什么面板上的文字消失了? [英] Why does text drawn on a panel disappear?

查看:76
本文介绍了为什么面板上的文字消失了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在面板上绘制文本(面板上有背景图片)。

I'm trying to draw a text on a panel(The panel has a background picture).

它很好用,但是当我最小化然后最大化

It works brilliant,but when I minimize and then maximize the application the text is gone.

我的代码:

using (Graphics gfx = Panel1.CreateGraphics())
{
    gfx.DrawString("a", new Font("Tahoma", 5), Brushes.White, new PointF(1, 1));
}

如何使其保持静态,以免丢失? p>

How do I keep it static so it doesn't get lost?

推荐答案

如果您不使用 Paint 事件,则只是在绘图在控件恰好在屏幕上。控件没有意识到这一点,因此它不知道您打算让文本停留在那里...

If you don't use a Paint event, you are just drawing on the screen where the control happens to be. The control is not aware of this, so it has no idea that you intended for the text to stay there...

如果您要绘制的值放在上面面板中的 Tag 属性中,您可以对所有面板使用相同的绘制事件处理程序。

If you put the value that you want drawn on the panel in it's Tag property, you can use the same paint event handler for all the panels.

此外,您需要正确处理Font对象,否则在它们将资源返回给系统之前,您将有很多等待最终确定的对象。

Also, you need to dispose of the Font object properly, or you will be having a lot of them waiting to be finalized before they return their resources to the system.

private void panel1_Paint(object sender, PaintEventArgs e) {
   Control c = sender as Control;
   using (Font f = new Font("Tahoma", 5)) {
      e.Graphics.DrawString(c.Tag.ToString(), f, Brushes.White, new PointF(1, 1));
   }
}

这篇关于为什么面板上的文字消失了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆