我的表单有背景图片。我有一些用作按钮的图片框。我正在尝试使用 MouseEnter/MouseLeave 事件来显示标签或图片框。
我一直在尝试各种方法。我得到了类似的结果——标签或图片框看起来很好,但在 MouseLeave 上,label1.Visible = false;在表单的背景图像上导致一个非常临时的空白框。虽然它是全功能的,但它看起来只是一个非常轻微的延迟,但让程序看起来很糟糕。
我试验了 DrawString 方法。这似乎是一个不错的选择,但我不知道如何在 MouseLeave 事件中删除对象。
这可能吗?如果没有,是否有更好的选择来完成我想要完成的任务?
这是我绘制字符串的方式(在用于测试的 buttonClick 事件中):
Graphics g = this.CreateGraphics();
string letter = "Yo Dawg!";
g.DrawString(letter, new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular),
new SolidBrush(Color.Black), 100, 100);
最佳答案
您将在绘制事件中绘制,在 MouseLeave
中设置一个标志,使用 Invalidate()
进行绘制,如果未设置标志则在绘制中画任何东西。
public partial class TheForm : Form
{
private Font _font = new Font(FontFamily.GenericSansSerif, 20, FontStyle.Regular);
private bool _hovering = false;
public TheForm() {
InitializeComponent();
picBox.Paint += new PaintEventHandler(picBox_Paint);
picBox.MouseEnter += (sender, e) => UpdateText(true);
picBox.MouseLeave += (sender, e) => UpdateText(false);
}
private void picBox_Paint(object sender, PaintEventArgs e) {
if (_hovering)
e.Graphics.DrawString("Yo Dawg!", _font, Brushes.Black, 100, 100);
}
private void UpdateText(bool show) {
_hovering = show;
picBox.Invalidate();
}
}
关于c# - 如何在 C# WinForm 中删除/删除/撤消 DrawString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32097439/