透明的富文本框

发布于 2024-10-15 19:37:12 字数 126 浏览 3 评论 0原文

如何使富文本框透明
我想要这个,因为我试图将文本放在图形图像(这是我表单的背景)之上。

这就是为什么我希望 richTextBox 是透明的,
我正在使用 .NET 、c# 和 Windows 窗体应用程序

how can I make my richtext box transparent
I want this cuz I am trying to put a text on top of a graphic image (which is the background of my form).

That is why I wanted the richTextBox to be transparent,
I am using .NET ,c# and in a windows form application

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春花秋月 2024-10-22 19:37:13

我知道这个答案已经很晚了,但我希望它可以帮助其他想要简单方法来完成此任务的人。

首先,在项目中创建一个新的用户控件并为其命名,例如 CustomRTB.cs。完成后,打开分部类并将: 更改

public partial class CustomRTB : UserControl

为:

public partial class CustomRTB : RichTextBox

当您打开设计文件时,这将导致错误,因此只需转到 Designer.cs 文件并删除/注释显示错误的行(不会超过两行有错误)。接下来,将以下内容添加到分部类中:

protected override CreateParams CreateParams
{
    get
    {
        //This makes the control's background transparent
        CreateParams CP = base.CreateParams;
        CP.ExStyle |= 0x20;
        return CP;
    }
}

该类现在应如下所示:

public partial class CustomRTB : RichTextBox
{
    public CustomRTB()
    {
        InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            //This makes the control's background transparent
            CreateParams CP = base.CreateParams;
            CP.ExStyle |= 0x20;
            return CP;
        }
    }
}

现在构建您的解决方案,您将能够在表单中使用该控件。该控件将完全透明,您将无法调整透明度。通过更改此代码中的第一行,您还可以创建除 Richtextbox 之外的不同透明控件。希望这会有所帮助:)

编辑:

上述控件的问题是它只能用于以编程方式显示文本,因为在运行或调试应用程序时进行编辑会出现问题(正如 @nevelis 在下面评论)。但是,有一个简单的解决方法:

首先,在项目中创建另一个用户控件并将其命名为 TranslucentPanel.cs(是的,它是一个面板,并且它将是半透明的,其不透明度可以通过编程方式控制)。现在打开分部类并将其修改为:

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

您必须通过简单地注释掉 Designer.cs 文件中抛出错误的行来处理构建项目时出现的错误。完成后,再次构建您的项目,半透明面板将像以前一样出现在您的工具箱中。使用此面板作为透明 RichTextBox 的父控件,即将该面板放置在表单上并将 RTB 放置在其中。您还可以将 BorderStyle 属性设置为 None,以从 UI 中删除 RTB 的任何痕迹。

您还可以通过在程序中使用半透明面板的 BackColor 属性来控制其不透明度:

translucentPanel1.BackColor = Color.FromArgb(50, 0, 0, 0);

更改上面传递的参数将允许您控制面板的不透明度和颜色。

此解决方法不仅可以解决透明 RTB 的光标和滚动问题,还可以解决您创建的任何其他透明控件的光标和滚动问题。

I know this answer is very late, but I hope it helps others who would like an easy way to get this done.

First, create a new User Control in your project and give it a name, say CustomRTB.cs. Once done, open the partial class and change:

public partial class CustomRTB : UserControl

to:

public partial class CustomRTB : RichTextBox

This will cause an error when you open the Design file so just go to the Designer.cs file and remove/comment the lines which show errors (there will be no more than two lines with errors). Next, add the following to the partial class:

protected override CreateParams CreateParams
{
    get
    {
        //This makes the control's background transparent
        CreateParams CP = base.CreateParams;
        CP.ExStyle |= 0x20;
        return CP;
    }
}

The class should look like this now:

public partial class CustomRTB : RichTextBox
{
    public CustomRTB()
    {
        InitializeComponent();
    }

    protected override CreateParams CreateParams
    {
        get
        {
            //This makes the control's background transparent
            CreateParams CP = base.CreateParams;
            CP.ExStyle |= 0x20;
            return CP;
        }
    }
}

Now build your solution and you will be able to use the control in your forms. This control will be completely transparent and you will not be able to adjust the transparency. You will also be able to create different transparent controls apart from a richtextbox by changing the first line in this code. Hope this helps :)

Edit:

The problem with the above control is that it can only be used to display text programmatically as it is problematic to edit while running or debugging the application (as @nevelis explains in the comment below). However, there is a simple workaround for this:

First, create another User Control in your project and name it TranslucentPanel.cs (Yes, it is a panel and it is going to be translucent whose opacity can be controlled programmatically). Now open the partial class and modify it as:

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

You will have to take care of the error that crops up when you build the project by simply commenting out the line in the Designer.cs file which throws it. Once done, build your project again and the translucent panel will appear in your toolbox as before. Use this panel as a parent control to your transparent richtextbox i.e. place the panel on your form and place the RTB inside it. You can also set the BorderStyle property as None to remove any trace of the RTB from the UI.

You can also control the opacity of the translucent panel by using its BackColor property in your program:

translucentPanel1.BackColor = Color.FromArgb(50, 0, 0, 0);

Changing the arguments passed above will let you control the opacity and the colour of the panel.

This workaround will solve the cursor and scrolling problems of not only the transparent RTB, but also any other transparent control you create.

辞旧 2024-10-22 19:37:13

WinForms 控件中不存在真正的透明性。透明模式继承其父级的默认背景。我过去解决这个问题的方法是使用 OnPaint 事件,然后使用 Graphics.DrawString 方法将文本放置在我想要的位置。

There is no such thing as true transparency in a WinForms Control. Transparent mode inherits the default background of its parent. The way I have worked around it in the past has been to use the OnPaint event and then use the Graphics.DrawString method to position the text where I want it.

零崎曲识 2024-10-22 19:37:13

无法让 Windows 窗体控件具有透明背景。许多人以前都尝试过,但都失败了。有些人想出了一些奇特的技巧,但他们都在某些细节上失败了。如果您需要比旧 Windows 窗体更高级的呈现功能,请使用 WPF 或 HTML。

There is no way to have Windows Forms controls with a transparent background. Many have tried it before and all have failed. Some came up with exotic hacks, but they all fail at some detail. Use WPF or HTML if you need more advanced rendering capabilities than the old Windows Forms can offer you.

深爱不及久伴 2024-10-22 19:37:13

情况:

public class TransparentRichTextBox : System.Windows.Forms.RichTextBox
{
    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern System.IntPtr LoadLibrary(string lpFileName);

    protected override System.Windows.Forms.CreateParams CreateParams
    {
        get
        {
            var prams = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != System.IntPtr.Zero)
            {
                prams.ExStyle |= 0x020; // transparent
                prams.ClassName = "RICHEDIT50W";
            }
            return prams;
        }
    }
}

codeproject 用户评论 窃取

What about:

public class TransparentRichTextBox : System.Windows.Forms.RichTextBox
{
    [System.Runtime.InteropServices.DllImport("kernel32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern System.IntPtr LoadLibrary(string lpFileName);

    protected override System.Windows.Forms.CreateParams CreateParams
    {
        get
        {
            var prams = base.CreateParams;
            if (LoadLibrary("msftedit.dll") != System.IntPtr.Zero)
            {
                prams.ExStyle |= 0x020; // transparent
                prams.ClassName = "RICHEDIT50W";
            }
            return prams;
        }
    }
}

Stolen from codeproject user's comment

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文