transparent richTextBox

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-19 06:41:27

问题


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


回答1:


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.




回答2:


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.




回答3:


Have you given this a try? http://www.codeproject.com/KB/edit/AlphaBlendedTextControls.aspx?artkw=richTextBox%20to%20be%20transparent




回答4:


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.



来源:https://stackoverflow.com/questions/4910036/transparent-richtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!