C#WinForm中⽀持透明的TextBox控件
WinForm 的 TextBox不⽀持透明背景⾊,设置背景⾊透明会报错:“控件不⽀持透明的背景⾊”。
解决⽅法⼀:(测试可⽤)
public class TransTextBox : RichTextBox
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
static extern IntPtr LoadLibrary(string lpFileName);
textbox控件边框设置protected override CreateParams CreateParams
{
get
{
CreateParams prams = base.CreateParams;
if (LoadLibrary("msftedit.dll") != IntPtr.Zero)
{
prams.ExStyle |= 0x020; // transparent
prams.ClassName = "RICHEDIT50W";// TRANSTEXTBOXW
}
return prams;
}
}
}
因为是派⽣⾃RichTextBox,所以若想仿照TextBox,还需要在派⽣控件的构造函数中设置:
this.Multiline = false;
×另,此⽅法有个可能出现的问题,若此控件下存在背景图⽚容器(如:PictureBox),会发现输⼊后再删除时⽂字会残留:⽬前我是通过给此派⽣控件添加事件函数来刷新界⾯解决的,如果有更好的⽅法,欢迎告诉我:
this.TextChanged += new System.EventHandler(this.TransTextBox_TextChanged);
this.LostFocus += new EventHandler(this.TransTextBox_LostFocus);
private void TransTextBox_LostFocus(object sender, EventArgs e)
{
this.Parent.Refresh();
}
private void TransTextBox_TextChanged(object sender, EventArgs e)
{
this.Parent.Refresh();
}
解决⽅法⼆:(测试不可⽤)
class TransTextBox : TextBox
{
public TransTextBox() : base()
{
this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.UserPaint, true); base.BackColor = System.Drawing.Color.Transparent;
this.UpdateStyles();
}
}
如果此⽅法我使⽤⽅式有什么问题,请告诉我~
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论