 |
|
 |
Thanks for good article!
You are overriding this:
override protected void OnPaintBackground( PaintEventArgs e ) { }
... and I would like to do this:
override protected void OnPaintBackground(PaintEventArgs e) { e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(200, Color.White)), this.Bounds); }
Tell me now, why this does not work? 
Greetings - gajatko
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi everybody. I've tried to create my own transparent TextBox. I started with RichTextBox variant. Unfortunately I'm not able to create it semi-transparent - its 100% transparent. But this can be solved by putting the control onto some semi-transparent panel. I've experimented with SetStyle a lot and finally guessed some working configuration. But there is still problem with flickering. I've reduced it, but especially when the control is large (multiline) it flickers quite a lot. I exactly know what is needed this issue to be solved: You have to catch the moment when RichTextBox is painting some region and obtain coordinates and dimensions of this region and invalidate appropriate region of parent control. Unfortunately I haven't been able to do this, so I'm invalidating whole parent in ONVScroll, ONHScroll, OnSelectionChanged and OnTextChanged - with one exception: If text is changed from keyboard I invalidate only region appropriate to from current line down.
This is my code:
Imports System.Windows.Forms Imports System.ComponentModel, System.ComponentModel.Design Imports System.Drawing, System.Drawing.Design Imports Tools.ComponentModel Imports System.Reflection #If Config <= Alpha Then Namespace Windows.Forms "Đonny", "dzonny@dzonny.cz", "http://dzonny.cz")> _ GetType(TransparentTextBox), LastChMMDDYYYY:="05/19/2007")> _ "tbx")> _ "Text"), DefaultEvent("Click")> _ "Text")> _ GetType(TransparentLabel), "TransparentTextBox.bmp")> _ Public Class TransparentTextBox : Inherits RichTextBox #Region "CTors" Public Sub New() Me.SetStyle(ControlStyles.OptimizedDoubleBuffer, True) Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True) Me.SetStyle(ControlStyles.ResizeRedraw, True) Me.SetStyle(ControlStyles.UserPaint, True) Me.ResetBackColor() End Sub Public Sub New(ByVal Text As String) Me.New() Me.Text = Text End Sub Public Sub New(ByVal BackColor As Color) Me.New() Me.BackColor = BackColor End Sub Public Sub New(ByVal BackColor As Color, ByVal Text As String) Me.New() Me.Text = Text Me.BackColor = BackColor End Sub #End Region #Region "Paint" Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams Get Dim cp As CreateParams = MyBase.CreateParams cp.ExStyle = cp.ExStyle Or &H20 Return cp End Get End Property Private InInvalidateParent As Boolean = False Private Sub InvalidateParent(ByVal rect As Rectangle) If Me.Parent Is Nothing OrElse InInvalidateParent Then Return Dim pr As Rectangle Try InInvalidateParent = True If rect.Width = 0 OrElse rect.Height = 0 Then Exit Sub pr = New Rectangle(Me.Left + rect.Left, Me.Top + rect.Top, rect.Width, rect.Height) Parent.Invalidate(pr, True) Finally InInvalidateParent = False End Try End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Const WM_PAINT As Int32 = &HF Const WM_PRINT_CLIENT As Int32 = &H318 Const WM_KEYDOWN As Int32 = &H100 Const WM_KEYUP As Int32 = &H101 Const WM_PASTE As Int32 = &H302 Const WM_CUT As Int32 = &H300
Select Case m.Msg Case WM_PRINT_CLIENT, WM_PAINT If InInvalidateParent Then Return Me.DefWndProc(m) End Select Dim KbdProcessOnStack As Boolean = KbdProcess Try KbdProcess = m.Msg = WM_KEYDOWN OrElse m.Msg = WM_KEYUP OrElse m.Msg = WM_PASTE OrElse m.Msg = WM_CUT MyBase.WndProc(m) Finally If Not KbdProcessOnStack Then KbdProcess = False End Try End Sub Private KbdProcess As Boolean = False Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs) If KbdProcess Then Dim pos As Point = Me.GetPositionFromCharIndex(Me.SelectionStart) InvalidateParent(New Rectangle(Me.ClientRectangle.Left, Me.ClientRectangle.Top + pos.Y, Me.ClientRectangle.Width, Me.ClientRectangle.Height - (Me.ClientRectangle.Top + pos.Y))) Else InvalidateParent(Me.ClientRectangle) End If MyBase.OnTextChanged(e) End Sub Protected Overrides Sub OnHScroll(ByVal e As System.EventArgs) InvalidateParent(Me.ClientRectangle) MyBase.OnHScroll(e) End Sub Protected Overrides Sub OnVScroll(ByVal e As System.EventArgs) InvalidateParent(Me.ClientRectangle) MyBase.OnVScroll(e) End Sub Protected Overrides Sub OnSelectionChanged(ByVal e As System.EventArgs) InvalidateParent(Me.ClientRectangle) MyBase.OnSelectionChanged(e) End Sub Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs) MyBase.OnKeyPress(e) End Sub Protected Overrides Sub OnKeyDown(ByVal e As System.Windows.Forms.KeyEventArgs) MyBase.OnKeyDown(e) End Sub #End Region #Region "Properties" GetType(Color), "Transparent"), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), Browsable(False)> _ Public Overrides Property BackColor() As System.Drawing.Color Get Return MyBase.BackColor End Get Set(ByVal value As System.Drawing.Color) MyBase.BackColor = value End Set End Property Public Overrides Sub ResetBackColor() Me.BackColor = Color.Transparent End Sub #End Region End Class End Namespace #End If
See CodePlex.com/DTools for details.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I implemented the RichTextBox version and it is transparent but when I type text the text looks like a bunch of black. If I highlight the text or minimize/maximize the window the text looks fine. Its just when typing it looks bad. Is there anything I can do to fix that? Have you seen that?
Thanks!
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
 |
The problem is that this proposed solution does not ever repaint the background.
I found a solution that works. Override WndProc in the subclassed rich text box. Make sure you call the base WndProc first. Then check for WM_CHAR (0x0102). When the character is received, send the parent form the message Invalidate() and then send the parent the message Update().
The will force an update of the background, which in turn forces the control to redraw the text.
There is noticeable flicker and it is easy to type ahead of the refresh rate. There is certainly no flicker with the background image which is probably redrawn using bitblt. I tried to Invalidate only a small rectangle, but the OnPaintBackground in the parent form receives a request to repaint the entire background. I would suspect this is because the control has no ability to do a partial update. It's all or nothing.
I tried to emulate my solution in unmanaged C++ by creating a subclass of CRichEditCtrl and checking for WM_CHAR. I noticed that there was slight flicker. I think that it was much faster because it was machine code.
ReDFox has created a similar C++ unmanaged code solution: http://www.codeproject.com/richedit/SemiRichEdit.asp#xx1207974xx.
He does not use a subclass of CRichEditCtrl. He uses the Win32 technique of sublcassing. He sets up a callback to RICHED20.dll. This is faster than the compiled version using CRichEditCtrl. I cannot detect any flicker in his, although someone did post that it does slow things down.
I would suspect that these are the reasons that MS did an override on the backgroundImage property to not allow it.
Seems like this is an opportunity for someone to write an ActiveX rich edit control from the ground up that does support an owner drawn background image.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I was looking for a RichTextBox control that would allow a Transparent BackColor.
It's taken me a day or two, on and off, to find a complete solution to my problem, but finally managed to pull it together with the help of the last post and others.
For the noobs, who love to copy/paste, here is the code I used in VB.NET (Sorry!, was working in a legacy product ):
Public Sub New() Me.SetStyle(ControlStyles.DoubleBuffer, True) Me.SetStyle(ControlStyles.SupportsTransparentBackColor, True) Me.ScrollBars = RichTextBoxScrollBars.None End Sub
Protected Overrides ReadOnly Property CreateParams() As System.Windows.Forms.CreateParams Get Dim cParams As CreateParams = MyBase.CreateParams cParams.ExStyle = cParams.ExStyle Or &H100 cParams.ExStyle = cParams.ExStyle Or &H2 cParams.ExStyle = cParams.ExStyle Or &H20 Return cParams End Get End Property
Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs) End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) MyBase.WndProc(m)
If Not Me.Parent Is Nothing Then If m.Msg = &H102 Then Me.Parent.Invalidate() End If End If End Sub And C#:
public RichTextBoxEx() { base.SetStyle( ControlStyles.DoubleBuffer, true ); base.SetStyle( ControlStyles.SupportsTransparentBackColor, true ); base.ScrollBars = RichTextBoxScrollBars.None; }
protected override void OnPaintBackground(PaintEventArgs pevent) { }
protected override CreateParams CreateParams { get { CreateParams cParams = base.CreateParams; cParams.ExStyle |= 0x100; cParams.ExStyle |= 0x2; cParams.ExStyle |= 0x20; return cParams; } }
protected override void WndProc(ref Message m) { base.WndProc (ref m);
if( m.Msg == 0x0102) { this.Parent.Invalidate(); } }
** Provided AS-IS ** Use at your own risk **
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
 |
Are you serious, you think this works? Controls flickers like h*ll. Text disappears when you hit enter (move to next line), scrollbars doesn't paint if you want them (uncommented your base.ScrollBars = RichTextBoxScrollBars.None) etc etc.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I tried creating an instnace of your class on a form :
transTB newTransTB = new transTB(); newTransTB.BackColor = Color.Transparent; this.Controls.Add(newTransTB);
newTransTB.Location = new Point (150,100); newTransTB.Size = new Size(200,200); newTransTB.Text = "Test Transparent Text Control";
panel1.SendToBack(); panel2.SendToBack(); newTransTB.BringToFront();
And positioning over two contrasting colored Panels. The TextBox is not transparent. It doesn't show the form background color either, if it positioned where it is only over the form. I experimented with this in a variety of ways, following suggestions of those who responded to this article. I tried putting the set BackColor to Transparent call in the Class, moving it around in the Form code, etc.
best, Bill
"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis
|
| Sign In·View Thread·PermaLink | 3.50/5 |
|
|
|
 |
|
 |
Hey Bill,
You won't use "Color.Transparent".
What I did in my app (which is a kiosk app) was that I had a form with a image background and I wanted to have a textbox on top of that which I could throw some text and still "see" the form's background image.
transTB newTransTB = new transTB(); // newTransTB.BackColor = Color.Transparent; -> don't do this newTransTB.Text = "Hello World"; this.Controls.Add(newTransTB);
That's what I needed it for and that's what I did. There are more threads below that are interesting as well.
Regards, Luis
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Please post a simple example of this working.
I took out the call to set the textbox backcolor to color.transparent : now the textbox appears with white (default) background no matter whether over a form with bitmap background image or over another control.
"The greater the social and cultural distances between people, the more magical the light that can spring from their contact." Milan Kundera in Testaments Trahis
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You're right. This code is only working for RichTextBox, not for TextBox.
For now, use this developer's code for TextBox:
http://www.codeproject.com/cs/miscctrl/alphablendtextbox.asp
With this code, the RichTextBox scroll gets messy (just like the user below said). Sorry, this has been a pain for me too. But I am still working at it and will be glad to share the code if I get this right.
Best regards, Luis
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
 | bug  GoAn | 16:31 19 Nov '04 |
|
 |
if you put a lot of text in a richtextbox so that the scrollbars appear and then you scroll the text it will appear all messed up
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I dropped the control onto the form but it doesn't look transparent... is there something else that has to be done?
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
You must set base.SetStyle(ControlStyles.UserPaint, true); base.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
Thomas Bock
|
| Sign In·View Thread·PermaLink | 3.00/5 |
|
|
|
 |
|
|
 |
|
 |
I tried to use your code for RichTextBox but I don't understand what I have to do.:( Where I have to use the TransparentControl class? Once I have created the transparent RichTextBox, if I write on RTB itself, the text becomes illegible because the refresh is not effected and the text overlaps. Can you put an example of your solution please? I think it would be useful for everybody. Thanx!
|
| Sign In·View Thread·PermaLink | 2.14/5 |
|
|
|
 |