透明度Windows窗体的文本框 [英] Transparency for windows forms textbox
问题描述
我使用Windows窗体在C#中,我需要做一个文本框的背景颜色是透明的。我有一个跟踪条,从0到255应该控制它,但我遇到了一些麻烦。我创建了一个问题,今天早些时候,询问同样的事情,但没有成功。
I'm using windows forms in C# and I need to make a textbox's background color transparent. I have a trackbar that goes from 0 to 255 that is supposed to control it, but I'm having some trouble. I created a question earlier today asking the exact same thing, but no success.
下面是code我目前有:
Here is the code I currently have:
private void trackAlpha_ValueChanged(object sender, EventArgs e)
{
newColor = Color.FromArgb(trackAlpha.Value, colorDialog.Color.R, colorDialog.Color.G, colorDialog.Color.B);
colorDialog.Color = newColor; // The Windows dialog used to pick the colors
colorPreview.BackColor = newColor; // Textbox that I'm setting the background color
}
问题是,绝对没有任何反应。为什么任何想法是不工作?
The problem is that absolutely nothing happens. Any ideas on why this is not working?
在previous问题,这不错的家伙说了一些关于的SetStyle(ControlStyles.SupportsTransparentBackColor,真实);
,但我在哪里我应该把不知道这一点。
On the previous question, this nice guy said something about SetStyle(ControlStyles.SupportsTransparentBackColor, true);
, but I have no idea on where I should put this.
推荐答案
您需要尝试了这样的事情。
You need to try out something like this.
添加新的用户控件,说的 CustomTextBox 并变更
Add a new user control , say CustomTextBox and change
public partial class CustomTextBox : UserControl
到
public partial class CustomTextBox : TextBox
您再收到以下错误说,AutoScaleMode没有定义。删除Designer.cs类以下行。
You will then get the following error saying that the 'AutoScaleMode' is not defined. Delete the following line in the Designer.cs class.
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
请如下更改新添加控件的构造函数。
Make changes to the constructor of your newly added control as follows.
public partial class CustomTextBox : TextBox
{
public CustomTextBox()
{
InitializeComponent();
SetStyle(ControlStyles.SupportsTransparentBackColor |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.UserPaint, true);
BackColor = Color.Transparent;
}
}
构建,关闭自定义控制设计,如果打开,你将能够使用任何其他控件或窗体此控件。
Build, close the custom control designer if open and you will be able to use this control on any other control or form.
Drop it from the toolbox as shown below
这篇关于透明度Windows窗体的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!