I've created a simple user control which is manually created with something like
MyUserControl ctrl = new MyUserControl();
The control have been designed to have BackColor = Color.Transparent and that works fine, until I set the Parent of the control to a form at which time it turns into the color of the form.
Might sound like its transparent but what it does is hide all the controls that exist on the form as well. I'm not 100% sure its the control that gets a solid background or something else thats happening when i hook it up, which prevents other controls from showing.
Basically if you do this
Create a form
Drop a button on it
In the click handler for the button you do the following
Example
MyUserControl ctrl = new MyUserControl();
ctrl.Parent = this;
ctrl.BackColor = Color.Transparent;
ctrl.Size = this.Parent.ClientRectangle.Size;
ctrl.Location = this.Parent.ClientRectangle.Location;
ctrl.BringToFront();
ctrl.Show();
Basically I want the usercontrol to overlay the entire form, while showing the underlaying controls on the form (hence the transparent background). I do not want to add it to the forms control collection because it doesn't really belong to the form, its just being shown ontop of everything else
I tried doing the same, but without setting the parent, but then the control didnt show at all.
Thanks!
EDIT: If I override the OnPaintBackground method in the usercontrol and prevent the background from being painted then it works, however that messes up with the transparent parts of a PNG image im painting in the control using DrawImage, which makes sense.
Windows Forms doesn't really support transparent controls.
You can work around this limitation by overriding the CreateParams property of the control and setting a custom style (look it up on google).
Further you have to override the painting of your control so that not only your control but also the parent control is redrawn. The reason is that the background must be painted before your control paints itself.
Finally you should override the OnPaintBackground method, as you have done, to make sure no background is painted.
Quite clumsy, and not perfect, but it should work.
Related
I have an application in which I have used radio button over an image so it seems very bad with white background in radio button.
So is there a way I can remove that white background ?
Only setting the BackColor to Color.Transparent is not enough to get rid of the little border that is around the RadioButton.
What you will need to also do is call the following code for each radio button to ensure that the background does actually go transparent
rbnTest.BackColor = Color.Transparent;
Point pos = this.PointToScreen(rbnTest.Location);
rbnTest.Parent = pibPicture;
rbnTest.Location = pibPicture.PointToClient(pos);
Source (Not a true duplicate, but similar, hence not flaggin as duplicate)
I would recommend refactoring that code into a reusable method so that you do not scatter the code all over your project.
Locate the constructor for your control class. The constructor appears in the control's code file. In C#, the constructor is the method with the same name as the control and with no return value.
Call the SetStyle method of your form in the constructor.
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
add the following line. This will set your control's BackColor to Transparent.
this.BackColor = Color.Transparent;
Note
Windows Forms controls do not support true transparency. The background of a transparent Windows Forms control is painted by its parent.
You can use the RadioButton.BackgroundImage or the RadioButton.BackColor property. Choose the one that suits you best
I'm creating a WPF 'Button' control and making it the child of my ElementHost control.
The background of the button is set to Red.
When I run my project, the button changes colour and it seems to loop through every couple of seconds going from Red to a light blue and back again.. until the Form loses focus.
If I hover over the button, it turns blue, then when I move off the button it starts this colour looping again.. my code is as simple as..
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
System.Windows.Controls.Button but = new System.Windows.Controls.Button();
but.Background = System.Windows.Media.Brushes.Red;
elementHost1.Child = but;
}
}
Is this normal ? can I turn it off?
Yes, it's normal. WPF buttons always do a subtle color cycling effect when they have input focus, and that's the case for your button since it's the only control on the form. It's not very subtle in your example because Red is a long way from the other blueish color the button cycles through -- delete that Background assignment to see the effect as intended. Try adding another ElementHost with another WPF Button to the same form, and you'll see that only the focused button does the color cycling.
As for how to turn it off... I don't know but I'm afraid it's not easy. I don't see any simple property on Button that would change this effect. Such effects are generally achieved by WPF style templates which is a subject that makes grown men cry. You can find the MSDN overview below, but note that this assumes you're working within WPF and XAML, not within Windows Forms:
http://msdn.microsoft.com/en-us/library/bb613570.aspx
My guess is that you would have to associate a changed Focused style with your button that won't do the color cycling, or else discover the resource name of the second brush (other than Background) that the Focused style cycles to, and set that resource to the same color as Background.
Is there any way to make a panel transparent or semi transparent? I haven't found any appropriate property to set transparency for a panel.
I was also trying to make a WPF control with grid (grid background and control background was set to transparent) and place it on normal windows form, but when I put this control on normal Windows Form(not WPF) I don't get the proper transparency.
If your display is pretty much static, you can do this to achieve semi-transparency (Source):
class SeeThroughPanel : Panel
{
public SeeThroughPanel()
{
}
protected override CreateParams CreateParams {
get {
var cp = base.CreateParams;
cp.ExStyle |= 0x00000020;
return cp;
}
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(50, 0, 0, 0)), this.ClientRectangle);
}
}
But, this approach is not without problems if you need dynamic rendering on your semi-transparent control. See this question which I have posted. Hope it gets answered someday.
Unfortunately, transparency is not natively or well supported in WinForms and is difficult to implement yourself.
If you want a transparent panel that allows you to "see through" the form, take a look at this article, which tells you to set both the Panel's TransparencyKey and BackColor to something like Fuscia. With text/other stuff in the panel this effect may not look so good.
If you want a transparent or translucent panel that shows the form below take a look at this SO question.
I would use WPF all the way, if you are able to use it in a form now.
I'm not sure about making the panel semi-transparent, but I know you can use the TransparencyKey property of the form to create completely transparent sections.
For example, set the form's TransparencyKey property to Fuchsia, then set the panel's BackColor to Fuchsia, and it will create a transparent area on the form.
Edit:
Agree with #Callum Rogers about going with WPF. Text directly on the panel shows fringing (see the label in the screenshot). However, if you need to add this quickly to an existing Win Forms app and don't need to show text directly on the panel, it could work for you.
I am just trying to create a form control in winform in .net with custom shaped of balloon shape.
There is need of a balloon tooltip which is transparent and I can put buttons on tooltip,but
tooltip in .net does not provide facality that we can put the buttons on tooltip control so
I want to make a form control looks like a balloon tooltip and so I can put buttons on that form looking like a tooltip.But I cannot show window form control look like a balloon tooltip.
So what should I do??
I tried in one way that I create a image in powerpoint of balloon shape and set it to as background image of form property.But there is no solution with that.
The Control class supports a BackColor with an alpha < 255, it is automatic. It asks the Parent to draw itself to produce the background of the control, then draws itself on top of that. However, you'll want a top-level window for a balloon. That's a window type that can arbitrarily overlap another window and isn't confined by the client area of an underlying window. It has no Parent. A ToolTip is such a window.
The only control available in Windows Forms that can be an top-level window is a Form. Problem is: the transparency trick no longer works. Since a top-level window doesn't have a Parent, there isn't any obvious window to ask to draw the background. It could be many windows, belonging to other processes. You can get transparency in a Form with its TransparencyKey property. But that's a "hard" transparency, equivalent to an alpha of 0. You probably want a soft one. Another nasty problem is that drawing anti-aliased (ClearType) text no longer works since there is no reliable background pixel color anymore.
Long story short: you can't make this work well unless you confine the balloon to the client area of a form. A control, not a form.
You can try to hook on the Paint event of the control and draw the Visual of the button there.
I want to show a transparent panel on top of another panel, both the panels have child controls like labels, text boxes etc. Transparency works fine if the transparent panel is child control of the other panel but if not then the label and text box of the normal panel appears on top of the transparent panel. Transparency for rest of the area works fine.
Any ideas ???
I have tried bring the transparent panel to the front but did not help. Perhaps I need to specify the order in which the controls should be drawn ?? If yes how do I do that ?
Interestingly if I move the application below the task bar and bring it up. It achives the right result. ( Reprinting resolves the issue !! but why??). However when I minimize it and restore it does not fix it!
Thanks,
Transparency in Windows.Forms is implemented by relational hierarchy rather than visual hierarchy. When a transparent control is painted, .NET basically calls up the Parent tree asking each parent control to paint itself, then paints the actual control content itself.
Two siblings in the same control will paint over each other.
So to answer the question, the topmost panel/control needs to be a child of the control you want to paint on top of.
If you want a transparent control that won't obscure its siblings, according to Bob Powell you can do it by overriding the CreateParams method to add true transparency. Read the link for a full description.
If all your panels/labels/controls are part of a single UserControl then you are probably right that you just need to set the Z-order (the front-to-back order). You can do this using the Document Outline inside of Visual Studio's Designer under View > Other Windows > Document Outline.
If you're doing it programatically then you can play around with calling Control.BringToFront() or Control.SendToBack() to change their z-order. One possible way to do this is like (assuming ctl1 is intended to be at the back and ctl4 is intended to be front-mode.
SuspendLayout()
ctl1.BringToFront()
ctl2.BringToFront() ' Bring ctl2 in front of ctl1
ctl3.BringToFront() ' 3 in front of 2 (and in turn 1)
ctl4.BringToFront() ' 4 in front of the rest
ResumeLayout()
The Suspend/Resume Layout calls prevent the UI from updating while you rearrange things.