TextBox with a Transparent Background [duplicate]

2019-01-15 14:39发布

This question already has an answer here:

I want to do textBox a Transparent Background c# .net Visual Studio is making a mistake if you define Properties

3条回答
smile是对你的礼貌
2楼-- · 2019-01-15 15:17

I've found a workaround for this problem, which I have on my own. Most of what I've read over here is true. And the approaches "à la" AlphaBlendTextBox are way too complex or too time-consuming for some environments, already heavily charged.

Assume you have a given background color and a given picture or whatever you want to see through the RichTextBox control. This is what I've done (summarized):

  1. on the main form, you place the picture, text, buttons or whatever as projected, with the proper background color and / or picture
  2. create a new form and position it wherever appropriate
  3. set this new form TransparencyKey to SystemColors.InactiveBorder
  4. take care of this form border properties (FormBorderStyle to FormBorderStyle.None; ControlBox,MinimizeBox, MaximizeBox and ShowIcon to false, TopMost to true, StartPosition to FormStartPosition.Manual, SizeGripStyle to SizeGripStyle.Hide), so there's no visible form structures
  5. create a RichTextBox with the same size of the form and located on its upper, left corner
  6. set this box BackColor to SystemColors.InactiveBorder (remember the TransparencyKey?) and its BorderStyle to None as well
  7. take care of textbox contents: color(s), font(s) and strings
  8. synchronize this form visibility with whatever you need to and... voilà! You can see your application background through whatever you write and edit on the text box!

I can't pretend this approach fits everybody, but it is way simpler than others I've seen and, as long as I can keep it that way, I do prefer the simpler solutions.

Of course, when you close the main form, you must take care of the child form, but this is pretty basic for you, isn't it?

Enjoy!

查看更多
一夜七次
3楼-- · 2019-01-15 15:37

Put this in the constructor:

SetStyle(ControlStyles.SupportsTransparentBackColor, true);

The class need to enable the transparent style. (For some reason it isn't supported by default).

public class MyControl : System.Windows.Forms.UserControl
{
        public MyControl ()
        {
            // Create visual controls
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        }
}

Or if it's not a custom control:

mycontrolObject.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

More about Control.SetStyle Method

Other Control Styles

查看更多
聊天终结者
4楼-- · 2019-01-15 15:37

This is not an easy task. .Net TextBox control is a wrapper around Win32 Edit control, so you will need to do sub-classing to achieve background transparency.

Take a look at this sample: AlphaBlendTextBox - A transparent/translucent textbox for .NET

查看更多
登录 后发表回答