且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

透明度Windows窗体的文本框

更新时间:2023-12-06 16:19:22

您需要尝试了这样的事情。

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.

如下所示从工具箱删除它
透明度Windows窗体的文本框

Drop it from the toolbox as shown below