How do I make a richTextBox have a transparent background / show the image beneath it? (C#, Windows Forms App)

dorian tester 0 Reputation points
2024-12-27T06:00:17.44+00:00

Hello, as per the title, I want to make the richTextBox in my project transparent, or have a way to show the image beneath it. Whenever I try to set the backcolor to transparent, i always get "Property value is not valid" with "Control does not support transparent background colors." as the Details. Is there a piece of code I can just shove in there, a plugin I can use, or am I just SOL? Thanks.

(In case it wasn't obvious, I have zero experience with coding in any fashion)

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,915 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,319 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,159 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jiale Xue - MSFT 48,441 Reputation points Microsoft Vendor
    2024-12-27T06:43:01.78+00:00

    Hi @dorian tester , Welcome to Microsoft Q&A,

    Sorry, there is no official plugin for the WinForms project that supports transparent background color.

    If you can use WPF, you can use transparent background color in it.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. KOZ6.0 6,580 Reputation points
    2024-12-28T10:29:08.01+00:00

    If the OS is Windows 8 or higher, you can make the child window a layered window. To do this, add an application manifest and enable Windows 8.

          <!-- Windows 8 -->
          <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />
    

    https://zcusa.951200.xyz/en-us/answers/questions/1433265/adding-transparent-form-in-panel

    public partial class Form1 : Form
    {
    
        public Form1()
        {
            InitializeComponent();
            richTextBox1.HandleCreated += RichTextBox1_HandleCreated;
        }
    
        private void RichTextBox1_HandleCreated(object? sender, EventArgs e)
        {
            IntPtr hWnd = richTextBox1.Handle;
            int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
            SetWindowLong(hWnd, GWL_EXSTYLE, exStyle | WS_EX_LAYERED);
            SetLayeredWindowAttributes(hWnd, ColorTranslator.ToWin32(richTextBox1.BackColor), 0, LWA_COLORKEY);
        }
    
        const int WS_EX_LAYERED = 0x00080000;
        const int GWL_EXSTYLE = -20;
        const int LWA_COLORKEY = 0x01;
        const int LWA_ALPHA = 0x02;
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
    
        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool SetLayeredWindowAttributes(
            IntPtr hWnd, int crKey, byte bAlpha, int dwFlags);
    }
    

    enter image description here

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.