470,586 Members | 1,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,586 developers. It's quick & easy.

C# Transparent Control Problem

Hello there !
I'm developing a Windows appliciation that involves so called "Transparent Controls". As you would probably know, the only ( as far as i know ) way to achieve a fully transparent control in C# is to add the so called "Transparent style" to your control ("WS_EX_TRANSPARENT"). There are numerous articles how to achieve this and the method is ALMOST always one and the same:
Expand|Select|Wrap|Line Numbers
  1. protected override  CreateParams CreateParams
  2. {
  3.   get
  4.   {
  5.     CreateParams cp=base.CreateParams;
  6.     cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
  7.     return cp;
  8.   }
  9. }
  10. protected override  void OnPaintBackground(PaintEventArgs pevent)
  11. {
  12.   //do not allow the background to be painted 
  13.  
And that works fine! ( well .... at least all say that ..... and all the articles say so ), but am i missing something or that method has SOME SERIOUS problems that nobody can see ?
For example the Z-order. The following piece of code creates simply a transparent control that draws a line accross itself with a random color:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Drawing;
  6. namespace WindowsApplication16
  7. {
  8.     public class HrisTranspControl : Control
  9.     {
  10.         private Pen _drawingPen;
  11.         public HrisTranspControl()
  12.         {
  13.             Random rnd = new Random();
  14.             this._drawingPen = new System.Drawing.Pen(new System.Drawing.SolidBrush(System.Drawing.Color.FromArgb(rnd.Next(0,255),rnd.Next(0,255), rnd.Next(0,255))),5);
  15.         }
  16.         protected override CreateParams CreateParams
  17.         {
  18.             get
  19.             {
  20.                 CreateParams cp = base.CreateParams;
  21.                 cp.ExStyle |= 0x00000020;
  22.                 return cp;
  23.             }
  24.         }
  25.         protected override void OnPaintBackground(PaintEventArgs pevent)
  26.         {
  27.             // Do nothing
  28.         }
  29.         protected override void OnPaint(PaintEventArgs e)
  30.         {
  31.             base.OnPaint(e);
  32.             e.Graphics.DrawLine(_drawingPen, new System.Drawing.Point(0, 0), new System.Drawing.Point(this.Width, this.Height));
  33.         }
  34.     }
  35. }
And so ... when u put few "line controls" on top of each other the Z-ORDER goes to hell. You can't change it, its not corrent and it changes randomly each time u select a random "line control".
I've tried A LOT of methods to fix that ( changing the z-order in background, repainting the actual background because i think its because the background is not drawn ... and so on ). None works. I'm out of ideas and the worst part is i've almost finished the application and right in the end i realize that bug and it kills me.
Please help !!!


The "line controls" were added in the order shows 1,2,3 , but the Z-order appears to be different ( 1,3,2 ). In the current case "bring to front" , "send to back" doesnt change the Z-ORDER
Jan 2 '09 #1
6 10779
Plater
7,872 Expert 4TB
LineControl? Are you talking about the old VB controls that are no longer supported?
You said transparent controls, did you mean transparent forms or transparent controls?
There is a Transparency key that works (albeit "sometimes") pretty well.

Here is a screenshot of one of my forms. The form itself is transparent, as is the background of the Picturebox on the left(so the iamge does not appear square). The control on the right is a regular label.
Attached Images
File Type: jpg transparentform.jpg (7.5 KB, 624 views)
Jan 5 '09 #2
tlhintoq
3,525 Expert 2GB
I don't think HristiyanVasilev's talking about old VB controls. (S)he has created his/her own class, subclassed from control.
public class HrisTranspControl : Control
This control would be the 'line control' mentioned.

As near as I can tell it creates a new random colored line every time it is painted. Which means it might make a new random line ever time it is moved, covered/uncovered, resized etc. NOT just when a new instance of the control is created.

So I don't really know how you could tell the z-order is getting 'messed up' if it is going to keep creating lines at random every time it gets painted.
Jan 6 '09 #3
Thank you for the response but unfortunatly you are not right. The contructor is not called every time the control is repainted and so the line does not change its color when the control is repainted.
Howeever i don't wish to discuss this basic matter. The problem still persists and i'm almost helpless .....
I'm almost sure that there is a serious problem with that method.
If someone has any suggestions ... please not hesitate to offer them ....
Jan 26 '09 #4
Plater
7,872 Expert 4TB
Hmm it could be the way your are overriding the CreateParams that is causing it to be wierd.
The controls do not layer themselves in the order in which they are .Add()'ed to the parent control? (Either by you in your own code or in the designer?)
Have you played with that myParentControl.Controls.SetChildIndex(myTranspCon trol,0); ?
Jan 26 '09 #5
Thank you for your quick reponse !
Behind the scenes in the "BringToFront" and "SendToBack" methods is actually used "SetChildIndex" , so ... "yes i have played with the SetChildIndex method"
However there are hundreds of examples where this exact method is used ( overriding the CreateParams and adding the TransparentStyle ) and neither of them work correctly.
If anyone has any other suggestions ... please dont hesitate to offer them ...!
Jan 27 '09 #6
i am also working on transparent control
and also encounter layer problem

i did search a lot of site but can't find the solution

only thing to maintain the z-order well is get rid off the transparent property in the custom control.

when i remark (for control transparent)
cp.ExStyle = cp.ExStyle Or &H20
Return cp

the z-order is fine.

any solution???
Feb 2 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Steve Koon | last post: by
8 posts views Thread by Grahammer | last post: by
4 posts views Thread by jcrouse | last post: by
1 post views Thread by FredC | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.