Click here to Skip to main content
15,911,132 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have customized a lucid control myself and when using it in two ways It works this way but that way.
The way 1:
The class of lucid control is included in the same namespace with the class of main form. I create an instance of my lucid control class and add it to the Controls of a picturebox on the main form and run the app. It works well as a tranparent backcolor control.
The way 2:
Now, I build the lucid control class into a dll file and create a new project. In this project, I also have a main form, a picturebox. I use my lucid control by adding it to the toolbox first and then drag and drop it from the toolbox onto the picturebox but sadly it doesn't work as a lucid control? If I drag and drop it on the main form, it works well as a lucid control.
Could you please suggest me with any ideas on why it is like that and how to solve?
Thank you so much!
Posted
Comments
BillWoodruff 24-Sep-11 6:26am    
When you say 'lucid' I think you mean transparent: 'lucid' is not generally used, technically, in this sense. That's no 'big deal:' but I would like to make absolutely sure that I know what you mean when say 'lucid.'

And, when you say 'control,' here: do mean UserControl ?

May I suggest you add some detail to your question about exactly what you are doing to enable transparency in your control.

In the project where your control works as expected, have you done anything in that project's 'Main' Form to enable transparency ?
[no name] 24-Sep-11 6:59am    
In fact, my control is inherited from the class Control. I make it a transparent back color simply by using SetStyle method to enable transparent backcolor (the default is disable). I also override some event raiser such as OnKeyPress (to update the Text property whenever user types, it works like a textbox), and override the OnPaint method to draw border and string (the Text) for my control. You can see that this control of mine works likely as a transparent textbox. I have smoothed some other features but it doesn't affect the way it is made transparent (as I said, simply using SetStyle ...). I have create an instance of it the way 1 and it works well but the way 2 doesn't.
If so everytime I want to use my control, I have to include (copy and paste) my original code of the transparent control class in my new project and unable to export it as a dll file and use it as a transparent control normally (I say normally because it works on main form but doesn't on a picturebox?).
Hope you have enough info to understand my problem.
Thank you so much

Hi, King Boy,

This is expected effect. What you use is not true transparency from the standpoint of Imaging used in PictureBox.

Instead, you need to use alpha channel (for example, using System.Drawing.Imaging.PixelFormat Format32bppArgb, Format32bppPArgb, Format64bppArgb or Format64bppPArgb (http://msdn.microsoft.com/en-us/library/system.drawing.imaging.pixelformat.aspx[^]). In particular, PNG format (default for .NET bitmaps) uses alpha channel.

Also, you did not explain the purpose of using the control System.Windows.Forms.PictureBox. This control is often heavily misused. It looks like you do the same when you drag and drop an image on a PictureBox. Even though you can do it, this control does not help you to implement desired behavior, only presents extra hassles and eat up resources with no purpose. If you need anything dynamic, or interactive, or animated, using a custom control with direct rendering using OnPaint is much better. I explained how to do it here: How do I clear a panel from old drawing[^].

—SA
 
Share this answer
 
Comments
[no name] 27-Sep-11 15:26pm    
Please see my comment under the solution 2!
I want to say more that my customized control is inherited from System.Windows.Forms.Control and I surely made it override some methods including OnPaint.
The picturebox is only a background for my transparent control. Please see more about my clarifications in the comment under Solution 2.
Thank you so much!
SA's suggestion you not use a PictureBox is, I think, a very good idea.

Without seeing your code, this is only a "guess," but I would think a Panel might be a good container for your Transparent Control. Are you familiar with this technique for enabling transparency in Controls ?:
C#
protected override CreateParams CreateParams
{
  get
  {
    CreateParams cp=base.CreateParams;
    cp.ExStyle|=0x00000020; //WS_EX_TRANSPARENT
    return cp;
  }
}

If not, please see:[^].

The above link is to a cached version of Bob Powell's site "Windows Forms Tips and Tricks:" the original site appears to have been taken off-line.

best, Bill
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 25-Sep-11 10:56am    
I fixed just the formatting of source code: this is "cs" not "css" :-)

Good idea, my 5, but
1) most likely, OP needs transparency of images put on controls, not control itself;
2) this is not portable to non-Windows platforms (while GDI+ is portable);
3) no need to take panel, Control is better, because you need to sub-class a control anyway, even if you won't use the sample shown above, access to SetStyle requires it to set up DoubleBuffering and AllDrawingInWmPaint.
--SA
BillWoodruff 25-Sep-11 17:19pm    
Hi SA, just curious; on what non-Windows platforms will GDI+ work ? Not sure what you fixed, but thanks for the edit. best, Bill
[no name] 27-Sep-11 15:14pm    
Wow! I didn't think there has still been someone paying attention to my questions (not only this).
Firstly, thank you all.
Here are some clarifications on my problems:
1. I really need the answer for "Why?" not for "Which one should be used?"
2. In fact I need a control to help me display an image and this control itself is larger than the form size, so I need to contain it in another container supporting auto scrollbars such as Panel. I will try another instead but all features above must be supported.
3. My idea is to draw string at any position on the image, user can move my tranparent "TextBox" to the point he/she wants and starts typing the desired text. Then he or she can save a copy (with some text drawn on the image) of the image and maybe print this copy.
That's one of the most popular and useful features of Image/picture editors such as Paint, PhotoShop...
Hope you understand my problem.
Thank you!
BillWoodruff 28-Sep-11 3:29am    
I think I couldn't begin to evaluate the "why" without seeing your full source code ... but please don't post that here ... and I don't think anyone here would here have time to try and debug your app for you.

I think you are going to have to switch to custom drawing using GDI+ to achieve what you want, and suggest you start by doing thorough research here on CP and on StackOveFlow.

Here's a good start on CP: "How to Use Transparent Images and Labels in Windows Forms
By Nuno Freitas" http://www.codeproject.com/KB/dotnet/transparent_controls_net.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900