473,769 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Transparent Control Problem

3 New Member
Hello there !
I'm developing a Windows appliciation that involves so called "Transparen t 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 "Transparen t style" to your control ("WS_EX_TRANSPA RENT"). 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 11025
Plater
7,872 Recognized Expert Expert
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, 647 views)
Jan 5 '09 #2
tlhintoq
3,525 Recognized Expert Specialist
I don't think HristiyanVasile v's talking about old VB controls. (S)he has created his/her own class, subclassed from control.
public class HrisTranspContr ol : 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
HristiyanVasilev
3 New Member
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 Recognized Expert Expert
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.SetCh ildIndex(myTran spControl,0); ?
Jan 26 '09 #5
HristiyanVasilev
3 New Member
Thank you for your quick reponse !
Behind the scenes in the "BringToFro nt" and "SendToBack " methods is actually used "SetChildIn dex" , 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 TransparentStyl e ) and neither of them work correctly.
If anyone has any other suggestions ... please dont hesitate to offer them ...!
Jan 27 '09 #6
fireghost
1 New Member
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

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

Similar topics

5
9505
by: Paul Schnitter | last post by:
Update: My custom control is based on the article "Creating Visual Basic .NET controls from scratch" in "Adventures in .NET" on MSDN. It is designed to be a replacement for the VB6 shape control. My control draws a shape (circle, square, rectangle ...)
6
3601
by: Tom | last post by:
why doesn't user control support transparent ? even after you programmatically make it transparent the controls that you add on it still doesn't transparency where they normally would in a winform why is that ? Tom
3
8058
by: °Ë´óɽÈË | last post by:
HI, I create a custom transparent control, set the style is ControlStyles.DoubleBuffer, It display black background. How to create a double buffer transparent control?
3
3103
by: Steve Koon | last post by:
Any thoughts on getting this project to work or suggesting another method would be appreciated. Steve ========================================== Project: A Windows Form which acts as a Whiteboard. The Form contains 3
8
10610
by: Grahammer | last post by:
Is it possible to set the background of a usercontrol as transparent? I tried setting the background image of the usercontrol to a transparent GIF, but that caused MAJOR problems. I'm making some controls that aren't rectangular and it won't be pretty if I end up with a grey rectangle behind each one. http://msdn.microsoft.com/library/en-us/vbcon/html/vbtskGivingYourControlTransparentBackground.asp?frame=true
4
2564
by: jcrouse | last post by:
I am using the following code to move a label on a form at runtime: If myMousedown = lblP1JoyRight.Name Then If lblP1JoyRight.BackColor.Equals(Color.Transparent) Then bTransCk = True lblP1JoyRight.BackColor = clrLabelMove
1
3083
by: FredC | last post by:
I'm using VS 2003, C#.Net 2003. I built a simple windows form app. with the following attributes: - has a BackgroundImage set to a jpeg I built a very simple user control that contains a picture box set to an icon. The UserControl constructor sets SetStyle( ControlStyles.SupportTransparentBackColor, true); this.BackColor = Color.Transparent; The control does nothing except display the icon.
4
9372
by: ray well | last post by:
in my app i need to make a RichTextbox control transparent. i need it to be a like a pane of glass lying on a sheet of paper, where u can see everything on the sheet of paper not covered by text written on the glass pane. i need to be able to see the control or form that is underneath the RichTextbox, and at the same time to be able to write and erase text to the RichTextbox. i'm assuming that it is the backcolor that hides what is...
0
2871
by: MasterOfTheDark | last post by:
This one's a hairy one to explain. What I've got is a class called "VistaGroupBox" that I put some nice-looking Vista-style gradients in the background and made to look like the ribbon categories you see in Office 2007. I got it working great, and created a few more controls, including a "VistaButton", which is primarily transparent when the cursor does not hover over it. However, when I added a standard Panel in the VistaGroupBox and put my...
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6661
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3551
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.