winforms “Windows窗体的透明度”文本框

jm2pwxwz  于 2022-12-23  发布在  Windows
关注(0)|答案(8)|浏览(170)

我正在使用C#中的windows窗体,我需要使一个文本框的背景色透明。我有一个从0到255的跟踪条,它应该可以控制它,但我遇到了一些麻烦。今天早些时候我创建了一个问题,问完全相同的事情,但没有成功。
下面是我目前拥有的代码:

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
}

问题是什么都没发生。你知道为什么这不起作用吗?
在上一个问题中,这个好人提到了一些关于SetStyle(ControlStyles.SupportsTransparentBackColor, true);的东西,但是我不知道应该把它放在哪里。

zi8p0yeb

zi8p0yeb1#

就这句台词,对我来说效果很好!

textBox1.BackColor = this.BackColor;

来源:https://www.codegrepper.com/code-examples/csharp/set+textbox+colour+to+transparent+c%23

bnlyeluc

bnlyeluc2#

晚上好,我知道现在回答这个问题已经太晚了,但是今天我面临着同样的问题。对于我来说,作为一个初学者,很难找到答案(而且开始学习WPF的发布日期已经不多了)。而上面的答案确实救了我。https://stackoverflow.com/a/16050862/18680342
但不幸的是,它有一个缺陷的形式消失的文字时,散焦。不久前,我设法找到了一个技巧来修复这个缺陷。我只是想分享它。这不是理想的,但为我工作。这里:https://stackoverflow.com/a/71896800/18680342

oprakyz7

oprakyz73#

SolidColorBrush br = new SolidColorBrush();
br.Color= Windows.UI.Color.FromArgb(0,0,0,0);
textbox.Background = br;

这对我来说是最有效的,不要忘记交换你的文本框名称。

vsikbqxv

vsikbqxv4#

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
zsbz8rwp

zsbz8rwp5#

你得试试这样的。
添加一个新的用户控件,例如CustomTextBox并更改

public partial class CustomTextBox : UserControl

public partial class CustomTextBox : TextBox

然后,您将收到以下错误消息,指出未定义“AutoScaleMode”。请删除Designer.cs类中的以下行。

this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

对新添加控件的构造函数进行更改,如下所示。

public partial class CustomTextBox : TextBox
{
    public CustomTextBox()
    {
        InitializeComponent();
        SetStyle(ControlStyles.SupportsTransparentBackColor |
                 ControlStyles.OptimizedDoubleBuffer |
                 ControlStyles.AllPaintingInWmPaint |
                 ControlStyles.ResizeRedraw |
                 ControlStyles.UserPaint, true);
        BackColor = Color.Transparent;
    }
}

生成,请关闭自定义控件设计器(如果打开),这样您就可以在任何其他控件或窗体上使用此控件。
将其从工具箱中拖出来,如下所示

jtoj6r0c

jtoj6r0c6#

创建一个继承自TextBox的新控件,在构造函数中设置样式以允许透明。然后使用新控件代替TextBox
在构造函数中执行以下操作:

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

这将允许新控件具有透明背景色。
您可以在此处阅读有关控件样式的更多信息; MSDN: Control Styles,这可能也有帮助; Inheriting from a Windows Forms Control with Visual C#

wbgh16ku

wbgh16ku7#

我从来不喜欢为此制作自己的继承控件。所以我为私有SetStyle函数制作了一个 Package 器函数。
尝试使用它,而不是创建自己的类?

public static bool SetStyle(Control c, ControlStyles Style, bool value)
{
    bool retval = false;
    Type typeTB = typeof(Control);
    System.Reflection.MethodInfo misSetStyle = typeTB.GetMethod("SetStyle", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    if (misSetStyle != null && c != null) { misSetStyle.Invoke(c, new object[] { Style, value }); retval = true; }
    return retval;
}

第一个月

igsr9ssn

igsr9ssn8#

很抱歉发现旧的帖子,但是,已经搜索了几天,现在找到一个解决这个可怕的问题,没有透明的文本框!!!(令人惊讶的是,MSAccess有一个检查状态显示透明度!)
无论如何,我已经建立了一个VB的工作区,然而,它是非常粗糙,虽然可能会帮助很多人也希望从更多的硬核'的任何见解输入...
它基本上使用文本框,然后调整大小,并替换为一个标签(因此现在表示一个透明的“出现”文本框。还有一些其他的事情,如停止哔哔声,如果按回车键在单行文本框。
创建一个新的类,并将所有代码粘贴到顶部,这应该会创建两个自定义对象(CTextBox和CLabel)-您只需要在表单设计中使用CTEXTBOX。
很容易转换成C,如果这是你的语言,但请让我知道,如果有任何建议?

Imports System.ComponentModel

Public Class CTextBox
Inherits TextBox
Dim _zUseEnterAsTab As Boolean = True
Dim _zUseTransparent As Boolean = False
Dim _zUseTransparentColor As Color = Color.Transparent
Dim _zUseTransparentBorderColor As Color = Color.Gray
<Description("Use the Enter Key as Tab (Stops Beeps) only for Single line TextBox"), Category("CTextBox")> _
Public Property zUseEnterAsTab() As Boolean
    Get
        Return _zUseEnterAsTab
    End Get
    Set(value As Boolean)
        _zUseEnterAsTab = value
        Me.Invalidate()
    End Set
End Property
<Description("Use Transparent TextBox"), Category("CTextBox")> _
    Public Property zUseTransparent() As Boolean
    Get
        Return _zUseTransparent
    End Get
    Set(value As Boolean)
        _zUseTransparent = value
        Me.Invalidate()
    End Set
End Property
<Description("Change the transparency to ANY color or shade or Alpha"), Category("CTextBox")> _
Public Property zUseTransparentColor() As Color
    Get
        Return _zUseTransparentColor
    End Get
    Set(value As Color)
        _zUseTransparentColor = value
        Me.Invalidate()
    End Set
End Property
<Description("Border color of the texbox when transparency used"), Category("CTextBox")> _
    Public Property zUseTransparentBorderColor() As Color
    Get
        Return _zUseTransparentBorderColor
    End Get
    Set(value As Color)
        _zUseTransparentBorderColor = value
        Me.Invalidate()
    End Set
End Property
Protected Overrides Sub OnCreateControl()
    'Again for my benifit - there may be other ways to force the transparency 
    'code at form / event startup, but this is the way i chose, any advice
    'or alternatives would be great!! :)
    If Not DesignMode Then
        'Basically don't do in design mode!
        If _zUseTransparent Then
            'Added to handle the event of textbox dissabled
            If Me.Enabled Then
                CreateMyLabel(Me)
                MakeLabelVisible(foundLabel, Me)
            End If
        End If
    End If
    MyBase.OnCreateControl()
End Sub
Protected Overrides Sub OnKeyPress(e As KeyPressEventArgs)
    If MyBase.Multiline = True Then
        MyBase.OnKeyPress(e)
    Else
        If e.KeyChar = Chr(Keys.Enter) Then
            e.Handled = True
            If zUseEnterAsTab = True Then SendKeys.Send("{tab}")
            MyBase.OnKeyPress(e)
        End If
    End If
End Sub
Protected Overrides Sub OnLeave(e As EventArgs)
    If _zUseTransparent Then
        CreateMyLabel(Me)
        MakeLabelVisible(foundLabel, Me)
    End If
    MyBase.OnLeave(e)
End Sub
Protected Overrides Sub OnEnter(e As EventArgs)
    If _zUseTransparent Then
        CreateMyLabel(Me)
        MakeTextBoxVisible(foundLabel, Me)
    End If
    MyBase.OnEnter(e)
End Sub
Dim foundLabel As CLabel = Nothing
Sub CreateMyLabel(_TxtBox As CTextBox)
    foundLabel = Nothing
    Dim l As CLabel
    If GetMyLabel("L_" & Me.Name, Me) Then
        l = foundLabel
        If Not l.Name = "L_" & Me.Name Then
            MsgBox("L_" & Me.Name)
        End If
        l.Font = _TxtBox.Font
        l.Text = _TxtBox.Text
        l.BorderColor = _zUseTransparentBorderColor
        l.BackColor = _zUseTransparentColor
        l.BorderStyle = Windows.Forms.BorderStyle.None 'Handled by paint event
    Else
        l = New CLabel
        l.Name = "L_" & _TxtBox.Name
        l.BorderColor = _zUseTransparentBorderColor
        l.BackColor = _zUseTransparentColor
        l.Size = _TxtBox.Size
        l.BorderStyle = Windows.Forms.BorderStyle.None 'Handled by paint event
        l.AutoSize = False
        l.Font = _TxtBox.Font
        l.Location = _TxtBox.Location
        l.Text = _TxtBox.Text
        l.Anchor = _TxtBox.Anchor
        _TxtBox.Parent.Controls.Add(l)
        foundLabel = l
    End If
End Sub
Function GetMyLabel(_LabelName As String, _TxtBox As CTextBox) As Boolean
    For Each ctl As Control In _TxtBox.Parent.Controls
        If ctl.Name = _LabelName Then
            foundLabel = ctl
            Return True
        End If
    Next
    Return False
End Function
Private Sub MakeLabelVisible(_Label As CLabel, _TxtBox As CTextBox)
    _Label.Location = _TxtBox.Location
    _Label.Anchor = _TxtBox.Anchor
    _Label.Size = _TxtBox.Size
    _TxtBox.Size = New Size(0, 0)
    _TxtBox.Anchor = AnchorStyles.None
End Sub
Private Sub MakeTextBoxVisible(_Label As CLabel, _TxtBox As CTextBox)
    _TxtBox.Location = _Label.Location
    _TxtBox.Anchor = _Label.Anchor
    _TxtBox.Size = _Label.Size
    _Label.Size = New Size(0, 0)
    _Label.Anchor = AnchorStyles.None
End Sub
End Class

Public Class CLabel
Inherits Label
Public BorderColor As Color = Color.Gray
Sub New()
    MyBase.FlatStyle = Windows.Forms.FlatStyle.Standard
    'Added padding as labels shifted text upwards
    'NOT tested on all fonts etc, purely for my sources
    MyBase.Padding = New Padding(0, 3, 0, 0)
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
    Dim _TxtBox As CTextBox = Nothing
    Dim _TxtBoxName As String = Microsoft.VisualBasic.Right(Me.Name, Len(Me.Name) - 2)
    For Each elem As Control In Me.Parent.Controls
        If elem.Name = _TxtBoxName Then
            _TxtBox = elem
            Exit For
        End If
    Next
    _TxtBox.Select()
    MyBase.OnMouseDown(e)
End Sub
Protected Overrides Sub OnMouseEnter(e As EventArgs)
    Cursor = Cursors.IBeam
    MyBase.OnMouseEnter(e)
End Sub
Protected Overrides Sub OnMouseLeave(e As EventArgs)
    Cursor = Cursors.Default
    MyBase.OnMouseLeave(e)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)
    ControlPaint.DrawBorder(e.Graphics, Me.DisplayRectangle, Color.Gray, ButtonBorderStyle.Solid)
End Sub
Private Sub MakeLabelVisible(_Label As CLabel, _TxtBox As CTextBox)
    _Label.Size = _TxtBox.Size
    _TxtBox.Size = New Size(0, 0)
    _Label.Anchor = _TxtBox.Anchor
    _TxtBox.Anchor = AnchorStyles.None
End Sub
Private Sub MakeTextBoxVisible(_Label As CLabel, _TxtBox As CTextBox)
    _TxtBox.Size = _Label.Size
    _Label.Size = New Size(0, 0)
    _TxtBox.Anchor = _Label.Anchor
    _TxtBox.Anchor = AnchorStyles.None
End Sub
End Class

相关问题