我为透明文本框制作了这个课程
public partial class TransTextBox : TextBox
{
public TransTextBox()
{
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
}
}
但是当我离开文本框时,文本消失但仍然存在。如何解决?
答案 0 :(得分:0)
您有两种选择。
您可以创建一个包含TextBox
和Label
的对象,焦点会进入TextBox
,隐藏Label
,当焦点离开{{1}时},显示TextBox
。这将立即修复您当前的设置。
我更直接的方法可能是这样的:
Label
但如果背景颜色发生变化,则需要再次调用。
答案 1 :(得分:0)
您可以执行以下操作:删除ControlStyles.OptimizedDoubleBuffer
标记,然后在DrawString
事件OnPaint
上重新标记文字
这样的事情:
public partial class TransTextBox : TextBox {
public TransTextBox() {
SetStyle(ControlStyles.SupportsTransparentBackColor |
//ControlStyles.OptimizedDoubleBuffer | //comment this flag out
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
}
private void redrawText() {
using (Graphics graphics = this.CreateGraphics())
using (SolidBrush brush = new SolidBrush(this.ForeColor))
graphics.DrawString(this.Text, this.Font, brush, 1, 1); //play around with how you draw string more to suit your original
}
protected override void OnPaint(PaintEventArgs e) {
base.OnPaint(e);
redrawText();
}
}
如果您使用DoubleBuffer
,即使您重新绘制字符串,您的字符串也会被双重“删除”。