Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

transparent richTextBox

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

like image 735
Ahmad Houri Avatar asked Feb 05 '11 22:02

Ahmad Houri


3 Answers

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.

like image 131
Shahid M Zubair Avatar answered Nov 02 '22 17:11

Shahid M Zubair


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.

like image 29
Mark Hall Avatar answered Nov 02 '22 18:11

Mark Hall


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

like image 30
Joe Avatar answered Nov 02 '22 18:11

Joe