The background color in Winform can be set as a translucent RichTextBox

background There was another problem with the...
background

There was another problem with the serial port tool made last time. The window requires pictures as the background, but RichTextBox can't set the background color to Transparent. A white frame covers most of the pictures. It's ugly and hot.

Looking for information on the website, I found a very simple way:

protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; // Turn on WS ex transparent to make the control transparent return cp; } }

In this way, although the background of the control can be completely transparent, it can not meet the requirements of translucency, so a simple and crude way is used:
-1. Create a RichTextBox derived class - CustomControl1, override its CreateParams property
-2. In the construction, setstyle (controlstyles. Supportstranparentbackcolor, true); / / make the transparent background transparent
-3. base.BackColor = Color.Transparent; / / the background is set with transparent color
-4. Create a custom control - UserControl1, and set base.BackColor to Transparent
-5. override the BackColor of the custom control to shield its Set function
-6. OnPaint of override custom control, in which transparent color is drawn
-7. Add the RichTextBox derived class created above in the custom control
-8. All necessary properties and methods of "transparent" CustomControl1 in the custom control

Specific implementation

CustomControl1:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WallSwipeTestTool2.MyControl { public partial class CustomControl1 : RichTextBox { public CustomControl1() { ControlStyles styles = ControlStyles.SupportsTransparentBackColor | ControlStyles.OptimizedDoubleBuffer; SetStyle(ControlStyles.SupportsTransparentBackColor, true);//Make transparent background transparent SetStyle(ControlStyles.OptimizedDoubleBuffer, true);//Use dual cache base.CreateControl(); InitializeComponent(); base.BackColor = Color.Transparent; //Transparent background } //Actively cause Paint when the text changes, otherwise it will leave traces of the cursor protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); this.Refresh(); } //Redraw while dragging the slider, otherwise the entire interface will "paste off" () protected override void OnVScroll(EventArgs e) { base.OnVScroll(e); this.Refresh(); } //Redraw while dragging the slider, otherwise the entire interface will be "pasted out" protected override void OnHScroll(EventArgs e) { base.OnHScroll(e); this.Refresh(); } //Make the background transparent protected override CreateParams CreateParams { get { CreateParams cp = base.CreateParams; cp.ExStyle |= 0x00000020; // Turn on WS ex transparent to make the control transparent return cp; } } } }

UserControl1:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace WallSwipeTestTool2.MyControl { public partial class UserControl1 : UserControl { private int _alpha = 80;//Set transparency private bool _isTransparent = true;//Transparent or not private Color _backColor = Color.White; public UserControl1() { this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.UserPaint, true); this.SetStyle(ControlStyles.ResizeRedraw, true); InitializeComponent(); } /// abstract: /// Appends text to the current text of the text box. /// /// parameter: /// text: /// The text to append to the current content of the text box. public void AppendText(string text) { this.customControl11.AppendText(text); } // // Summary: // Scrolls the contents of the control to the current caret location. public void ScrollToCaret() { this.customControl11.ScrollToCaret(); } // // Summary: // Clears all text from the text box control. public void Clear() { this.customControl11.Clear(); } #region attribute public int SelectionStart { get { return this.customControl11.SelectionStart; } set { this.customControl11.SelectionStart = value; } } public string SelectedText { get { return this.customControl11.SelectedText; } set { this.customControl11.SelectedText = value; } } public Color SelectionColor { get { return this.customControl11.SelectionColor; } set { this.customControl11.SelectionColor = value; } } [Browsable(true), Category("XX control"), Description("Set get foreground color"), DefaultValue(typeof(Color), "Black")] public new Color ForeColor { get { return this.customControl11.ForeColor; } set { this.customControl11.ForeColor = value; } } [Browsable(true), Category("XX control"), Description("Controls whether to edit text in a text box"), DefaultValue(typeof(Font), "Song style, 9pt")] public new Font Font { get { return this.customControl11.Font; } set { this.customControl11.Font = value; } } [Browsable(true), Category("XX control"), Description("Controls whether to edit text in a text box"), DefaultValue(false)] public bool ReadOnly { get { return this.customControl11.ReadOnly; } set { this.customControl11.ReadOnly = value; } } [Browsable(false),Category("XX control"), Description("Set or get the selected background color"), DefaultValue("")] public Color SelectionBackColor { get { return this.customControl11.SelectionBackColor; } set { this.customControl11.SelectionBackColor = value; } } [Browsable(true), Category("XX control"), Description("Set or get the displayed text"), DefaultValue("")] public new string Text { get { return this.customControl11.Text; } set { this.customControl11.Text = value; } } [Browsable(true), Category("XX control"), Description("Set or get transparency"), DefaultValue(80)] public int Alpha { get { return _alpha; } set { _alpha = value; } } [Browsable(true), Category("XX control"), Description("Use transparency or not,Default is True"), DefaultValue(true)] public bool IsTransparent { get { return _isTransparent; } set { _isTransparent = value; } } [Browsable(true), Category("XX control"), Description("Set get transparent background color"), DefaultValue(typeof(Color), "Transparent")] public Color AlphaBackColor { get { return this._backColor;; } set { this._backColor = value; base.BackColor = Color.Transparent; } } [Browsable(false), Category("XX control"), Description("Set get background color"), DefaultValue(typeof(Color), "Transparent")] public override Color BackColor { get { return base.BackColor; } } #endregion #region Overrides protected override void OnPaint(PaintEventArgs e) { Bitmap bmp = new Bitmap(this.ClientRectangle.Width, this.ClientRectangle.Height, e.Graphics); Graphics g = Graphics.FromImage(bmp); Pen labelBorderPen; SolidBrush labelBackColorBrush; if (_isTransparent) { Color cl = Color.FromArgb(_alpha, this._backColor); labelBorderPen = new Pen(Color.Black, 2); labelBackColorBrush = new SolidBrush(cl); } else { labelBorderPen = new Pen(this._backColor, 0); labelBackColorBrush = new SolidBrush(_backColor); } g.DrawRectangle(labelBorderPen, 0, 0, this.Width, this.Height); g.FillRectangle(labelBackColorBrush, 0, 0, this.Width, this.Height); base.OnPaint(e); e.Graphics.DrawImage(bmp, 0, 0); bmp.Dispose(); } #endregion private void customControl11_TextChanged(object sender, EventArgs e) { this.Refresh(); } private void customControl11_Scroll(object sender, EventArgs e) { this.Refresh(); } } }

4 May 2020, 22:54 | Views: 5002

Add new comment

For adding a comment, please log in
or create account

0 comments