Windows窗体文本框的透明度 [英] Transparency for windows forms textbox

查看:20
本文介绍了Windows窗体文本框的透明度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 C# 中使用 Windows 窗体,我需要使文本框的背景颜色透明.我有一个从 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.

这是我目前拥有的代码:

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?

上一个问题,这位好人说了一些关于SetStyle(ControlStyles.SupportsTransparentBackColor, true);,但我不知道该放在哪里.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆