【问题标题】:Windows Forms Transparent TextBox C# Existing solutions don't workWindows 窗体透明文本框 C# 现有解决方案不起作用
【发布时间】:2016-02-04 16:29:36
【问题描述】:

我在一个即时构建表单窗口的控制台应用程序中以编程方式创建我的文本框。我试图让输入框(例如文本框)显示为不可见,但仍允许用户输入数据,例如用户名和密码或我提供的任何其他自定义字段。这是一个游戏启动器,我试图让它看起来不像一个 Windows 组件。 我已经尝试了下面帖子中的一些解决方案。

Transparency for windows forms textbox

编辑:正如您在上面看到的,我已经提到这并不能解决我的问题。我不使用表单设计器,因为它有删除我的代码的讨厌习惯,因为我认为“它知道得更好”。

接受的答案对我不起作用,因为我不使用表单设计器和InitializeComponent(); 不起作用它只是告诉我它不是组件的功能。 我已经做到了这一点。

using System.Windows.Forms;

namespace Launcher_Namespace
{
    public class TransparentTextBox : TextBox
    {
        public TransparentTextBox()
        {
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
    }
}

在初始化字段的代码主体中

            //Initialise Inputs
            _username = new TransparentTextBox();
            _username.Bounds = new Rectangle(120, 10, 120, 21);
            _username.BackColor = Color.Transparent;
            _username.BorderStyle = 0;
            _username.Visible = false;

但这一切已经实现的是允许我设置_username.BackColor = Color.Transparent; 而不抛出错误。输入框保持白色,没有边框。我只想让背景透明。甚至 MSDN 也推荐此解决方案,但它对我不起作用。我剩下的唯一解决方案是构建一个自定义 Label 类,该类获取输入并读取关键输入并将它们添加到 .Text 属性,但我不想这样做。

【问题讨论】:

  • 这不是重复的。
  • 聊天被冻结在加载面板上。

标签: c# winforms textbox transparency


【解决方案1】:

链接答案中的解决方案工作正常。如果您不使用设计器,那没关系……您仍然可以使用相同的解决方案。 InitializeComponent() 只是一个由设计器文件中的代码生成器创建的方法。如果您想知道创建控件的作用(查看它可能会提供非常丰富的信息),请使用设计器创建控件,然后检查.Designer.cs 文件。

编辑:它的行为有点滑稽。您可以覆盖OnPaint 来修复白色背景和消失的文本,见下文。不是“完成”的实现,光标似乎不知道去哪里,但这应该会让你朝着正确的方向前进。

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            for (int i = 0; i < 3; i++)
            {
                var x = new UserControl1 {Location = new Point(0, i*20)};
                this.Controls.Add(x);
            }
        }
    }

    public  class UserControl1 : TextBox
    {
        public UserControl1()
        {
            SetStyle(ControlStyles.SupportsTransparentBackColor |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.ResizeRedraw |
                 ControlStyles.UserPaint, true);
            BackColor = Color.Transparent;
            TextChanged += UserControl2_OnTextChanged;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            var backgroundBrush = new SolidBrush(Color.Transparent);
            Graphics g = e.Graphics;
            g.FillRectangle(backgroundBrush, 0, 0, this.Width, this.Height);          
            g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(0,0), StringFormat.GenericDefault);
        }

        public void UserControl2_OnTextChanged(object sender, EventArgs e)
        {
            Invalidate();
        }
    }
}

【讨论】:

  • 这会使这个问题重复。 =D
  • 当然。我会在一分钟内投票结束作为一个骗子。只是想尝试帮助这个可怜的家伙,他听起来很沮丧。
  • 如果它是重复的,我就不需要问这个问题了。显然,这里的区别在于解决方案与其他线程中的解决方案不同。这不是同一个问题。
  • 这行得通,但只要我输入,背景就会再次变白。它有时会再次变得透明,但所有文本都变得不可见。又是java了。我只是要建立一个自定义标签来完成这项工作。这不能正常工作。
  • 如果可以的话,你可以试试 WPF 项目。我正在寻找白色背景是否有解决方法。
【解决方案2】:

当我们使用 SetStyle(ControlStyles.UserPaint,true) 控件边框不绘制。我在文本框中做了这个。我的文本框边框样式是 FixedSingle 但是在使用 setstyle 和 UserPaint 后,文本框边框没有被绘制。文本框看起来像边框设置为无。

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 2016-10-19
    • 2018-12-03
    • 2015-04-17
    • 1970-01-01
    • 2011-01-03
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多